Skip to content

Commit 6f17b75

Browse files
mishushakovclaude
andauthored
docs: fix out-of-date PTY Python examples (#279)
* docs: fix out-of-date PTY Python examples The Python PTY examples used a JS-style API that the Python SDK doesn't have. Update them to the real SDK: Sandbox.create(), PtySize(rows, cols), and the on_pty callback passed to handle.wait() instead of on_data in create(). The JS/TS examples already matched the current SDK. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: make Create a PTY session a full hello-world example Run echo 'hello world', exit, and print the output, in both Python and TypeScript, so the first example is runnable end to end. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 078a3bc commit 6f17b75

1 file changed

Lines changed: 70 additions & 72 deletions

File tree

docs/sandbox/pty.mdx

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Unlike `commands.run()` which executes a command and returns output after comple
1313

1414
## Create a PTY session
1515

16-
Use `sandbox.pty.create()` to start an interactive bash shell.
16+
Use `sandbox.pty.create()` to start an interactive bash shell. This example runs `echo 'hello world'`, then exits and prints the terminal output.
1717

1818
<CodeGroup>
1919
```js JavaScript & TypeScript
@@ -22,37 +22,56 @@ import { Sandbox } from 'e2b'
2222
const sandbox = await Sandbox.create()
2323

2424
const terminal = await sandbox.pty.create({
25-
cols: 80, // Terminal width in characters
26-
rows: 24, // Terminal height in characters
25+
cols: 80, // Terminal size in characters
26+
rows: 24,
27+
envs: { MY_VAR: 'hello' }, // Optional environment variables
28+
cwd: '/home/user', // Optional working directory
29+
user: 'root', // Optional user to run as
2730
onData: (data) => {
28-
// Called whenever terminal outputs data
31+
// Called whenever the terminal outputs data
2932
process.stdout.write(data)
3033
},
31-
envs: { MY_VAR: 'hello' }, // Optional environment variables
32-
cwd: '/home/user', // Optional working directory
33-
user: 'root', // Optional user to run as
3434
})
3535

3636
// terminal.pid contains the process ID
3737
console.log('Terminal PID:', terminal.pid)
38+
39+
// Send input to the PTY. Data must be bytes (Uint8Array), and the trailing newline "presses Enter".
40+
await sandbox.pty.sendInput(terminal.pid, new TextEncoder().encode("echo 'hello world'\n"))
41+
42+
// The PTY runs an interactive login shell that won't exit on its own, so tell it
43+
// to exit — otherwise wait() below blocks forever.
44+
await sandbox.pty.sendInput(terminal.pid, new TextEncoder().encode('exit\n'))
45+
46+
// Wait for the terminal to exit (output is streamed to onData above)
47+
await terminal.wait()
3848
```
3949

4050
```python Python
41-
from e2b import Sandbox
51+
from e2b import Sandbox, PtySize
4252

43-
sandbox = Sandbox()
53+
sandbox = Sandbox.create()
4454

4555
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
49-
envs={'MY_VAR': 'hello'}, # Optional environment variables
50-
cwd='/home/user', # Optional working directory
51-
user='root', # Optional user to run as
56+
size=PtySize(rows=24, cols=80), # Terminal size in characters
57+
envs={'MY_VAR': 'hello'}, # Optional environment variables
58+
cwd='/home/user', # Optional working directory
59+
user='root', # Optional user to run as
5260
)
5361

5462
# terminal.pid contains the process ID
5563
print('Terminal PID:', terminal.pid)
64+
65+
# Send input to the PTY via the pty module. Data must be bytes, and the trailing newline "presses Enter".
66+
sandbox.pty.send_stdin(terminal.pid, b"echo 'hello world'\n")
67+
68+
# The PTY runs an interactive login shell that won't exit on its own, so tell it
69+
# to exit — otherwise wait() below blocks forever.
70+
sandbox.pty.send_stdin(terminal.pid, b"exit\n")
71+
72+
# Stream output by passing on_pty to wait()
73+
# end='' prevents print from adding an extra newline
74+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
5675
```
5776
</CodeGroup>
5877

@@ -79,24 +98,24 @@ const terminal = await sandbox.pty.create({
7998
```
8099

81100
```python Python
82-
from e2b import Sandbox
101+
from e2b import Sandbox, PtySize
83102

84-
sandbox = Sandbox()
103+
sandbox = Sandbox.create()
85104

86-
# end='' prevents print() from adding an extra newline
87-
# (PTY output already contains newlines)
88105
terminal = sandbox.pty.create(
89-
cols=80,
90-
rows=24,
91-
on_data=lambda data: print(data.decode(), end=''),
106+
PtySize(rows=24, cols=80),
92107
timeout=0, # Keep the session open indefinitely
93108
)
109+
110+
# end='' prevents print() from adding an extra newline
111+
# (PTY output already contains newlines)
112+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
94113
```
95114
</CodeGroup>
96115

97116
## Send input to PTY
98117

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.
118+
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 `onData` callback (JavaScript) or to the `on_pty` callback you pass to `wait()` (Python).
100119

101120
<CodeGroup>
102121
```js JavaScript & TypeScript
@@ -118,19 +137,19 @@ await sandbox.pty.sendInput(
118137
```
119138

120139
```python Python
121-
from e2b import Sandbox
140+
from e2b import Sandbox, PtySize
122141

123-
sandbox = Sandbox()
142+
sandbox = Sandbox.create()
124143

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-
)
144+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
130145

131146
# Send a command as bytes (b'...' is Python's byte string syntax)
132147
# Don't forget the newline!
133148
sandbox.pty.send_stdin(terminal.pid, b'echo "Hello from PTY"\n')
149+
150+
# Receive output by passing on_pty to wait()
151+
# end='' prevents print from adding an extra newline
152+
terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
134153
```
135154
</CodeGroup>
136155

@@ -158,18 +177,14 @@ await sandbox.pty.resize(terminal.pid, {
158177
```
159178

160179
```python Python
161-
from e2b import Sandbox
180+
from e2b import Sandbox, PtySize
162181

163-
sandbox = Sandbox()
182+
sandbox = Sandbox.create()
164183

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-
)
184+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
170185

171186
# Resize to new dimensions (in characters)
172-
sandbox.pty.resize(terminal.pid, cols=120, rows=40)
187+
sandbox.pty.resize(terminal.pid, PtySize(rows=40, cols=120))
173188
```
174189
</CodeGroup>
175190

@@ -214,38 +229,29 @@ await reconnected.wait()
214229
```
215230

216231
```python Python
217-
import time
218-
from e2b import Sandbox
232+
from e2b import Sandbox, PtySize
219233

220-
sandbox = Sandbox()
234+
sandbox = Sandbox.create()
221235

222236
# 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-
)
237+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
228238

229239
pid = terminal.pid
230240

231241
# Send a command
232242
sandbox.pty.send_stdin(pid, b'echo hello\n')
233-
time.sleep(0.5)
234243

235244
# Disconnect - PTY keeps running in the background
236245
terminal.disconnect()
237246

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-
)
247+
# Later: reconnect to the same session
248+
reconnected = sandbox.pty.connect(pid)
243249

244-
# Continue using the session
245-
sandbox.pty.send_stdin(pid, b'echo world\n')
250+
# Continue using the session, then exit
251+
sandbox.pty.send_stdin(pid, b'echo world\nexit\n')
246252

247-
# Wait for the terminal to exit
248-
reconnected.wait()
253+
# Wait for the terminal to exit, streaming output via on_pty
254+
reconnected.wait(on_pty=lambda data: print('Handler:', data.decode()))
249255
```
250256
</CodeGroup>
251257

@@ -274,15 +280,11 @@ console.log('Killed:', killed) // true if successful
274280
```
275281

276282
```python Python
277-
from e2b import Sandbox
283+
from e2b import Sandbox, PtySize
278284

279-
sandbox = Sandbox()
285+
sandbox = Sandbox.create()
280286

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-
)
287+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
286288

287289
# Kill the PTY
288290
killed = sandbox.pty.kill(terminal.pid)
@@ -318,21 +320,17 @@ console.log('Exit code:', result.exitCode)
318320
```
319321

320322
```python Python
321-
from e2b import Sandbox
323+
from e2b import Sandbox, PtySize
322324

323-
sandbox = Sandbox()
325+
sandbox = Sandbox.create()
324326

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-
)
327+
terminal = sandbox.pty.create(PtySize(rows=24, cols=80))
330328

331329
# Send exit command
332330
sandbox.pty.send_stdin(terminal.pid, b'exit\n')
333331

334-
# Wait for the terminal to exit
335-
result = terminal.wait()
332+
# Wait for the terminal to exit, streaming output via on_pty
333+
result = terminal.wait(on_pty=lambda data: print(data.decode(), end=''))
336334
print('Exit code:', result.exit_code)
337335
```
338336
</CodeGroup>

0 commit comments

Comments
 (0)