Skip to content

Commit 9a3f492

Browse files
committed
Add imports to all PTY code examples
Make all code examples copy-paste ready by including sandbox imports and initialization in every section.
1 parent 67cca0d commit 9a3f492

1 file changed

Lines changed: 81 additions & 31 deletions

File tree

docs/sandbox/pty.mdx

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Use `sandbox.pty.create()` to start an interactive bash shell.
1717

1818
<CodeGroup>
1919
```js JavaScript & TypeScript
20+
import { Sandbox } from '@e2b/code-interpreter'
21+
22+
const sandbox = await Sandbox.create()
23+
2024
const terminal = await sandbox.pty.create({
2125
cols: 80, // Terminal width in characters
2226
rows: 24, // Terminal height in characters
@@ -41,7 +45,7 @@ sandbox = Sandbox()
4145
terminal = sandbox.pty.create(
4246
cols=80, # Terminal width in characters
4347
rows=24, # Terminal height in characters
44-
on_data=lambda data: print(data.decode(), end=''), # Prints output without adding newlines
48+
on_data=lambda data: print(data.decode(), end=''), # end='' prevents print from adding extra newline
4549
envs={'MY_VAR': 'hello'}, # Optional environment variables
4650
cwd='/home/user', # Optional working directory
4751
user='root', # Optional user to run as
@@ -64,6 +68,10 @@ For long-running sessions, set `timeoutMs: 0` (JavaScript) or `timeout=0` (Pytho
6468

6569
<CodeGroup>
6670
```js JavaScript & TypeScript
71+
import { Sandbox } from '@e2b/code-interpreter'
72+
73+
const sandbox = await Sandbox.create()
74+
6775
const terminal = await sandbox.pty.create({
6876
cols: 80,
6977
rows: 24,
@@ -73,6 +81,12 @@ const terminal = await sandbox.pty.create({
7381
```
7482

7583
```python Python
84+
from e2b_code_interpreter import Sandbox
85+
86+
sandbox = Sandbox()
87+
88+
# end='' prevents print() from adding an extra newline
89+
# (PTY output already contains newlines)
7690
terminal = sandbox.pty.create(
7791
cols=80,
7892
rows=24,
@@ -88,6 +102,10 @@ Use `sendInput()` in JavaScript or `send_stdin()` in Python to send data to the
88102

89103
<CodeGroup>
90104
```js JavaScript & TypeScript
105+
import { Sandbox } from '@e2b/code-interpreter'
106+
107+
const sandbox = await Sandbox.create()
108+
91109
const terminal = await sandbox.pty.create({
92110
cols: 80,
93111
rows: 24,
@@ -102,6 +120,10 @@ await sandbox.pty.sendInput(
102120
```
103121

104122
```python Python
123+
from e2b_code_interpreter import Sandbox
124+
125+
sandbox = Sandbox()
126+
105127
terminal = sandbox.pty.create(
106128
cols=80,
107129
rows=24,
@@ -120,6 +142,10 @@ When the user's terminal window changes size, notify the PTY with `resize()`. Th
120142

121143
<CodeGroup>
122144
```js JavaScript & TypeScript
145+
import { Sandbox } from '@e2b/code-interpreter'
146+
147+
const sandbox = await Sandbox.create()
148+
123149
const terminal = await sandbox.pty.create({
124150
cols: 80,
125151
rows: 24,
@@ -134,6 +160,10 @@ await sandbox.pty.resize(terminal.pid, {
134160
```
135161

136162
```python Python
163+
from e2b_code_interpreter import Sandbox
164+
165+
sandbox = Sandbox()
166+
137167
terminal = sandbox.pty.create(
138168
cols=80,
139169
rows=24,
@@ -154,6 +184,10 @@ You can disconnect from a PTY session while keeping it running, then reconnect l
154184

155185
<CodeGroup>
156186
```js JavaScript & TypeScript
187+
import { Sandbox } from '@e2b/code-interpreter'
188+
189+
const sandbox = await Sandbox.create()
190+
157191
// Create a PTY session
158192
const terminal = await sandbox.pty.create({
159193
cols: 80,
@@ -183,6 +217,9 @@ await reconnected.wait()
183217

184218
```python Python
185219
import time
220+
from e2b_code_interpreter import Sandbox
221+
222+
sandbox = Sandbox()
186223

187224
# Create a PTY session
188225
terminal = sandbox.pty.create(
@@ -220,6 +257,10 @@ Terminate the PTY session with `kill()`.
220257

221258
<CodeGroup>
222259
```js JavaScript & TypeScript
260+
import { Sandbox } from '@e2b/code-interpreter'
261+
262+
const sandbox = await Sandbox.create()
263+
223264
const terminal = await sandbox.pty.create({
224265
cols: 80,
225266
rows: 24,
@@ -235,6 +276,10 @@ console.log('Killed:', killed) // true if successful
235276
```
236277

237278
```python Python
279+
from e2b_code_interpreter import Sandbox
280+
281+
sandbox = Sandbox()
282+
238283
terminal = sandbox.pty.create(
239284
cols=80,
240285
rows=24,
@@ -256,6 +301,10 @@ Use `wait()` to wait for the terminal session to end (e.g., when the user types
256301

257302
<CodeGroup>
258303
```js JavaScript & TypeScript
304+
import { Sandbox } from '@e2b/code-interpreter'
305+
306+
const sandbox = await Sandbox.create()
307+
259308
const terminal = await sandbox.pty.create({
260309
cols: 80,
261310
rows: 24,
@@ -271,6 +320,10 @@ console.log('Exit code:', result.exitCode)
271320
```
272321

273322
```python Python
323+
from e2b_code_interpreter import Sandbox
324+
325+
sandbox = Sandbox()
326+
274327
terminal = sandbox.pty.create(
275328
cols=80,
276329
rows=24,
@@ -298,42 +351,38 @@ For a fully interactive terminal experience similar to SSH, you need to:
298351
```js JavaScript & TypeScript
299352
import { Sandbox } from '@e2b/code-interpreter'
300353

301-
async function main() {
302-
const sandbox = await Sandbox.create()
354+
const sandbox = await Sandbox.create()
303355

304-
// Set terminal to raw mode for character-by-character input
305-
process.stdin.setRawMode(true)
356+
// Set terminal to raw mode for character-by-character input
357+
process.stdin.setRawMode(true)
306358

307-
const terminal = await sandbox.pty.create({
308-
cols: process.stdout.columns,
309-
rows: process.stdout.rows,
310-
onData: (data) => process.stdout.write(data),
311-
timeoutMs: 0,
312-
})
359+
const terminal = await sandbox.pty.create({
360+
cols: process.stdout.columns,
361+
rows: process.stdout.rows,
362+
onData: (data) => process.stdout.write(data),
363+
timeoutMs: 0,
364+
})
313365

314-
// Forward stdin to the PTY
315-
process.stdin.on('data', async (data) => {
316-
await sandbox.pty.sendInput(terminal.pid, data)
317-
})
366+
// Forward stdin to the PTY
367+
process.stdin.on('data', async (data) => {
368+
await sandbox.pty.sendInput(terminal.pid, data)
369+
})
318370

319-
// Handle terminal resize
320-
process.stdout.on('resize', () => {
321-
sandbox.pty.resize(terminal.pid, {
322-
cols: process.stdout.columns,
323-
rows: process.stdout.rows,
324-
})
371+
// Handle terminal resize
372+
process.stdout.on('resize', () => {
373+
sandbox.pty.resize(terminal.pid, {
374+
cols: process.stdout.columns,
375+
rows: process.stdout.rows,
325376
})
377+
})
326378

327-
// Wait for the session to end
328-
try {
329-
await terminal.wait()
330-
} finally {
331-
process.stdin.setRawMode(false)
332-
await sandbox.kill()
333-
}
379+
// Wait for the session to end
380+
try {
381+
await terminal.wait()
382+
} finally {
383+
process.stdin.setRawMode(false)
384+
await sandbox.kill()
334385
}
335-
336-
main()
337386
```
338387

339388
```python Python
@@ -355,6 +404,7 @@ async def main():
355404
# Set terminal to raw mode
356405
tty.setraw(sys.stdin.fileno())
357406

407+
# Write raw bytes directly to stdout (no extra newlines added)
358408
terminal = await sandbox.pty.create(
359409
size=PtySize(cols=80, rows=24),
360410
on_data=lambda data: sys.stdout.buffer.write(data) or sys.stdout.flush(),
@@ -387,5 +437,5 @@ asyncio.run(main())
387437
</CodeGroup>
388438

389439
<Note>
390-
This is the same pattern used by the [E2B CLI](https://github.com/e2b-dev/E2B/blob/main/packages/cli/src/terminal.ts) to provide interactive sandbox sessions. For production use, consider batching input to reduce network calls.
440+
This is the same pattern used by the [E2B CLI](https://github.com/e2b-dev/E2B/blob/main/packages/cli/src/terminal.ts) to provide interactive sandbox sessions. For production use, consider batching input to reduce network lag.
391441
</Note>

0 commit comments

Comments
 (0)