Skip to content

Commit 67cca0d

Browse files
committed
Address PR review feedback
- Change sidebarTitle to "Interactive terminal" - Remove imports from JS snippet examples - Use sync Python code instead of async for examples - Add user parameter to create examples - Explain end='' in Python (prevents extra newlines) - Explain b'' byte string syntax in Python - Clarify cols/rows are in characters, not pixels - Clarify sendInput returns Promise, output goes to callback - Update timeout description to mention "connection to PTY session"
1 parent 7abb9c8 commit 67cca0d

1 file changed

Lines changed: 80 additions & 142 deletions

File tree

docs/sandbox/pty.mdx

Lines changed: 80 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Interactive terminal (PTY)"
3-
sidebarTitle: PTY
3+
sidebarTitle: Interactive terminal
44
---
55

66
The PTY (pseudo-terminal) module allows you to create interactive terminal sessions in the sandbox with real-time, bidirectional communication.
@@ -17,44 +17,38 @@ 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-
2420
const terminal = await sandbox.pty.create({
25-
cols: 80, // Terminal width in columns
26-
rows: 24, // Terminal height in rows
21+
cols: 80, // Terminal width in characters
22+
rows: 24, // Terminal height in characters
2723
onData: (data) => {
2824
// Called whenever terminal outputs data
2925
process.stdout.write(data)
3026
},
3127
envs: { MY_VAR: 'hello' }, // Optional environment variables
3228
cwd: '/home/user', // Optional working directory
29+
user: 'root', // Optional user to run as
3330
})
3431

3532
// terminal.pid contains the process ID
3633
console.log('Terminal PID:', terminal.pid)
3734
```
3835

3936
```python Python
40-
from e2b_code_interpreter import AsyncSandbox
41-
from e2b.sandbox_async.commands.pty import PtySize
42-
import asyncio
37+
from e2b_code_interpreter import Sandbox
4338

44-
async def main():
45-
sandbox = await AsyncSandbox.create()
39+
sandbox = Sandbox()
4640

47-
terminal = await sandbox.pty.create(
48-
size=PtySize(cols=80, rows=24),
49-
on_data=lambda data: print(data.decode(), end=''), # Called on output
50-
envs={'MY_VAR': 'hello'}, # Optional environment variables
51-
cwd='/home/user', # Optional working directory
52-
)
53-
54-
# terminal.pid contains the process ID
55-
print('Terminal PID:', terminal.pid)
41+
terminal = sandbox.pty.create(
42+
cols=80, # Terminal width in characters
43+
rows=24, # Terminal height in characters
44+
on_data=lambda data: print(data.decode(), end=''), # Prints output without adding newlines
45+
envs={'MY_VAR': 'hello'}, # Optional environment variables
46+
cwd='/home/user', # Optional working directory
47+
user='root', # Optional user to run as
48+
)
5649

57-
asyncio.run(main())
50+
# terminal.pid contains the process ID
51+
print('Terminal PID:', terminal.pid)
5852
```
5953
</CodeGroup>
6054

@@ -64,7 +58,7 @@ The PTY runs an interactive bash shell with `TERM=xterm-256color`, which support
6458

6559
## Timeout
6660

67-
By default, PTY sessions have a **60-second timeout** which limits the total duration of the session. When the timeout is reached, the connection will be closed regardless of activity.
61+
By default, PTY sessions have a **60-second timeout** which limits the total duration of the session. When the timeout is reached, the connection to the PTY session will be closed regardless of activity.
6862

6963
For long-running sessions, set `timeoutMs: 0` (JavaScript) or `timeout=0` (Python) to disable the timeout.
7064

@@ -79,8 +73,9 @@ const terminal = await sandbox.pty.create({
7973
```
8074

