Skip to content

Commit

Permalink
Update python SDK docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Oct 16, 2024
1 parent 2c42a21 commit fc5c52a
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 45 deletions.
1 change: 0 additions & 1 deletion js/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export class Sandbox extends BaseSandbox {
*/
async createCodeContext(opts?: CreateCodeContextOpts): Promise<Context> {
try {

const res = await fetch(`${this.jupyterUrl}/contexts`, {
method: 'POST',
headers: {
Expand Down
121 changes: 99 additions & 22 deletions python/e2b_code_interpreter/code_interpreter_async.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import httpx

from typing import Optional, Dict, overload
from typing import Optional, Dict, overload, Union, Literal
from httpx import AsyncClient

from e2b import (
Expand Down Expand Up @@ -34,6 +34,27 @@


class AsyncSandbox(BaseAsyncSandbox):
"""
E2B cloud sandbox is a secure and isolated cloud environment.
The sandbox allows you to:
- Access Linux OS
- Create, list, and delete files and directories
- Run commands
- Run isolated code
- Access the internet
Check docs [here](https://e2b.dev/docs).
Use the `AsyncSandbox.create()` to create a new sandbox.
Example:
```python
from e2b_code_interpreter import AsyncSandbox
sandbox = await AsyncSandbox.create()
```
"""

default_template = DEFAULT_TEMPLATE

def __init__(self, sandbox_id: str, connection_config: ConnectionConfig):
Expand All @@ -51,34 +72,75 @@ def _client(self) -> AsyncClient:
async def run_code(
self,
code: str,
language: Optional[str] = None,
language: Union[Literal["python"], None] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
on_result: Optional[OutputHandler[Result]] = None,
on_error: Optional[OutputHandler[ExecutionError]] = None,
envs: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> Execution: ...
) -> Execution:
"""
Runs the code as Python.
Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
You can reference previously defined variables, imports, and functions in the code.
:param code: Code to execute
:param language: Language to use for code execution. If not defined, the default Python context is used.
:param on_stdout: Callback for stdout messages
:param on_stderr: Callback for stderr messages
:param on_result: Callback for the `Result` object
:param on_error: Callback for the `ExecutionError` object
:param envs: Custom environment variables
:param timeout: Timeout for the code execution in **seconds**
:param request_timeout: Timeout for the request in **seconds**
:return: `Execution` result object
"""
...

@overload
async def run_code(
self,
code: str,
context: Optional[Context] = None,
language: Optional[str] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
on_result: Optional[OutputHandler[Result]] = None,
on_error: Optional[OutputHandler[ExecutionError]] = None,
envs: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> Execution: ...
) -> Execution:
"""
Runs the code for the specified language.
Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
If no language is specified, Python is used.
You can reference previously defined variables, imports, and functions in the code.
:param code: Code to execute
:param language: Language to use for code execution. If not defined, the default Python context is used.
:param on_stdout: Callback for stdout messages
:param on_stderr: Callback for stderr messages
:param on_result: Callback for the `Result` object
:param on_error: Callback for the `ExecutionError` object
:param envs: Custom environment variables
:param timeout: Timeout for the code execution in **seconds**
:param request_timeout: Timeout for the request in **seconds**
:return: `Execution` result object
"""
...

@overload
async def run_code(
self,
code: str,
language: Optional[str] = None,
context: Optional[Context] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
Expand All @@ -89,21 +151,39 @@ async def run_code(
request_timeout: Optional[float] = None,
) -> Execution:
"""
Runs the code in the specified language/context, if not specified, the default context is used.
Runs the code in the specified context, if not specified, the default context is used.
Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
You can reference previously defined variables, imports, and functions in the code.
:param code: The code to execute
: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.
: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.
:param code: Code to execute
: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.
:param on_stdout: Callback for stdout messages
:param on_stderr: Callback for stderr messages
:param on_result: Callback for the `Result` object
:param on_error: Callback for the `ExecutionError` object
:param envs: Environment variables
:param timeout: Max time to wait for the execution to finish
:param request_timeout: Max time to wait for the request to finish
:return: Execution object
:param envs: Custom environment variables
:param timeout: Timeout for the code execution in **seconds**
:param request_timeout: Timeout for the request in **seconds**
:return: `Execution` result object
"""
...

async def run_code(
self,
code: str,
language: Optional[str] = None,
context: Optional[Context] = None,
on_stdout: Optional[OutputHandler[OutputMessage]] = None,
on_stderr: Optional[OutputHandler[OutputMessage]] = None,
on_result: Optional[OutputHandler[Result]] = None,
on_error: Optional[OutputHandler[ExecutionError]] = None,
envs: Optional[Dict[str, str]] = None,
timeout: Optional[float] = None,
request_timeout: Optional[float] = None,
) -> Execution:
logger.debug(f"Executing code {code}")

if context and language:
Expand Down Expand Up @@ -154,17 +234,16 @@ async def create_code_context(
self,
cwd: Optional[str] = None,
language: Optional[str] = None,
envs: Optional[Dict[str, str]] = None,
request_timeout: Optional[float] = None,
) -> Context:
"""
Creates a new context to run code in.
:param cwd: Set the current working directory for the context
:param language: Language of the context. If not specified, the default Python context is used.
:param envs: Environment variables
:param request_timeout: Max time to wait for the request to finish
:return: Context id
:param cwd: Set the current working directory for the context, defaults to `/home/user`
:param language: Language of the context. If not specified, defaults to Python
:param request_timeout: Timeout for the request in **milliseconds**
:return: Context object
"""
logger.debug(f"Creating new {language} context")

Expand All @@ -173,8 +252,6 @@ async def create_code_context(
data["language"] = language
if cwd:
data["cwd"] = cwd
if envs:
data["env_vars"] = envs

try:
response = await self._client.post(
Expand Down
Loading

0 comments on commit fc5c52a

Please sign in to comment.