e2b-code-interpreter 0.0.1a2__tar.gz → 0.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: e2b-code-interpreter
3
- Version: 0.0.1a2
3
+ Version: 0.0.2
4
4
  Summary: E2B Code Interpreter - Stateful code execution
5
5
  Home-page: https://e2b.dev/
6
6
  License: Apache-2.0
@@ -109,7 +109,7 @@ print("world")
109
109
  """
110
110
 
111
111
  with CodeInterpreter() as sandbox:
112
- sandbox.notebook.exec_cell(code, on_stdout=print, on_stderr=print, on_display_data=(lambda data: print(data.text)))
112
+ sandbox.notebook.exec_cell(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
113
113
  ```
114
114
 
115
115
  ### Pre-installed Python packages inside the sandbox
@@ -86,7 +86,7 @@ print("world")
86
86
  """
87
87
 
88
88
  with CodeInterpreter() as sandbox:
89
- sandbox.notebook.exec_cell(code, on_stdout=print, on_stderr=print, on_display_data=(lambda data: print(data.text)))
89
+ sandbox.notebook.exec_cell(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
90
90
  ```
91
91
 
92
92
  ### Pre-installed Python packages inside the sandbox
@@ -10,8 +10,7 @@ from e2b import EnvVars, ProcessMessage, Sandbox
10
10
  from e2b.constants import TIMEOUT
11
11
 
12
12
  from e2b_code_interpreter.messaging import JupyterKernelWebSocket
13
- from e2b_code_interpreter.models import KernelException, Execution
14
-
13
+ from e2b_code_interpreter.models import KernelException, Execution, Result
15
14
 
16
15
  logger = logging.getLogger(__name__)
17
16
 
@@ -66,7 +65,7 @@ class JupyterExtension:
66
65
  kernel_id: Optional[str] = None,
67
66
  on_stdout: Optional[Callable[[ProcessMessage], Any]] = None,
68
67
  on_stderr: Optional[Callable[[ProcessMessage], Any]] = None,
69
- on_display_data: Optional[Callable[[Dict[str, Any]], Any]] = None,
68
+ on_result: Optional[Callable[[Result], Any]] = None,
70
69
  timeout: Optional[float] = TIMEOUT,
71
70
  ) -> Execution:
72
71
  """
@@ -76,7 +75,7 @@ class JupyterExtension:
76
75
  :param kernel_id: The ID of the kernel to execute the code on. If not provided, the default kernel is used.
77
76
  :param on_stdout: A callback function to handle standard output messages from the code execution.
78
77
  :param on_stderr: A callback function to handle standard error messages from the code execution.
79
- :param on_display_data: A callback function to handle display data messages from the code execution.
78
+ :param on_result: A callback function to handle the result and display calls of the code execution.
80
79
  :param timeout: Timeout for the call
81
80
 
82
81
  :return: Result of the execution
@@ -93,9 +92,7 @@ class JupyterExtension:
93
92
  logger.debug(f"Creating new websocket connection to kernel {kernel_id}")
94
93
  ws = self._connect_to_kernel_ws(kernel_id, timeout=timeout)
95
94
 
96
- session_id = ws.send_execution_message(
97
- code, on_stdout, on_stderr, on_display_data
98
- )
95
+ session_id = ws.send_execution_message(code, on_stdout, on_stderr, on_result)
99
96
  logger.debug(
100
97
  f"Sent execution message to kernel {kernel_id}, session_id: {session_id}"
101
98
  )
@@ -14,7 +14,7 @@ from e2b.sandbox.websocket_client import WebSocket
14
14
  from e2b.utils.future import DeferredFuture
15
15
  from pydantic import ConfigDict, PrivateAttr, BaseModel
16
16
 
17
- from e2b_code_interpreter.models import Execution, Result, Error, MIMEType
17
+ from e2b_code_interpreter.models import Execution, Result, Error
18
18
 
19
19
  logger = logging.getLogger(__name__)
20
20
 
@@ -26,21 +26,21 @@ class CellExecution:
26
26
  """
27
27
 
28
28
  input_accepted: bool = False
29
- on_stdout: Optional[Callable[[ProcessMessage], None]] = None
30
- on_stderr: Optional[Callable[[ProcessMessage], None]] = None
31
- on_display_data: Optional[Callable[[Dict[MIMEType, str]], None]] = None
29
+ on_stdout: Optional[Callable[[ProcessMessage], Any]] = None
30
+ on_stderr: Optional[Callable[[ProcessMessage], Any]] = None
31
+ on_result: Optional[Callable[[Result], Any]] = None
32
32
 
33
33
  def __init__(
34
34
  self,
35
- on_stdout: Optional[Callable[[ProcessMessage], None]] = None,
36
- on_stderr: Optional[Callable[[ProcessMessage], None]] = None,
37
- on_display_data: Optional[Callable[[Dict[MIMEType, str]], None]] = None,
35
+ on_stdout: Optional[Callable[[ProcessMessage], Any]] = None,
36
+ on_stderr: Optional[Callable[[ProcessMessage], Any]] = None,
37
+ on_result: Optional[Callable[[Result], Any]] = None,
38
38
  ):
39
39
  self.partial_result = Execution()
40
40
  self.execution = Future()
41
41
  self.on_stdout = on_stdout
42
42
  self.on_stderr = on_stderr
43
- self.on_display_data = on_display_data
43
+ self.on_result = on_result
44
44
 
45
45
 
46
46
  class JupyterKernelWebSocket(BaseModel):
@@ -129,9 +129,9 @@ class JupyterKernelWebSocket(BaseModel):
129
129
  def send_execution_message(
130
130
  self,
131
131
  code: str,
132
- on_stdout: Optional[Callable[[ProcessMessage], None]] = None,
133
- on_stderr: Optional[Callable[[ProcessMessage], None]] = None,
134
- on_display_data: Optional[Callable[[Dict[MIMEType, str]], None]] = None,
132
+ on_stdout: Optional[Callable[[ProcessMessage], Any]] = None,
133
+ on_stderr: Optional[Callable[[ProcessMessage], Any]] = None,
134
+ on_result: Optional[Callable[[Result], Any]] = None,
135
135
  ) -> str:
136
136
  message_id = str(uuid.uuid4())
137
137
  logger.debug(f"Sending execution message: {message_id}")
@@ -139,7 +139,7 @@ class JupyterKernelWebSocket(BaseModel):
139
139
  self._cells[message_id] = CellExecution(
140
140
  on_stdout=on_stdout,
141
141
  on_stderr=on_stderr,
142
- on_display_data=on_display_data,
142
+ on_result=on_result,
143
143
  )
144
144
  request = self._get_execute_request(message_id, code)
145
145
  self._queue_in.put(request)
@@ -204,12 +204,13 @@ class JupyterKernelWebSocket(BaseModel):
204
204
  elif data["msg_type"] in "display_data":
205
205
  result = Result(is_main_result=False, data=data["content"]["data"])
206
206
  execution.results.append(result)
207
- if cell.on_display_data:
208
- cell.on_display_data(result)
207
+ if cell.on_result:
208
+ cell.on_result(result)
209
209
  elif data["msg_type"] == "execute_result":
210
- execution.results.append(
211
- Result(is_main_result=True, data=data["content"]["data"])
212
- )
210
+ result = Result(is_main_result=True, data=data["content"]["data"])
211
+ execution.results.append(result)
212
+ if cell.on_result:
213
+ cell.on_result(result)
213
214
  elif data["msg_type"] == "status":
214
215
  if data["content"]["execution_state"] == "idle":
215
216
  if cell.input_accepted:
@@ -35,7 +35,7 @@ class MIMEType(str):
35
35
  class Result:
36
36
  """
37
37
  Represents the data to be displayed as a result of executing a cell in a Jupyter notebook.
38
- This is result returned by ipython kernel: https://ipython.readthedocs.io/en/stable/development/execution.html#execution-semantics
38
+ The result is similar to the structure returned by ipython kernel: https://ipython.readthedocs.io/en/stable/development/execution.html#execution-semantics
39
39
 
40
40
  The result can contain multiple types of data, such as text, images, plots, etc. Each type of data is represented
41
41
  as a string, and the result can contain multiple types of data. The text representation is always present, and
@@ -80,13 +80,36 @@ class Result:
80
80
  self.javascript = data.pop("application/javascript", None)
81
81
  self.extra = data
82
82
 
83
- def keys(self) -> Iterable[str]:
84
- """
85
- Returns the MIME types of the data.
86
-
87
- :return: The MIME types of the data.
88
- """
89
- return self.raw.keys()
83
+ def formats(self) -> Iterable[str]:
84
+ """
85
+ Returns all available formats of the result.
86
+
87
+ :return: All available formats of the result in MIME types.
88
+ """
89
+ formats = []
90
+ if self.html:
91
+ formats.append("html")
92
+ if self.markdown:
93
+ formats.append("markdown")
94
+ if self.svg:
95
+ formats.append("svg")
96
+ if self.png:
97
+ formats.append("png")
98
+ if self.jpeg:
99
+ formats.append("jpeg")
100
+ if self.pdf:
101
+ formats.append("pdf")
102
+ if self.latex:
103
+ formats.append("latex")
104
+ if self.json:
105
+ formats.append("json")
106
+ if self.javascript:
107
+ formats.append("javascript")
108
+
109
+ for key in self.extra:
110
+ formats.append(key)
111
+
112
+ return formats
90
113
 
91
114
  def __str__(self) -> str:
92
115
  """
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "e2b-code-interpreter"
3
- version = "0.0.1a2"
3
+ version = "0.0.2"
4
4
  description = "E2B Code Interpreter - Stateful code execution"
5
5
  authors = ["e2b <hello@e2b.dev>"]
6
6
  license = "Apache-2.0"