Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates example to use Modal Tunnel #530

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions 11_notebooks/jupyter_inside_modal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#
# Quick snippet to connect to a Jupyter notebook server running inside a Modal container,
# especially useful for exploring the contents of Modal network file systems.
# This uses https://github.com/ekzhang/bore to expose the server to the public internet.
# This uses [Modal Tunnels](https://modal.com/docs/guide/tunnels#tunnels-beta)
# to create a tunnel between the running Jupyter instance and the internet.

import os
import subprocess
Expand All @@ -13,11 +14,9 @@
import modal

stub = modal.Stub(
image=modal.Image.debian_slim()
.pip_install("jupyter", "bing-image-downloader~=1.1.2")
.apt_install("curl")
.run_commands("curl https://sh.rustup.rs -sSf | bash -s -- -y")
.run_commands(". $HOME/.cargo/env && cargo install bore-cli")
image=modal.Image.debian_slim().pip_install(
"jupyter", "bing-image-downloader~=1.1.2"
)
)
# This volume is not persisted, so the data will be deleted when this demo app is stopped.
volume = modal.NetworkFileSystem.new()
Expand Down Expand Up @@ -55,33 +54,33 @@ def seed_volume():
concurrency_limit=1, network_file_systems={CACHE_DIR: volume}, timeout=1_500
)
def run_jupyter(timeout: int):
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": JUPYTER_TOKEN},
)

bore_process = subprocess.Popen(
["/root/.cargo/bin/bore", "local", "8888", "--to", "bore.pub"],
)

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:
bore_process.kill()
jupyter_process.kill()
jupyter_port = 8888
with modal.forward(jupyter_port) as tunnel:
jupyter_process = subprocess.Popen(
[
"jupyter",
"notebook",
"--no-browser",
"--allow-root",
"--ip=0.0.0.0",
f"--port={jupyter_port}",
"--NotebookApp.allow_origin='*'",
"--NotebookApp.allow_remote_access=1",
],
env={**os.environ, "JUPYTER_TOKEN": JUPYTER_TOKEN},
)

print(f"Jupyter available at => {tunnel.url}")

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:
jupyter_process.kill()


@stub.local_entrypoint()
Expand All @@ -93,5 +92,6 @@ def main(timeout: int = 10_000):


# Doing `modal run jupyter_inside_modal.py` will run a Modal app which starts
# the Juypter server at an address like http://bore.pub:$PORT/. Visit this address
# in your browser, and enter the security token you set for `JUPYTER_TOKEN`.
# the Juypter server at an address like https://u35iiiyqp5klbs.r3.modal.host.
# Visit this address in your browser, and enter the security token
# you set for `JUPYTER_TOKEN`.