Skip to content

Commit e158e5c

Browse files
webserver example includes simple code + state (#148)
* webserver example includes simple code + state * await on the inner log * rm extra time import
1 parent ac188ed commit e158e5c

1 file changed

Lines changed: 45 additions & 6 deletions

File tree

docs/sandbox/auto-resume.mdx

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,40 @@ const sandbox = await Sandbox.create({
8181
},
8282
})
8383

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

106+
await new Promise((resolve) => setTimeout(resolve, 1000))
107+
91108
const previewHost = sandbox.getHost(3000)
92109
console.log(`Preview URL: https://${previewHost}`)
110+
111+
console.log(`Status before pause: ${(await sandbox.getInfo()).state}`)
112+
await sandbox.pause()
113+
console.log(`Status after pause: ${(await sandbox.getInfo()).state}`)
93114
```
94115
```python Python
116+
import time
117+
95118
from e2b import Sandbox
96119

97120
sandbox = Sandbox.create(
@@ -102,15 +125,31 @@ sandbox = Sandbox.create(
102125
},
103126
)
104127

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

145+
time.sleep(1)
146+
112147
preview_host = sandbox.get_host(3000)
113148
print(f"Preview URL: https://{preview_host}")
149+
150+
print(f"Status before pause: {sandbox.get_info().state}")
151+
sandbox.pause()
152+
print(f"Status after pause: {sandbox.get_info().state}")
114153
```
115154
</CodeGroup>
116155

0 commit comments

Comments
 (0)