Mojo Notebooks
A Jupyter notebook is a page in your browser made of cells. You type some code in a cell, run it, and the output appears right under it. Notes, code and results stay together, which makes notebooks great for experiments, teaching and sharing what you tried.
Mojo works in notebooks too. There’s no separate Mojo kernel: you use a normal Python notebook and mark Mojo cells with %%mojo. This page shows how to set that up and how Mojo cells behave, because they’re a bit different from Python cells.
How Mojo cells work
Section titled “How Mojo cells work”The mojo package you installed comes with a small Python module, mojo.notebook. When you import it in a notebook, it adds a cell magic called %%mojo. A cell magic is a special first line that tells Jupyter “handle this cell differently”.
When you run a %%mojo cell, it:
- saves the cell’s code to a temporary
.mojofile, - runs
mojo runon that file, - shows what the program printed.
So every Mojo cell is a complete, separate program, with its own def main():. Keep that in mind and the rest of this page will make sense.
Set up JupyterLab with uv
Section titled “Set up JupyterLab with uv”If you followed Mojo Get Started, you already have a uv project with Mojo in it. Add JupyterLab to the same project:
uv add jupyterlabThen start it:
uv run jupyter labJupyterLab prints a link with a token (something like http://localhost:8888/lab?token=...) and usually opens your browser. If it doesn’t, copy that link into the browser yourself.
In JupyterLab, choose File > New > Notebook and pick the Python 3 kernel. Notebooks are saved in your project folder as .ipynb files.
Set up with pixi (the official way)
Section titled “Set up with pixi (the official way)”The official docs use pixi instead of uv. These are their commands:
pixi init notebooks \ -c https://conda.modular.com/max-nightly/ \ -c conda-forgecd notebookspixi shellpixi add max jupyterlab ipykerneljupyter labThey install the max package rather than mojo. max includes the Mojo compiler, so %%mojo cells work the same, and it adds Modular’s GPU library for the GPU examples further down. Note that this uses the nightly channel (max-nightly), so you may get a newer Mojo than 1.2. Pick one tool for your project; this tutorial uses uv.
Use Google Colab
Section titled “Use Google Colab”Google Colab runs notebooks in the cloud, so you don’t install anything locally. It’s also a way to try Mojo GPU code without owning a supported GPU. According to the official page:
- Create a new notebook in Colab.
- In the first cell, install Mojo with MAX and run it:
!pip install max- Wait for the “Successfully installed” message, then continue with the next section.
Your first Mojo cell
Section titled “Your first Mojo cell”Whichever setup you chose, the first cell of the notebook must load the magic:
import mojo.notebookIt prints nothing. Now make a new cell and start it with %%mojo:
%%mojo
def main(): print("Hello from a notebook!")Output:
Hello from a notebook!Run a cell with Shift + Enter. The first Mojo cell takes a moment, because the code is compiled before it runs.
A cell can hold everything a normal Mojo file can, like helper functions above main:
%%mojo
def square(x: Int) -> Int: return x * x
def main(): for i in range(1, 4): print(i, "squared is", square(i))Output:
1 squared is 12 squared is 43 squared is 9Each cell is its own program
Section titled “Each cell is its own program”In a Python notebook, a variable you create in one cell is still there in the next. Mojo cells don’t work that way. Here’s a cell that creates score:
%%mojo
def main(): var score = 10 print("score is", score)Output:
score is 10And a later cell that tries to use it:
%%mojo
def main(): print(score + 1)MojoCompilationError: Error compiling Mojo at /tmp/tmpq0wx3s1e/cell.mojo. Command: run /tmp/tmpq0wx3s1e/cell.mojo
/tmp/tmpq0wx3s1e/cell.mojo:3:11: error: use of unknown declaration 'score' print(score + 1) ^~~~~mojo: error: failed to parse the provided Mojo source moduleThe second cell is a brand-new program, and score doesn’t exist in it. Each Mojo cell needs to create everything it uses. If you want to reuse a helper function, copy it into each cell that needs it.
Code still goes inside main
Section titled “Code still goes inside main”A Python cell can just say print("Hello"). A Mojo cell can’t, for the same reason a Mojo file can’t: code has to be inside a function.
%%mojo
print("Hello")/tmp/tmpqtw0jrac/cell.mojo:2:1: error: expressions must not appear at file scope; move this into a function bodyprint("Hello")^mojo: error: failed to parse the provided Mojo source moduleWrap it in def main(): and it works.
Mixing Python and Mojo cells
Section titled “Mixing Python and Mojo cells”You can have Python cells and Mojo cells in the same notebook. A plain Python cell works as always:
temps = [21.5, 23.0, 19.8]print(sum(temps) / len(temps))Output:
21.433333333333334But a Mojo cell can’t see temps. Remember, it’s a separate program, so print(temps) in a %%mojo cell fails with use of unknown declaration 'temps'.
What a Mojo cell can do is import Python modules itself:
%%mojo
from std.python import Python
def main() raises: var statistics = Python.import_module("statistics") var temps = Python.list(21.5, 23.0, 19.8) print(statistics.mean(temps))Output:
21.433333333333334That’s useful for loading data with a Python library and doing the heavy work in Mojo. Mojo and Python explains how the two sides talk to each other.
When a cell fails
Section titled “When a cell fails”Two things surprised us when testing:
- Every failure is called
MojoCompilationError, even when the program compiled fine and failed while running (for example an error raised inmain). Read the message under it to see what really happened. - When a program fails while running, its earlier output isn’t shown. A cell that prints
beforeand then raises an error only shows the error. If you need to see output up to the crash, run the code as a file withmojo runin a terminal instead.
GPU code in a notebook
Section titled “GPU code in a notebook”The official page has GPU examples for Colab, using a GPU runtime (Runtime > Change runtime type in Colab) and the max package. Here’s its smallest one:
%%mojo
from max.gpu.host import DeviceContext
def kernel(): print("Hello from the GPU")
def main() raises: # Launch GPU kernel with DeviceContext() as ctx: ctx.enqueue_function[kernel](grid_dim=1, block_dim=1) ctx.synchronize()The official page shows the output Hello from the GPU. We couldn’t run it, because our test machine has no GPU and the example needs the max package. If you try it, use a Colab GPU runtime or a machine with a supported GPU. GPU programming starts in Mojo GPU Basics.
Run a notebook from the terminal
Section titled “Run a notebook from the terminal”You can run a whole notebook without opening the browser, which is handy for checking that all cells still work after upgrading Mojo:
uv run jupyter nbconvert --to notebook --execute my_notebook.ipynb --output checked.ipynbThis runs every cell from top to bottom and saves the results in checked.ipynb. If a cell fails, it stops and tells you which one. That’s how we checked the outputs on this page.
Practice
Section titled “Practice”1. A times table cell
Section titled “1. A times table cell”Write a Mojo cell that prints the 7 times table from 7 x 1 = 7 to 7 x 5 = 35.
Hint: don’t forget the %%mojo line and def main():. print puts spaces between its values.
Show solution
%%mojo
def main(): for i in range(1, 6): print("7 x", i, "=", 7 * i)Output:
7 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 35The cell is a complete program: the %%mojo line, then main with the loop inside it.
2. Fix the notebook
Section titled “2. Fix the notebook”A notebook has these two Mojo cells. The second one fails with use of unknown declaration 'score'. Change the second cell so it prints 11.
%%mojo
def main(): var score = 10 print(score)%%mojo
def main(): print(score + 1)Hint: the second cell can’t see anything from the first one.
Show solution
%%mojo
def main(): var score = 10 print(score + 1)Output:
11Each Mojo cell is a separate program, so the second cell has to create score itself.
- Mojo runs in a normal Python notebook: run
import mojo.notebookfirst, then start Mojo cells with%%mojo. - Set it up locally with
uv add jupyterlabanduv run jupyter lab, with pixi as in the official docs, or in Google Colab. - Every Mojo cell is a complete program with its own
def main():. - Mojo cells don’t share variables with other cells, Mojo or Python.
- A Mojo cell can still import Python modules with
Python.import_module. - Failures show up as
MojoCompilationError, and output printed before a crash is lost.
Next up: Mojo Syntax, the basic rules every Mojo file follows.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Jupyter notebooks (official Mojo docs): setup for Colab and local JupyterLab, plus GPU examples.
- Project Jupyter: what notebooks are and the tools around them.
- JupyterLab documentation: how to use the JupyterLab interface.
- Google Colab: run notebooks in the cloud, with optional GPUs.