Skip to content

Commit 42355f6

Browse files
webserver example includes simple code + state
1 parent ac188ed commit 42355f6

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

docs/sandbox/auto-resume.mdx

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const sandbox = await Sandbox.create({
2929
})
3030
```
3131
```python Python
32+
import time
33+
3234
from e2b import Sandbox
3335

3436
sandbox = Sandbox.create(
@@ -81,17 +83,40 @@ const sandbox = await Sandbox.create({
8183
},
8284
})
8385

84-
// Example: app source already exists in /home/user/app.
85-
// Replace this command with your API server, Next.js, Vite, etc.
8686
await sandbox.commands.run(
87-
'cd /home/user/app && npm install && npm run dev -- --host 0.0.0.0 --port 3000',
87+
`python3 -m pip -q install 'flask>=2.2'`
88+
)
89+
90+
await sandbox.files.write(
91+
'/home/user/app.py',
92+
[
93+
'from flask import Flask',
94+
'app = Flask(__name__)',
95+
'@app.route("/")',
96+
'def hello():',
97+
' return "Hello, World!"',
98+
'app.run(host="0.0.0.0", port=3000)',
99+
'',
100+
].join('\n')
101+
)
102+
103+
await sandbox.commands.run(
104+
'python3 -u /home/user/app.py > /home/user/flask.log 2>&1',
88105
{ background: true }
89106
)
90107

108+
await new Promise((resolve) => setTimeout(resolve, 1000))
109+
91110
const previewHost = sandbox.getHost(3000)
92111
console.log(`Preview URL: https://${previewHost}`)
112+
113+
console.log(`Status before pause: ${sandbox.getInfo().state}`)
114+
await sandbox.pause()
115+
console.log(`Status after pause: ${sandbox.getInfo().state}`)
93116
```
94117
```python Python
118+
import time
119+
95120
from e2b import Sandbox
96121

97122
sandbox = Sandbox.create(
@@ -102,15 +127,31 @@ sandbox = Sandbox.create(
102127
},
103128
)
104129

105-
# Example: app source already exists in /home/user/app.
106-
# Replace this command with your API server, Next.js, Vite, etc.
130+
sandbox.commands.run("python3 -m pip -q install 'flask>=2.2'")
131+
132+
sandbox.files.write(
133+
"/home/user/app.py",
134+
'from flask import Flask\n'
135+
'app = Flask(__name__)\n'
136+
'@app.route("/")\n'
137+
'def hello():\n'
138+
' return "Hello, World!"\n'
139+
'app.run(host="0.0.0.0", port=3000)\n'
140+
)
141+
107142
sandbox.commands.run(
108-
"cd /home/user/app && npm install && npm run dev -- --host 0.0.0.0 --port 3000",
143+
"python3 -u /home/user/app.py > /home/user/flask.log 2>&1",
109144
background=True,
110145
)
111146

147+
time.sleep(1)
148+
112149
preview_host = sandbox.get_host(3000)
113150
print(f"Preview URL: https://{preview_host}")
151+
152+
print(f"Status before pause: {sandbox.get_info().state}")
153+
sandbox.pause()
154+
print(f"Status after pause: {sandbox.get_info().state}")
114155
```
115156
</CodeGroup>
116157

0 commit comments

Comments
 (0)