Skip to content

Commit 4263f41

Browse files
committed
Add docstrings
1 parent 84da653 commit 4263f41

File tree

6 files changed

+174
-1
lines changed

6 files changed

+174
-1
lines changed

js/src/codeInterpreter.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,30 @@ async function* readLines(stream: ReadableStream<Uint8Array>) {
5252
}
5353
}
5454

55+
/**
56+
* Code interpreter module for executing code in a stateful context.
57+
*/
5558
export class JupyterExtension {
5659
private static readonly execTimeoutMs = 300_000
5760
private static readonly defaultKernelID = 'default'
5861

5962
constructor(private readonly url: string, private readonly connectionConfig: ConnectionConfig) { }
6063

64+
/**
65+
* Runs the code in the specified context, if not specified, the default context is used.
66+
* You can reference previously defined variables, imports, and functions in the code.
67+
*
68+
* @param code The code to execute
69+
* @param opts Options for executing the code
70+
* @param opts.kernelID The context ID to run the code in
71+
* @param opts.onStdout Callback for handling stdout messages
72+
* @param opts.onStderr Callback for handling stderr messages
73+
* @param opts.onResult Callback for handling the final result
74+
* @param opts.envs Environment variables to set for the execution
75+
* @param opts.timeoutMs Max time to wait for the execution to finish
76+
* @param opts.requestTimeoutMs Max time to wait for the request to finish
77+
* @returns Execution object
78+
*/
6179
async execCell(
6280
code: string,
6381
opts?: {
@@ -132,6 +150,14 @@ export class JupyterExtension {
132150
}
133151
}
134152

153+
/**
154+
* Creates a new context to run code in.
155+
*
156+
* @param cwd The working directory for the context
157+
* @param kernelName The name of the context
158+
* @param requestTimeoutMs Max time to wait for the request to finish
159+
* @returns The context ID
160+
*/
135161
async createKernel({
136162
cwd,
137163
kernelName,
@@ -168,6 +194,13 @@ export class JupyterExtension {
168194
}
169195
}
170196

197+
/**
198+
* Restarts the context.
199+
* Restarting will clear all variables, imports, and other settings set during previous executions.
200+
*
201+
* @param kernelID The context ID to restart
202+
* @param requestTimeoutMs Max time to wait for the request to finish
203+
*/
171204
async restartKernel({
172205
kernelID,
173206
requestTimeoutMs,
@@ -195,6 +228,12 @@ export class JupyterExtension {
195228
}
196229
}
197230

231+
/**
232+
* Shuts down the context.
233+
*
234+
* @param kernelID The context ID to shut down
235+
* @param requestTimeoutMs Max time to wait for the request to finish
236+
*/
198237
async shutdownKernel({
199238
kernelID,
200239
requestTimeoutMs,
@@ -221,6 +260,12 @@ export class JupyterExtension {
221260
}
222261
}
223262

263+
/**
264+
* Lists all available contexts.
265+
*
266+
* @param requestTimeoutMs Max time to wait for the request to finish
267+
* @returns List of context IDs and names
268+
*/
224269
async listKernels({
225270
requestTimeoutMs,
226271
}: {
@@ -244,6 +289,9 @@ export class JupyterExtension {
244289
}
245290
}
246291

292+
/**
293+
* Code interpreter module for executing code in a stateful context.
294+
*/
247295
export class CodeInterpreter extends Sandbox {
248296
protected static override readonly defaultTemplate: string = 'code-interpreter-beta'
249297
protected static readonly jupyterPort = 49999

js/src/graphs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Graph types
3+
*/
14
export enum GraphType {
25
LINE = 'line',
36
SCATTER = 'scatter',
@@ -8,6 +11,10 @@ export enum GraphType {
811
UNKNOWN = 'unknown',
912
}
1013

14+
15+
/**
16+
* Ax scale types
17+
*/
1118
export enum ScaleType {
1219
LINEAR = "linear",
1320
DATETIME = "datetime",

js/tests/graphs/line.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ plt.show()
3939
expect(graph.x_label).toBe('Time (s)')
4040
expect(graph.y_label).toBe('Amplitude (Hz)')
4141

42+
expect(graph.x_scale).toBe('datetime')
43+
expect(graph.y_scale).toBe('linear')
44+
4245
expect(graph.x_unit).toBe('s')
4346
expect(graph.y_unit).toBe('Hz')
4447

python/e2b_code_interpreter/code_interpreter_async.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929

3030

3131
class JupyterExtension:
32+
"""
33+
Code interpreter module for executing code in a stateful context.
34+
"""
35+
3236
_exec_timeout = 300
3337

3438
@property
@@ -56,6 +60,20 @@ async def exec_cell(
5660
timeout: Optional[float] = None,
5761
request_timeout: Optional[float] = None,
5862
) -> Execution:
63+
"""
64+
Runs the code in the specified context, if not specified, the default context is used.
65+
You can reference previously defined variables, imports, and functions in the code.
66+
67+
:param code: The code to execute
68+
:param kernel_id: The context id
69+
:param on_stdout: Callback for stdout messages
70+
:param on_stderr: Callback for stderr messages
71+
:param on_result: Callback for the `Result` object
72+
:param envs: Environment variables
73+
:param timeout: Max time to wait for the execution to finish
74+
:param request_timeout: Max time to wait for the request to finish
75+
:return: Execution object
76+
"""
5977
logger.debug(f"Executing code {code}")
6078

6179
timeout = None if timeout == 0 else (timeout or self._exec_timeout)
@@ -98,9 +116,18 @@ async def create_kernel(
98116
self,
99117
cwd: Optional[str] = None,
100118
kernel_name: Optional[str] = None,
101-
request_timeout: Optional[float] = None,
102119
envs: Optional[Dict[str, str]] = None,
120+
request_timeout: Optional[float] = None,
103121
) -> str:
122+
"""
123+
Creates a new context to run code in.
124+
125+
:param cwd: Set the current working directory for the context
126+
:param kernel_name: Type of the context
127+
:param envs: Environment variables
128+
:param request_timeout: Max time to wait for the request to finish
129+
:return: Context id
130+
"""
104131
logger.debug(f"Creating new kernel {kernel_name}")
105132

106133
data = {}
@@ -132,6 +159,12 @@ async def shutdown_kernel(
132159
kernel_id: Optional[str] = None,
133160
request_timeout: Optional[float] = None,
134161
) -> None:
162+
"""
163+
Shuts down a context.
164+
165+
:param kernel_id: Context id
166+
:param request_timeout: Max time to wait for the request to finish
167+
"""
135168
kernel_id = kernel_id or DEFAULT_KERNEL_ID
136169

137170
logger.debug(f"Shutting down a kernel with id {kernel_id}")
@@ -153,6 +186,13 @@ async def restart_kernel(
153186
kernel_id: Optional[str] = None,
154187
request_timeout: Optional[float] = None,
155188
) -> None:
189+
"""
190+
Restarts the context.
191+
Restarting will clear all variables, imports, and other settings set during previous executions.
192+
193+
:param kernel_id: Context id
194+
:param request_timeout: Max time to wait for the request to finish
195+
"""
156196
kernel_id = kernel_id or DEFAULT_KERNEL_ID
157197

158198
logger.debug(f"Restarting kernel {kernel_id}")
@@ -173,6 +213,14 @@ async def list_kernels(
173213
self,
174214
request_timeout: Optional[float] = None,
175215
) -> List[Kernel]:
216+
"""
217+
Lists all available contexts.
218+
219+
:param request_timeout: Max time to wait for the request to finish
220+
:return: List of Kernel objects
221+
"""
222+
logger.debug("Listing kernels")
223+
176224
try:
177225
response = await self._client.get(
178226
f"{self._url}/contexts",
@@ -194,6 +242,9 @@ class AsyncCodeInterpreter(AsyncSandbox):
194242

195243
@property
196244
def notebook(self) -> JupyterExtension:
245+
"""
246+
Code interpreter module for executing code in a stateful context.
247+
"""
197248
return self._notebook
198249

199250
def __init__(self, sandbox_id: str, connection_config: ConnectionConfig):

python/e2b_code_interpreter/code_interpreter_sync.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828

2929

3030
class JupyterExtension:
31+
"""
32+
Code interpreter module for executing code in a stateful context.
33+
"""
34+
3135
_exec_timeout = 300
3236

3337
@property
@@ -55,6 +59,20 @@ def exec_cell(
5559
timeout: Optional[float] = None,
5660
request_timeout: Optional[float] = None,
5761
) -> Execution:
62+
"""
63+
Runs the code in the specified context, if not specified, the default context is used.
64+
You can reference previously defined variables, imports, and functions in the code.
65+
66+
:param code: The code to execute
67+
:param kernel_id: The context id
68+
:param on_stdout: Callback for stdout messages
69+
:param on_stderr: Callback for stderr messages
70+
:param on_result: Callback for the `Result` object
71+
:param envs: Environment variables
72+
:param timeout: Max time to wait for the execution to finish
73+
:param request_timeout: Max time to wait for the request to finish
74+
:return: Execution object
75+
"""
5876
logger.debug(f"Executing code {code}")
5977

6078
timeout = None if timeout == 0 else (timeout or self._exec_timeout)
@@ -96,15 +114,27 @@ def create_kernel(
96114
self,
97115
cwd: Optional[str] = None,
98116
kernel_name: Optional[str] = None,
117+
envs: Optional[Dict[str, str]] = None,
99118
request_timeout: Optional[float] = None,
100119
) -> str:
120+
"""
121+
Creates a new context to run code in.
122+
123+
:param cwd: Set the current working directory for the context
124+
:param kernel_name: Type of the context
125+
:param envs: Environment variables
126+
:param request_timeout: Max time to wait for the request to finish
127+
:return: Context id
128+
"""
101129
logger.debug(f"Creating new kernel {kernel_name}")
102130

103131
data = {}
104132
if kernel_name:
105133
data["name"] = kernel_name
106134
if cwd:
107135
data["cwd"] = cwd
136+
if envs:
137+
data["env_vars"] = envs
108138

109139
try:
110140
response = self._client.post(
@@ -127,6 +157,12 @@ def shutdown_kernel(
127157
kernel_id: Optional[str] = None,
128158
request_timeout: Optional[float] = None,
129159
) -> None:
160+
"""
161+
Shuts down a context.
162+
163+
:param kernel_id: Context id to shut down
164+
:param request_timeout: Max time to wait for the request to finish
165+
"""
130166
kernel_id = kernel_id or DEFAULT_KERNEL_ID
131167

132168
logger.debug(f"Shutting down a kernel with id {kernel_id}")
@@ -147,6 +183,13 @@ def restart_kernel(
147183
kernel_id: Optional[str] = None,
148184
request_timeout: Optional[float] = None,
149185
) -> None:
186+
"""
187+
Restarts the context.
188+
Restarting will clear all variables, imports, and other settings set during previous executions.
189+
190+
:param kernel_id: Context id
191+
:param request_timeout: Max time to wait for the request to finish
192+
"""
150193
kernel_id = kernel_id or DEFAULT_KERNEL_ID
151194

152195
logger.debug(f"Restarting kernel {kernel_id}")
@@ -167,6 +210,12 @@ def list_kernels(
167210
self,
168211
request_timeout: Optional[float] = None,
169212
) -> List[Kernel]:
213+
"""
214+
Lists all available contexts.
215+
216+
:param request_timeout: Max time to wait for the request to finish
217+
:return: List of Kernel objects
218+
"""
170219
logger.debug("Listing kernels")
171220

172221
try:
@@ -190,6 +239,9 @@ class CodeInterpreter(Sandbox):
190239

191240
@property
192241
def notebook(self) -> JupyterExtension:
242+
"""
243+
Code interpreter module for executing code in a stateful context.
244+
"""
193245
return self._notebook
194246

195247
def __init__(

python/e2b_code_interpreter/graphs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33

44

55
class GraphType(str, enum.Enum):
6+
"""
7+
Graph types
8+
"""
9+
610
LINE = "line"
711
SCATTER = "scatter"
812
BAR = "bar"
@@ -13,6 +17,10 @@ class GraphType(str, enum.Enum):
1317

1418

1519
class ScaleType(str, enum.Enum):
20+
"""
21+
Ax scale types
22+
"""
23+
1624
LINEAR = "linear"
1725
DATETIME = "datetime"
1826
CATEGORICAL = "categorical"
@@ -26,6 +34,10 @@ class ScaleType(str, enum.Enum):
2634

2735

2836
class Graph:
37+
"""
38+
Extracted data from a graph. It's useful for building an interactive graphs or custom visualizations.
39+
"""
40+
2941
type: GraphType
3042
title: str
3143

0 commit comments

Comments
 (0)