import { Sandbox } from 'e2b'
const sandbox = await Sandbox.create({
timeoutMs: 10 * 60 * 1000,
lifecycle: {
onTimeout: 'pause',
autoResume: true,
},
})
await sandbox.commands.run(
`python3 -m pip -q install 'flask>=2.2'`
)
await sandbox.files.write(
'/home/user/app.py',
[
'from flask import Flask',
'app = Flask(__name__)',
'@app.route("/")',
'def hello():',
' return "Hello, World!"',
'app.run(host="0.0.0.0", port=3000)',
'',
].join('\n')
)
await sandbox.commands.run(
'python3 -u /home/user/app.py > /home/user/flask.log 2>&1',
{ background: true }
)
await new Promise((resolve) => setTimeout(resolve, 1000))
const previewHost = sandbox.getHost(3000)
console.log(`Preview URL: https://${previewHost}`)