8175
```python Python
82-
terminal = await sandbox.pty.create(
83-
size=PtySize(cols=80, rows=24),
76+
terminal = sandbox.pty.create(
77+
cols=80,
78+
rows=24,
8479
on_data=lambda data: print(data.decode(), end=''),
8580
timeout=0, # No timeout for long-running sessions
8681
)
@@ -89,14 +84,10 @@ terminal = await sandbox.pty.create(
8984

9085
## Send input to PTY
9186

92-
Use `sendInput()` in JavaScript or `send_stdin()` in Python to send data to the terminal.
87+
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.
9388

9489
<CodeGroup>
9590
```js JavaScript & TypeScript
96-
import { Sandbox } from '@e2b/code-interpreter'
97-
98-
const sandbox = await Sandbox.create()
99-
10091
const terminal = await sandbox.pty.create({
10192
cols: 80,
10293
rows: 24,
@@ -111,65 +102,46 @@ await sandbox.pty.sendInput(
111102
```
112103

113104
```python Python
114-
from e2b_code_interpreter import AsyncSandbox
115-
from e2b.sandbox_async.commands.pty import PtySize
116-
import asyncio
117-
118-
async def main():
119-
sandbox = await AsyncSandbox.create()
120-
121-
terminal = await sandbox.pty.create(
122-
size=PtySize(cols=80, rows=24),
123-
on_data=lambda data: print(data.decode(), end=''),
124-
)
125-
126-
# Send a command (don't forget the newline!)
127-
await sandbox.pty.send_stdin(terminal.pid, b'echo "Hello from PTY"\n')
105+
terminal = sandbox.pty.create(
106+
cols=80,
107+
rows=24,
108+
on_data=lambda data: print(data.decode(), end=''),
109+
)
128110

129-
asyncio.run(main())
111+
# Send a command as bytes (b'...' is Python's byte string syntax)
112+
# Don't forget the newline!
113+
sandbox.pty.send_stdin(terminal.pid, b'echo "Hello from PTY"\n')
130114
```
131115
</CodeGroup>
132116

133117
## Resize the terminal
134118

135-
When the user's terminal window changes size, notify the PTY with `resize()`.
119+
When the user's terminal window changes size, notify the PTY with `resize()`. The `cols` and `rows` parameters are measured in characters, not pixels.
136120

137121
<CodeGroup>
138122
```js JavaScript & TypeScript
139-
import { Sandbox } from '@e2b/code-interpreter'
140-
141-
const sandbox = await Sandbox.create()
142-
143123
const terminal = await sandbox.pty.create({
144124
cols: 80,
145125
rows: 24,
146126
onData: (data) => process.stdout.write(data),
147127
})
148128

149-
// Resize to new dimensions
129+
// Resize to new dimensions (in characters)
150130
await sandbox.pty.resize(terminal.pid, {
151131
cols: 120,
152132
rows: 40,
153133
})
154134
```
155135

156136
```python Python
157-
from e2b_code_interpreter import AsyncSandbox
158-
from e2b.sandbox_async.commands.pty import PtySize
159-
import asyncio
160-
161-
async def main():
162-
sandbox = await AsyncSandbox.create()
163-
164-
terminal = await sandbox.pty.create(
165-
size=PtySize(cols=80, rows=24),
166-
on_data=lambda data: print(data.decode(), end=''),
167-
)
168-
169-
# Resize to new dimensions
170-
await sandbox.pty.resize(terminal.pid, PtySize(cols=120, rows=40))
137+
terminal = sandbox.pty.create(
138+
cols=80,
139+
rows=24,
140+
on_data=lambda data: print(data.decode(), end=''),
141+
)
171142

172-
asyncio.run(main())
143+
# Resize to new dimensions (in characters)
144+
sandbox.pty.resize(terminal.pid, cols=120, rows=40)
173145
```
174146
</CodeGroup>
175147

@@ -182,10 +154,6 @@ You can disconnect from a PTY session while keeping it running, then reconnect l
182154

183155
<CodeGroup>
184156
```js JavaScript & TypeScript
185-
import { Sandbox } from '@e2b/code-interpreter'
186-
187-
const sandbox = await Sandbox.create()
188-
189157
// Create a PTY session
190158
const terminal = await sandbox.pty.create({
191159
cols: 80,
@@ -214,41 +182,35 @@ await reconnected.wait()
214182
```
215183

216184
```python Python
217-
from e2b_code_interpreter import AsyncSandbox
218-
from e2b.sandbox_async.commands.pty import PtySize
219-
import asyncio
185+
import time
220186

221-
async def main():
222-
sandbox = await AsyncSandbox.create()
223-
224-
# Create a PTY session
225-
terminal = await sandbox.pty.create(
226-
size=PtySize(cols=80, rows=24),
227-
on_data=lambda data: print('Handler 1:', data.decode()),
228-
)
229-
230-
pid = terminal.pid
187+
# Create a PTY session
188+
terminal = sandbox.pty.create(
189+
cols=80,
190+
rows=24,
191+
on_data=lambda data: print('Handler 1:', data.decode()),
192+
)
231193

232-
# Send a command
233-
await sandbox.pty.send_stdin(pid, b'echo hello\n')
234-
await asyncio.sleep(0.5)
194+
pid = terminal.pid
235195

236-
# Disconnect - PTY keeps running in the background
237-
await terminal.disconnect()
196+
# Send a command
197+
sandbox.pty.send_stdin(pid, b'echo hello\n')
198+
time.sleep(0.5)
238199

239-
# Later: reconnect with a new data handler
240-
reconnected = await sandbox.pty.connect(
241-
pid,
242-
on_data=lambda data: print('Handler 2:', data.decode()),
243-
)
200+
# Disconnect - PTY keeps running in the background
201+
terminal.disconnect()
244202

245-
# Continue using the session
246-
await sandbox.pty.send_stdin(pid, b'echo world\n')
203+
# Later: reconnect with a new data handler
204+
reconnected = sandbox.pty.connect(
205+
pid,
206+
on_data=lambda data: print('Handler 2:', data.decode()),
207+
)
247208

248-
# Wait for the terminal to exit
249-
await reconnected.wait()
209+
# Continue using the session
210+
sandbox.pty.send_stdin(pid, b'echo world\n')
250211

251-
asyncio.run(main())
212+
# Wait for the terminal to exit
213+
reconnected.wait()
252214
```
253215
</CodeGroup>
254216

@@ -258,10 +220,6 @@ Terminate the PTY session with `kill()`.
258220

259221
<CodeGroup>
260222
```js JavaScript & TypeScript
261-
import { Sandbox } from '@e2b/code-interpreter'
262-
263-
const sandbox = await Sandbox.create()
264-
265223
const terminal = await sandbox.pty.create({
266224
cols: 80,
267225
rows: 24,
@@ -277,26 +235,18 @@ console.log('Killed:', killed) // true if successful
277235
```
278236

279237
```python Python
280-
from e2b_code_interpreter import AsyncSandbox
281-
from e2b.sandbox_async.commands.pty import PtySize
282-
import asyncio
283-
284-
async def main():
285-
sandbox = await AsyncSandbox.create()
286-
287-
terminal = await sandbox.pty.create(
288-
size=PtySize(cols=80, rows=24),
289-
on_data=lambda data: print(data.decode(), end=''),
290-
)
291-
292-
# Kill the PTY
293-
killed = await sandbox.pty.kill(terminal.pid)
294-
print('Killed:', killed) # True if successful
238+
terminal = sandbox.pty.create(
239+
cols=80,
240+
rows=24,
241+
on_data=lambda data: print(data.decode(), end=''),
242+
)
295243

296-
# Or use the handle method
297-
# await terminal.kill()
244+
# Kill the PTY
245+
killed = sandbox.pty.kill(terminal.pid)
246+
print('Killed:', killed) # True if successful
298247

299-
asyncio.run(main())
248+
# Or use the handle method
249+
# terminal.kill()
300250
```
301251
</CodeGroup>
302252

@@ -306,10 +256,6 @@ Use `wait()` to wait for the terminal session to end (e.g., when the user types
306256

307257
<CodeGroup>
308258
```js JavaScript & TypeScript
309-
import { Sandbox } from '@e2b/code-interpreter'
310-
311-
const sandbox = await Sandbox.create()
312-
313259
const terminal = await sandbox.pty.create({
314260
cols: 80,
315261
rows: 24,
@@ -325,26 +271,18 @@ console.log('Exit code:', result.exitCode)
325271
```
326272

327273
```python Python
328-
from e2b_code_interpreter import AsyncSandbox
329-
from e2b.sandbox_async.commands.pty import PtySize
330-
import asyncio
331-
332-
async def main():
333-
sandbox = await AsyncSandbox.create()
334-
335-
terminal = await sandbox.pty.create(
336-
size=PtySize(cols=80, rows=24),
337-
on_data=lambda data: print(data.decode(), end=''),
338-
)
339-
340-
# Send exit command
341-
await sandbox.pty.send_stdin(terminal.pid, b'exit\n')
274+
terminal = sandbox.pty.create(
275+
cols=80,
276+
rows=24,
277+
on_data=lambda data: print(data.decode(), end=''),
278+
)
342279

343-
# Wait for the terminal to exit
344-
result = await terminal.wait()
345-
print('Exit code:', result.exit_code)
280+
# Send exit command
281+
sandbox.pty.send_stdin(terminal.pid, b'exit\n')
346282

347-
asyncio.run(main())
283+
# Wait for the terminal to exit
284+
result = terminal.wait()
285+
print('Exit code:', result.exit_code)
348286
```
349287
</CodeGroup>
350288

0 commit comments

Comments
 (0)