Last active
April 5, 2023 13:26
-
-
Save aksh-at/0cc3f1d8840b70f42732de274c804dc7 to your computer and use it in GitHub Desktop.
Jupyter on Modal through Bore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quick snippet to connect to a Jupyter notebook server running inside a Modal container, | |
# especially useful for exploring the contents of Modal shared volumes. | |
# This uses https://github.com/ekzhang/bore to expose the server to the public internet. | |
# | |
# Steps | |
# ----- | |
# 1. (Recommended) Change `JUPYTER_TOKEN` to a different value; default is 1234. | |
# 2. `modal run jupyter-bore.py` | |
# 3. Find the `bore.pub` URL printed in the logs, and navigate to it using your browser. | |
import os | |
import time | |
import modal | |
stub = modal.Stub( | |
image=modal.Image.debian_slim() | |
.pip_install("jupyter") | |
.apt_install("curl") | |
.run_commands("curl https://sh.rustup.rs -sSf | bash -s -- -y") | |
.run_commands(". $HOME/.cargo/env && cargo install bore-cli") | |
) | |
@stub.function(concurrency_limit=1, timeout=1000) | |
def run_jupyter(): | |
import subprocess | |
jupyter_process = subprocess.Popen( | |
[ | |
"jupyter", | |
"notebook", | |
"--no-browser", | |
"--allow-root", | |
"--port=8888", | |
"--NotebookApp.allow_origin='*'", | |
"--NotebookApp.allow_remote_access=1", | |
], | |
env={**os.environ, "JUPYTER_TOKEN": "1234"}, | |
) | |
bore_process = subprocess.Popen( | |
["/root/.cargo/bin/bore", "local", "8888", "--to", "bore.pub"], | |
) | |
try: | |
while True: | |
time.sleep(1) | |
except KeyboardInterrupt: | |
print("Exiting...") | |
bore_process.kill() | |
jupyter_process.kill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment