Skip to content

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.

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:

  1. saves the cell’s code to a temporary .mojo file,
  2. runs mojo run on that file,
  3. 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.

If you followed Mojo Get Started, you already have a uv project with Mojo in it. Add JupyterLab to the same project:

Terminal window
uv add jupyterlab

Then start it:

Terminal window
uv run jupyter lab

JupyterLab 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.

The official docs use pixi instead of uv. These are their commands:

Terminal window
pixi init notebooks \
-c https://conda.modular.com/max-nightly/ \
-c conda-forge
cd notebooks
pixi shell
pixi add max jupyterlab ipykernel
jupyter lab

They 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.

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:

  1. Create a new notebook in Colab.
  2. In the first cell, install Mojo with MAX and run it:
!pip install max
  1. Wait for the “Successfully installed” message, then continue with the next section.

Whichever setup you chose, the first cell of the notebook must load the magic:

import mojo.notebook

It 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 1
2 squared is 4
3 squared is 9

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 10

And 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 module

The 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.

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 body
print("Hello")
^
mojo: error: failed to parse the provided Mojo source module

Wrap it in def main(): and it works.

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.433333333333334

But 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.433333333333334

That’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.

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 in main). 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 before and then raises an error only shows the error. If you need to see output up to the crash, run the code as a file with mojo run in a terminal instead.

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.

You can run a whole notebook without opening the browser, which is handy for checking that all cells still work after upgrading Mojo:

Terminal window
uv run jupyter nbconvert --to notebook --execute my_notebook.ipynb --output checked.ipynb

This 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.

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 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35

The cell is a complete program: the %%mojo line, then main with the loop inside it.

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:

11

Each 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.notebook first, then start Mojo cells with %%mojo.
  • Set it up locally with uv add jupyterlab and uv 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

1. What must the first cell of a Mojo notebook run?
2. One Mojo cell creates var score = 10. A later Mojo cell uses score. What happens?
3. Which kernel do you choose for a Mojo notebook?