moso provides a %%moso cell magic that lets you build up a Mojo
program across multiple notebook cells. All %%moso cells are
collected, compiled, and run together as a single Mojo script. The last
collected cell is wrapped in def main() raises:.
moso primarily supports SolveIt (from Answer.ai) because it allows the
tool to identify which cell is currently executing. In a standard
Jupyter Notebook environment, the magic simply collects all cells
containing %%moso.
- Iterative Mojo Programming with
%%mosoexplains whymosoexists and how it supports iterative Mojo development in SolveIt. - Learning Mojo with SolveIt walks through learning Mojo with SolveIt.
Install from PyPI:
pip install mosoOr install an editable checkout for local development:
pip install -e .from moso.core import *
setup_moso()from moso.core import *
setup_moso(nb_path='path/to/your/notebook.ipynb')Then write Mojo code in %%moso cells. Earlier %%moso cells are
treated as reusable definitions, and the final cell is wrapped in
def main() raises::
%%moso
def add(a: Int, b: Int) -> Int:
return a + b%%moso
print(add(40, 2))For example, check which GPU Mojo can see:
%%moso
from std.gpu.host import DeviceContext
var ctx = DeviceContext()
print(ctx.name())Pass an SSH host and command template if Mojo runs on another machine.
In a Jupyter notebook, include the notebook path so moso can collect
the %%moso cells from the file on disk:
from moso.core import *
setup_moso(
host="user@host",
cmd="cd ~/mojo-gpu-puzzles && /root/.pixi/bin/pixi run mojo run {path}",
nb_path="/path/to/your/notebook.ipynb",
)filter_moso
strips the %%moso lines, concatenates previous cells, and wraps the
current cell in a main() (or the last cell in standard Jupyter
Notebook).
setup_moso
registers the IPython cell magic and runs the generated source either
locally or over SSH.