1
1
import logging
2
2
import httpx
3
3
4
- from typing import Optional , Dict , overload
4
+ from typing import Optional , Dict , overload , Union , Literal
5
5
from httpx import AsyncClient
6
6
7
7
from e2b import (
34
34
35
35
36
36
class AsyncSandbox (BaseAsyncSandbox ):
37
+ """
38
+ E2B cloud sandbox is a secure and isolated cloud environment.
39
+
40
+ The sandbox allows you to:
41
+ - Access Linux OS
42
+ - Create, list, and delete files and directories
43
+ - Run commands
44
+ - Run isolated code
45
+ - Access the internet
46
+
47
+ Check docs [here](https://e2b.dev/docs).
48
+
49
+ Use the `AsyncSandbox.create()` to create a new sandbox.
50
+
51
+ Example:
52
+ ```python
53
+ from e2b_code_interpreter import AsyncSandbox
54
+ sandbox = await AsyncSandbox.create()
55
+ ```
56
+ """
57
+
37
58
default_template = DEFAULT_TEMPLATE
38
59
39
60
def __init__ (self , sandbox_id : str , connection_config : ConnectionConfig ):
@@ -51,34 +72,75 @@ def _client(self) -> AsyncClient:
51
72
async def run_code (
52
73
self ,
53
74
code : str ,
54
- language : Optional [ str ] = None ,
75
+ language : Union [ Literal [ "python" ], None ] = None ,
55
76
on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
56
77
on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
57
78
on_result : Optional [OutputHandler [Result ]] = None ,
58
79
on_error : Optional [OutputHandler [ExecutionError ]] = None ,
59
80
envs : Optional [Dict [str , str ]] = None ,
60
81
timeout : Optional [float ] = None ,
61
82
request_timeout : Optional [float ] = None ,
62
- ) -> Execution : ...
83
+ ) -> Execution :
84
+ """
85
+ Runs the code as Python.
86
+
87
+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
88
+
89
+ You can reference previously defined variables, imports, and functions in the code.
90
+
91
+ :param code: Code to execute
92
+ :param language: Language to use for code execution. If not defined, the default Python context is used.
93
+ :param on_stdout: Callback for stdout messages
94
+ :param on_stderr: Callback for stderr messages
95
+ :param on_result: Callback for the `Result` object
96
+ :param on_error: Callback for the `ExecutionError` object
97
+ :param envs: Custom environment variables
98
+ :param timeout: Timeout for the code execution in **seconds**
99
+ :param request_timeout: Timeout for the request in **seconds**
100
+
101
+ :return: `Execution` result object
102
+ """
103
+ ...
63
104
64
105
@overload
65
106
async def run_code (
66
107
self ,
67
108
code : str ,
68
- context : Optional [Context ] = None ,
109
+ language : Optional [str ] = None ,
69
110
on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
70
111
on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
71
112
on_result : Optional [OutputHandler [Result ]] = None ,
72
113
on_error : Optional [OutputHandler [ExecutionError ]] = None ,
73
114
envs : Optional [Dict [str , str ]] = None ,
74
115
timeout : Optional [float ] = None ,
75
116
request_timeout : Optional [float ] = None ,
76
- ) -> Execution : ...
117
+ ) -> Execution :
118
+ """
119
+ Runs the code for the specified language.
120
+
121
+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
122
+ If no language is specified, Python is used.
123
+
124
+ You can reference previously defined variables, imports, and functions in the code.
77
125
126
+ :param code: Code to execute
127
+ :param language: Language to use for code execution. If not defined, the default Python context is used.
128
+ :param on_stdout: Callback for stdout messages
129
+ :param on_stderr: Callback for stderr messages
130
+ :param on_result: Callback for the `Result` object
131
+ :param on_error: Callback for the `ExecutionError` object
132
+ :param envs: Custom environment variables
133
+ :param timeout: Timeout for the code execution in **seconds**
134
+ :param request_timeout: Timeout for the request in **seconds**
135
+
136
+ :return: `Execution` result object
137
+ """
138
+ ...
139
+
140
+ @overload
78
141
async def run_code (
79
142
self ,
80
143
code : str ,
81
- language : Optional [str ] = None ,
82
144
context : Optional [Context ] = None ,
83
145
on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
84
146
on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
@@ -89,21 +151,39 @@ async def run_code(
89
151
request_timeout : Optional [float ] = None ,
90
152
) -> Execution :
91
153
"""
92
- Runs the code in the specified language/context, if not specified, the default context is used.
154
+ Runs the code in the specified context, if not specified, the default context is used.
155
+
156
+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
157
+
93
158
You can reference previously defined variables, imports, and functions in the code.
94
159
95
- :param code: The code to execute
96
- :param language Based on the value, a default context for the language is used. If not defined and no context is provided, the default Python context is used.
97
- :param context Concrete context to run the code in. If not specified, the default context for the language is used. It's mutually exclusive with the language.
160
+ :param code: Code to execute
161
+ :param context: Concrete context to run the code in. If not specified, the default context for the language is used. It's mutually exclusive with the language.
98
162
:param on_stdout: Callback for stdout messages
99
163
:param on_stderr: Callback for stderr messages
100
164
:param on_result: Callback for the `Result` object
101
165
:param on_error: Callback for the `ExecutionError` object
102
- :param envs: Environment variables
103
- :param timeout: Max time to wait for the execution to finish
104
- :param request_timeout: Max time to wait for the request to finish
105
- :return: Execution object
166
+ :param envs: Custom environment variables
167
+ :param timeout: Timeout for the code execution in **seconds**
168
+ :param request_timeout: Timeout for the request in **seconds**
169
+
170
+ :return: `Execution` result object
106
171
"""
172
+ ...
173
+
174
+ async def run_code (
175
+ self ,
176
+ code : str ,
177
+ language : Optional [str ] = None ,
178
+ context : Optional [Context ] = None ,
179
+ on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
180
+ on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
181
+ on_result : Optional [OutputHandler [Result ]] = None ,
182
+ on_error : Optional [OutputHandler [ExecutionError ]] = None ,
183
+ envs : Optional [Dict [str , str ]] = None ,
184
+ timeout : Optional [float ] = None ,
185
+ request_timeout : Optional [float ] = None ,
186
+ ) -> Execution :
107
187
logger .debug (f"Executing code { code } " )
108
188
109
189
if context and language :
@@ -154,17 +234,16 @@ async def create_code_context(
154
234
self ,
155
235
cwd : Optional [str ] = None ,
156
236
language : Optional [str ] = None ,
157
- envs : Optional [Dict [str , str ]] = None ,
158
237
request_timeout : Optional [float ] = None ,
159
238
) -> Context :
160
239
"""
161
240
Creates a new context to run code in.
162
241
163
- :param cwd: Set the current working directory for the context
164
- :param language: Language of the context. If not specified, the default Python context is used.
165
- :param envs: Environment variables
166
- :param request_timeout: Max time to wait for the request to finish
167
- :return: Context id
242
+ :param cwd: Set the current working directory for the context, defaults to `/home/user`
243
+ :param language: Language of the context. If not specified, defaults to Python
244
+ :param request_timeout: Timeout for the request in **milliseconds**
245
+
246
+ :return: Context object
168
247
"""
169
248
logger .debug (f"Creating new { language } context" )
170
249
@@ -173,8 +252,6 @@ async def create_code_context(
173
252
data ["language" ] = language
174
253
if cwd :
175
254
data ["cwd" ] = cwd
176
- if envs :
177
- data ["env_vars" ] = envs
178
255
179
256
try :
180
257
response = await self ._client .post (
0 commit comments