Skip to content

Commit 71a6461

Browse files
committed
docs: fix PTY Python examples to match the real SDK API
1 parent bc7485f commit 71a6461

1 file changed

Lines changed: 41 additions & 60 deletions

File tree

docs/sandbox/pty.mdx

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const terminal = await sandbox.pty.create({
2525
cols: 80, // Terminal width in characters
2626
rows: 24, // Terminal height in characters
2727
onData: (data) => {
28-
// Called whenever terminal outputs data
29-
process.stdout.write(data)
28+
// Called whenever terminal outputs data (decode the Uint8Array to text)
29+
process.stdout.write(new TextDecoder().decode(data))
3030
},
3131
envs: { MY_VAR: 'hello' }, // Optional environment variables
3232
cwd: '/home/user', // Optional working directory
@@ -38,21 +38,24 @@ console.log('Terminal PID:', terminal.pid)
3838
```
3939

4040
```python Python
41-
from e2b import Sandbox
41+
from e2b import Sandbox, PtySize
4242

4343
sandbox = Sandbox()
4444

45+
# PtySize takes the terminal dimensions in characters
4546
terminal = sandbox.pty.create(
46-
cols=80, # Terminal width in characters
47-
rows=24, # Terminal height in characters
48-
on_data=lambda data: print(data.decode(), end=''), # end='' prevents print from adding extra newline
47+
PtySize(rows=24, cols=80),
4948
envs={'MY_VAR': 'hello'}, # Optional environment variables
5049
cwd='/home/user', # Optional working directory
5150
user='root', # Optional user to run as
5251
)
5352

5453
# terminal.pid contains the process ID
5554
print('Terminal PID:', terminal.pid)
55+
56+
# Handle output by passing on_pty to wait(). The data is bytes, so decode it.
57+
# end='' prevents print from adding an extra newline (PTY output already contains newlines).
58+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
5659
```
5760
</CodeGroup>
5861

@@ -73,30 +76,30 @@ const sandbox = await Sandbox.create()
7376
const terminal = await sandbox.pty.create({
7477
cols: 80,
7578
rows: 24,
76-
onData: (data) => process.stdout.write(data),
79+
onData: (data) => process.stdout.write(new TextDecoder().decode(data)),
7780
timeoutMs: 0, // Keep the session open indefinitely
7881
})
7982
```
8083

8184
```python Python
82-
from e2b import Sandbox
85+
from e2b import Sandbox, PtySize
8386

8487
sandbox = Sandbox()
8588

86-
# end='' prevents print() from adding an extra newline
87-
# (PTY output already contains newlines)
8889
terminal = sandbox.pty.create(
89-
cols=80,
90-
rows=24,
91-
on_data=lambda data: print(data.decode(), end=''),
90+
PtySize(rows=24, cols=80),
9291
timeout=0, # Keep the session open indefinitely
9392
)
93+
94+
# Handle output by passing on_pty to wait(). The data is bytes, so decode it.
95+
# end='' prevents print() from adding an extra newline (PTY output already contains newlines).
96+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
9497
```
9598
</CodeGroup>
9699

97100
## Send input to PTY
98101

99-
Use `sendInput()` in JavaScript or `send_stdin()` in Python to send data to the terminal. These methods return a Promise (JavaScript) or complete synchronously (Python) - the actual output will be delivered to your `onData` callback.
102+
Use `sendInput()` in JavaScript or `send_stdin()` in Python to send data to the terminal. These methods return a Promise (JavaScript) or complete synchronously (Python) - the actual output is delivered to your output callback (`onData` in JavaScript, or `on_pty` passed to `wait()` in Python).
100103

101104
<CodeGroup>
102105
```js JavaScript & TypeScript
@@ -107,7 +110,7 @@ const sandbox = await Sandbox.create()
107110
const terminal = await sandbox.pty.create({
108111
cols: 80,
109112
rows: 24,
110-
onData: (data) => process.stdout.write(data),
113+
onData: (data) => process.stdout.write(new TextDecoder().decode(data)),
111114
})
112115

113116
// Send a command (don't forget the newline!)
@@ -118,19 +121,18 @@ await sandbox.pty.sendInput(
118121
```
119122

120123
```python Python
121-
from e2b import Sandbox
124+
from e2b import Sandbox, PtySize
122125

123126
sandbox = Sandbox()
124127

125-
terminal = sandbox.pty.create(
126-
cols=80,
127-
rows=24,
128-
on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline
129-
)
128+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
130129

131130
# Send a command as bytes (b'...' is Python's byte string syntax)
132131
# Don't forget the newline!
133132
sandbox.pty.send_stdin(terminal.pid, b'echo "Hello from PTY"\n')
133+
134+
# Receive output via on_pty on wait() (data is bytes, so decode it)
135+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
134136
```
135137
</CodeGroup>
136138

@@ -147,7 +149,7 @@ const sandbox = await Sandbox.create()
147149
const terminal = await sandbox.pty.create({
148150
cols: 80,
149151
rows: 24,
150-
onData: (data) => process.stdout.write(data),
152+
onData: (data) => process.stdout.write(new TextDecoder().decode(data)),
151153
})
152154

153155
// Resize to new dimensions (in characters)
@@ -158,18 +160,14 @@ await sandbox.pty.resize(terminal.pid, {
158160
```
159161

160162
```python Python
161-
from e2b import Sandbox
163+
from e2b import Sandbox, PtySize
162164

163165
sandbox = Sandbox()
164166

165-
terminal = sandbox.pty.create(
166-
cols=80,
167-
rows=24,
168-
on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline
169-
)
167+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
170168

171169
# Resize to new dimensions (in characters)
172-
sandbox.pty.resize(terminal.pid, cols=120, rows=40)
170+
sandbox.pty.resize(terminal.pid, PtySize(rows=40, cols=120))
173171
```
174172
</CodeGroup>
175173

@@ -214,38 +212,29 @@ await reconnected.wait()
214212
```
215213

216214
```python Python
217-
import time
218-
from e2b import Sandbox
215+
from e2b import Sandbox, PtySize
219216

220217
sandbox = Sandbox()
221218

222219
# Create a PTY session
223-
terminal = sandbox.pty.create(
224-
cols=80,
225-
rows=24,
226-
on_data=lambda data: print('Handler 1:', data.decode()),
227-
)
220+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
228221

229222
pid = terminal.pid
230223

231224
# Send a command
232225
sandbox.pty.send_stdin(pid, b'echo hello\n')
233-
time.sleep(0.5)
234226

235227
# Disconnect - PTY keeps running in the background
236228
terminal.disconnect()
237229

238-
# Later: reconnect with a new data handler
239-
reconnected = sandbox.pty.connect(
240-
pid,
241-
on_data=lambda data: print('Handler 2:', data.decode()),
242-
)
230+
# Later: reconnect to the running session
231+
reconnected = sandbox.pty.connect(pid)
243232

244233
# Continue using the session
245234
sandbox.pty.send_stdin(pid, b'echo world\n')
246235

247-
# Wait for the terminal to exit
248-
reconnected.wait()
236+
# Receive output via on_pty on wait() and wait for the terminal to exit
237+
reconnected.wait(on_pty=lambda data: print('Handler 2:', data.decode()))
249238
```
250239
</CodeGroup>
251240

@@ -262,7 +251,7 @@ const sandbox = await Sandbox.create()
262251
const terminal = await sandbox.pty.create({
263252
cols: 80,
264253
rows: 24,
265-
onData: (data) => process.stdout.write(data),
254+
onData: (data) => process.stdout.write(new TextDecoder().decode(data)),
266255
})
267256

268257
// Kill the PTY
@@ -274,15 +263,11 @@ console.log('Killed:', killed) // true if successful
274263
```
275264

276265
```python Python
277-
from e2b import Sandbox
266+
from e2b import Sandbox, PtySize
278267

279268
sandbox = Sandbox()
280269

281-
terminal = sandbox.pty.create(
282-
cols=80,
283-
rows=24,
284-
on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline
285-
)
270+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
286271

287272
# Kill the PTY
288273
killed = sandbox.pty.kill(terminal.pid)
@@ -306,7 +291,7 @@ const sandbox = await Sandbox.create()
306291
const terminal = await sandbox.pty.create({
307292
cols: 80,
308293
rows: 24,
309-
onData: (data) => process.stdout.write(data),
294+
onData: (data) => process.stdout.write(new TextDecoder().decode(data)),
310295
})
311296

312297
// Send exit command
@@ -318,21 +303,17 @@ console.log('Exit code:', result.exitCode)
318303
```
319304

320305
```python Python
321-
from e2b import Sandbox
306+
from e2b import Sandbox, PtySize
322307

323308
sandbox = Sandbox()
324309

325-
terminal = sandbox.pty.create(
326-
cols=80,
327-
rows=24,
328-
on_data=lambda data: print(data.decode(), end=''), # end='' prevents extra newline
329-
)
310+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
330311

331312
# Send exit command
332313
sandbox.pty.send_stdin(terminal.pid, b'exit\n')
333314

334-
# Wait for the terminal to exit
335-
result = terminal.wait()
315+
# Wait for the terminal to exit, handling output via on_pty (data is bytes)
316+
result = terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
336317
print('Exit code:', result.exit_code)
337318
```
338319
</CodeGroup>

0 commit comments

Comments
 (0)