Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

example: deploy editable notebooks in Modal (#3087)

Adit Shahf2c288b7dfd3
diff --git a/examples/cloud/modal/README.md b/examples/cloud/modal/README.md
index 033cb28ebd..abee3b05da 100644
--- a/examples/cloud/modal/README.md
+++ b/examples/cloud/modal/README.md
@@ -1,4 +1,7 @@
# Deploying marimo on modal
-This folder contains an example of how to deploy a marimo notebook as an
-application on modal.
+This folder contains examples of how to use marimo notebook on Modal.
+
+[modal_app.py](modal_app.py) has an example of how to deploy a read-only marimo notebook as an app on Modal.
+
+[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).
diff --git a/examples/cloud/modal/modal_edit.py b/examples/cloud/modal/modal_edit.py
new file mode 100644
index 0000000000..8e24c59bb9
--- /dev/null
+++ b/examples/cloud/modal/modal_edit.py
@@ -0,0 +1,46 @@
+import secrets
+import subprocess
+import time
+
+import modal
+
+app = modal.App(
+ image=modal.Image.debian_slim().pip_install(
+ "marimo>=0.9.32", "modal>=0.67.31"
+ ))
+
+TOKEN = secrets.token_urlsafe(16)
+PORT = 2718
+
+
[email protected](concurrency_limit=1, timeout=1_500)
+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",
+ ],
+ )
+
+ print(f"Marimo available at => {tunnel.url}?access_token={TOKEN}")
+
+ try:
+ end_time = time.time() + timeout
+ while time.time() < end_time:
+ time.sleep(5)
+ print(f"Reached end of {timeout} second timeout period. Exiting...")
+ except KeyboardInterrupt:
+ print("Exiting...")
+ finally:
+ marimo_process.kill()
+
+
[email protected]_entrypoint()
+def main():
+ run_marimo.remote(1000)