from moru import Sandbox# Create a sandbox with the default 'base' templatesandbox = Sandbox.create()print(f"Sandbox created: {sandbox.sandbox_id}")# Run a commandresult = sandbox.commands.run("echo 'Hello, World!'")print(f"Output: {result.stdout}")print(f"Exit code: {result.exit_code}")# Write a filesandbox.files.write("/tmp/hello.txt", "Hello from Moru!")# Read it backcontent = sandbox.files.read("/tmp/hello.txt")print(f"File content: {content}")# List directory contentsentries = sandbox.files.list("/tmp")for entry in entries: print(f" {entry.name} ({entry.type})")# Clean upsandbox.kill()print("Sandbox terminated")
Stream output in real-time for long-running commands:
Copy
from moru import Sandboxsandbox = Sandbox.create()# Stream output as it arrivessandbox.commands.run( "for i in 1 2 3; do echo $i; sleep 1; done", on_stdout=lambda data: print(f"stdout: {data}"), on_stderr=lambda data: print(f"stderr: {data}"), timeout=30)sandbox.kill()
Create a sandbox with a specific template. You can create custom templates with pre-installed dependencies via the dashboard or CLI.
Copy
from moru import Sandbox# Use a custom template (create one in the dashboard first)sandbox = Sandbox.create("my-template")result = sandbox.commands.run("echo 'Running in custom template'")print(result.stdout)sandbox.kill()