Skip to content

Commit

Permalink
adjusts formatting of sandbox examples (#946)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesfrye authored Oct 23, 2024
1 parent 871718b commit a507e49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
9 changes: 6 additions & 3 deletions 13_sandboxes/jupyter_sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

# ## Setting up the Sandbox

# All Sandboxes are associated with an App.

# We look up our app by name, creating it if it doesn't exist.

import json
import secrets
import time
import urllib.request

import modal

# All Sandboxes are associated with an App. We look up our app by name, creating it if it doesn't exist.

app = modal.App.lookup("example-jupyter", create_if_missing=True)

# We define a custom Docker image that has Jupyter and some other dependencies installed.
Expand Down Expand Up @@ -62,14 +64,15 @@
timeout=5 * 60, # 5 minutes
image=image,
app=app,
gpu=None, # add a GPU if you need it!
)

print(f"🏖️ Sandbox ID: {sandbox.object_id}")

# ## Communicating with a Jupyter server

# Next, we print out a URL that we can use to connect to our Jupyter server.
# Note that we have to call [`Sandbox.tunnels`](/docs/reference/modal.Sandbox#tunnels)
# Note that we have to call [`Sandbox.tunnels`](https://modal.com/docs/reference/modal.Sandbox#tunnels)
# to get the URL. The Sandbox is not publicly accessible until we do so.

tunnel = sandbox.tunnels()[JUPYTER_PORT]
Expand Down
33 changes: 14 additions & 19 deletions 13_sandboxes/safe_code_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
# pytest: false
# ---

# # Running arbitrary code in a sandboxed environment
# # Run arbitrary code in a sandboxed environment

# This example demonstrates how to run arbitrary code in a Modal [Sandbox](https://modal.com/docs/guide/sandbox).
# This example demonstrates how to run arbitrary code
# in multiple languages in a Modal [Sandbox](https://modal.com/docs/guide/sandbox).

# ## Setting up a multi-language environment

import modal
# Sandboxes allow us to run any kind of code in a safe environment.
# We'll use an image with a few different language runtimes to demonstrate this.

# Sandboxes allow us to run any kind of code in a safe environment. We'll use an image with a few
# different language runtimes to demonstrate this.
import modal

image = modal.Image.debian_slim(python_version="3.11").apt_install(
"nodejs", "ruby", "php"
Expand Down Expand Up @@ -47,6 +48,8 @@
print(php_ps.stdout.read(), end="")
print()

# The output should look something like

# ```
# hello from bash
# hello from python
Expand Down Expand Up @@ -75,10 +78,6 @@
result = combined_process.stdout.read().strip()
print(result)

# ```
# The sum of the random numbers is: 501
# ```

# For long-running processes, you can use stdout as an iterator to stream the output.

slow_printer = sandbox.exec(
Expand All @@ -96,25 +95,21 @@
for line in slow_printer.stdout:
print(line, end="")

# This should print something like

# ```
# Line 1: 2024-10-21 15:30:53 +0000
# Line 2: 2024-10-21 15:30:54 +0000
# Line 3: 2024-10-21 15:30:54 +0000
# Line 4: 2024-10-21 15:30:55 +0000
# Line 5: 2024-10-21 15:30:55 +0000
# Line 6: 2024-10-21 15:30:56 +0000
# Line 7: 2024-10-21 15:30:56 +0000
# Line 8: 2024-10-21 15:30:57 +0000
# Line 9: 2024-10-21 15:30:57 +0000
# ...
# Line 10: 2024-10-21 15:30:58 +0000
# ```

# Since sandboxes are safely separated from the rest of our system,
# Since Sandboxes are safely separated from the rest of our system,
# we can run very dangerous code in them!

sandbox.exec("rm", "-rf", "/", "--no-preserve-root")
sandbox.exec("rm", "-rfv", "/", "--no-preserve-root")

# This command has deleted the entire filesystem, so we can't run any more commands.
# Let's terminate the sandbox to finish the example.
# Let's terminate the Sandbox to clean up after ourselves.

sandbox.terminate()

0 comments on commit a507e49

Please sign in to comment.