Skip to content

Commit

Permalink
examples: GPU notebooks with modal (#3711)
Browse files Browse the repository at this point in the history
Update the modal edit example to use a T4 instance, and to read from a
local notebook that queries nvidia-smi.

Outstanding issues:
- No venv installed (package installation not working)
- Edits to remote notebook not currently synced back to local
  • Loading branch information
akshayka authored Feb 6, 2025
1 parent 4aa3acd commit 1315b5b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
27 changes: 23 additions & 4 deletions examples/cloud/modal/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
# Deploying marimo on modal
# Running marimo on Modal

This folder contains examples of how to use marimo notebook on Modal.
This folder contains examples of how to run marimo notebooks on
[Modal](https://modal.com/), making it easy to get access to cloud GPUs. To get
started, first create a modal account and follow their onboarding. You'll also
need to install the [uv package manager](https://docs.astral.sh/uv/).

[modal_app.py](modal_app.py) has an example of how to deploy a read-only marimo notebook as an app on Modal.
## Editable notebooks
[modal_edit.py](modal_edit.py) has an example of how to spin up an editable
marimo notebook that runs on a Modal container. Run with

```bash
uvx -p 3.12 modal run modal_edit.py
```

You can configure your GPU selection by editing `modal_edit`.

## Run as apps

[modal_app.py](modal_app.py) has an example of how to deploy a read-only marimo
notebook as an app on Modal. Run with

```bash
uvx -p 3.12 modal run modal_run.py
```

[modal_edit.py](modal_edit.py) has an example of how to spin up an editable marimo notebook that runs on a Modal container. Run with `modal run modal_edit.py` (requires Modal account and configured credentials).
2 changes: 1 addition & 1 deletion examples/cloud/modal/modal_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import marimo

server = marimo.create_asgi_app().with_app(path="", root="/marimo/home.py")
server = marimo.create_asgi_app().with_app(path="", root="/marimo/notebook.py")

app = modal.App()

Expand Down
24 changes: 15 additions & 9 deletions examples/cloud/modal/modal_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@
import modal

app = modal.App(
image=modal.Image.debian_slim().pip_install(
"marimo>=0.9.32", "modal>=0.67.31"
))
image=modal.Image.debian_slim()
.pip_install("marimo>=0.9.32", "modal>=0.67.31")
.add_local_dir("nbs", remote_path="/root/nbs")
)

TOKEN = secrets.token_urlsafe(16)
PORT = 2718


@app.function(concurrency_limit=1, timeout=1_500)
@app.function(concurrency_limit=1, timeout=1_500, gpu="t4")
def run_marimo(timeout: int):
with modal.forward(PORT) as tunnel:
marimo_process = subprocess.Popen(
[
"marimo",
"edit",
"--headless",
"--host", "0.0.0.0",
"--port", str(PORT),
f"--token-password", TOKEN,
"notebook.py",
"--host",
"0.0.0.0",
"--port",
str(PORT),
f"--token-password",
TOKEN,
"nbs/notebook.py",
],
)

Expand All @@ -34,7 +38,9 @@ def run_marimo(timeout: int):
end_time = time.time() + timeout
while time.time() < end_time:
time.sleep(5)
print(f"Reached end of {timeout} second timeout period. Exiting...")
print(
f"Reached end of {timeout} second timeout period. Exiting..."
)
except KeyboardInterrupt:
print("Exiting...")
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ def __(mo):
@app.cell
def __():
import marimo as mo
return mo,
return (mo,)


@app.cell
def __():
import subprocess

subprocess.run(["nvidia-smi"])
return subprocess


if __name__ == "__main__":
Expand Down

0 comments on commit 1315b5b

Please sign in to comment.