e2b-code-interpreter 0.0.11b46__tar.gz → 1.0.0__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.
@@ -0,0 +1,150 @@
1
+ Metadata-Version: 2.1
2
+ Name: e2b-code-interpreter
3
+ Version: 1.0.0
4
+ Summary: E2B Code Interpreter - Stateful code execution
5
+ Home-page: https://e2b.dev/
6
+ License: Apache-2.0
7
+ Author: e2b
8
+ Author-email: hello@e2b.dev
9
+ Requires-Python: >=3.8,<4.0
10
+ Classifier: License :: OSI Approved :: Apache Software License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Dist: attrs (>=21.3.0)
18
+ Requires-Dist: e2b (>=1.0.0,<2.0.0)
19
+ Requires-Dist: httpx (>=0.20.0,<0.28.0)
20
+ Project-URL: Bug Tracker, https://github.com/e2b-dev/code-interpreter/issues
21
+ Project-URL: Repository, https://github.com/e2b-dev/e2b-code-interpreter/tree/python
22
+ Description-Content-Type: text/markdown
23
+
24
+ <p align="center">
25
+ <img width="100" src="/readme-assets/logo-circle.png" alt="e2b logo">
26
+ </p>
27
+
28
+
29
+ <h1 align="center">
30
+ Code interpreter extension for Python
31
+ </h1>
32
+
33
+ <h4 align="center">
34
+ <a href="https://pypi.org/project/e2b/">
35
+ <img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
36
+ style="color:transparent;width:auto;height:100%" src="https://img.shields.io/pypi/dm/e2b?label=PyPI%20Downloads">
37
+ </a>
38
+ <a href="https://www.npmjs.com/package/e2b">
39
+ <img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
40
+ style="color:transparent;width:auto;height:100%" src="https://img.shields.io/npm/dm/e2b?label=NPM%20Downloads">
41
+ </a>
42
+ </h4>
43
+
44
+ The repository contains a template and modules for the code interpreter sandbox. It is based on the Jupyter server and implements the Jupyter Kernel messaging protocol. This allows for sharing context between code executions and improves support for plotting charts and other display-able data.
45
+
46
+ ## Key Features
47
+
48
+ - **Stateful Execution**: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.
49
+ - **Displaying Charts & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.
50
+
51
+ ## Installation
52
+
53
+ ```sh
54
+ pip install e2b-code-interpreter
55
+ ```
56
+
57
+ ## Examples
58
+
59
+ ### Minimal example with the sharing context
60
+
61
+ ```python
62
+ from e2b_code_interpreter import Sandbox
63
+
64
+ with Sandbox() as sandbox:
65
+ sandbox.run_code()("x = 1")
66
+
67
+ execution = sandbox.run_code()("x+=1; x")
68
+ print(execution.text) # outputs 2
69
+ ```
70
+
71
+ ### Get charts and any display-able data
72
+
73
+ ```python
74
+ import base64
75
+ import io
76
+
77
+ from matplotlib import image as mpimg, pyplot as plt
78
+
79
+ from e2b_code_interpreter import Sandbox
80
+
81
+ code = """
82
+ import matplotlib.pyplot as plt
83
+ import numpy as np
84
+
85
+ x = np.linspace(0, 20, 100)
86
+ y = np.sin(x)
87
+
88
+ plt.plot(x, y)
89
+ plt.show()
90
+ """
91
+
92
+ with Sandbox() as sandbox:
93
+ # you can install dependencies in "jupyter notebook style"
94
+ sandbox.run_code("!pip install matplotlib")
95
+
96
+ # plot random chart
97
+ execution = sandbox.run_code(code)
98
+
99
+ # there's your image
100
+ image = execution.results[0].png
101
+
102
+ # example how to show the image / prove it works
103
+ i = base64.b64decode(image)
104
+ i = io.BytesIO(i)
105
+ i = mpimg.imread(i, format='PNG')
106
+
107
+ plt.imshow(i, interpolation='nearest')
108
+ plt.show()
109
+ ```
110
+
111
+ ### Streaming code output
112
+
113
+ ```python
114
+ from e2b_code_interpreter import Sandbox
115
+
116
+ code = """
117
+ import time
118
+ import pandas as pd
119
+
120
+ print("hello")
121
+ time.sleep(3)
122
+ data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
123
+ display(data.head(10))
124
+ time.sleep(3)
125
+ print("world")
126
+ """
127
+
128
+ with Sandbox() as sandbox:
129
+ sandbox.run_code(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
130
+ ```
131
+
132
+ ### More resources
133
+ - Check out the [JavaScript/TypeScript](https://e2b.dev/docs/hello-world/js) and [Python](https://e2b.dev/docs/hello-world/py) "Hello World" guides to learn how to use our SDK.
134
+
135
+ - See [E2B documentation](https://e2b.dev/docs) to get started.
136
+
137
+ - Visit our [Cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main) to get inspired by examples with different LLMs and AI frameworks.
138
+
139
+ ___
140
+
141
+ <div align='center'>
142
+ <!-- <a href="https://e2b.dev/docs" target="_blank">
143
+ <img src="https://img.shields.io/badge/docs-%2300acee.svg?color=143D52&style=for-the-badge&logo=x&logoColor=white" alt=docs style="margin-bottom: 5px;"/></a> -->
144
+ <a href="https://twitter.com/e2b_dev" target="_blank">
145
+ <img src="https://img.shields.io/badge/x (twitter)-%2300acee.svg?color=000000&style=for-the-badge&logo=x&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
146
+ <a href="https://discord.com/invite/U7KEcGErtQ" target="_blank">
147
+ <img src="https://img.shields.io/badge/discord -%2300acee.svg?color=143D52&style=for-the-badge&logo=discord&logoColor=white" alt=discord style="margin-bottom: 5px;"/></a>
148
+ <a href="https://www.linkedin.com/company/e2b-dev/" target="_blank">
149
+ <img src="https://img.shields.io/badge/linkedin-%2300acee.svg?color=000000&style=for-the-badge&logo=linkedin&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
150
+ </div align='center'>
@@ -0,0 +1,127 @@
1
+ <p align="center">
2
+ <img width="100" src="/readme-assets/logo-circle.png" alt="e2b logo">
3
+ </p>
4
+
5
+
6
+ <h1 align="center">
7
+ Code interpreter extension for Python
8
+ </h1>
9
+
10
+ <h4 align="center">
11
+ <a href="https://pypi.org/project/e2b/">
12
+ <img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
13
+ style="color:transparent;width:auto;height:100%" src="https://img.shields.io/pypi/dm/e2b?label=PyPI%20Downloads">
14
+ </a>
15
+ <a href="https://www.npmjs.com/package/e2b">
16
+ <img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
17
+ style="color:transparent;width:auto;height:100%" src="https://img.shields.io/npm/dm/e2b?label=NPM%20Downloads">
18
+ </a>
19
+ </h4>
20
+
21
+ The repository contains a template and modules for the code interpreter sandbox. It is based on the Jupyter server and implements the Jupyter Kernel messaging protocol. This allows for sharing context between code executions and improves support for plotting charts and other display-able data.
22
+
23
+ ## Key Features
24
+
25
+ - **Stateful Execution**: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.
26
+ - **Displaying Charts & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.
27
+
28
+ ## Installation
29
+
30
+ ```sh
31
+ pip install e2b-code-interpreter
32
+ ```
33
+
34
+ ## Examples
35
+
36
+ ### Minimal example with the sharing context
37
+
38
+ ```python
39
+ from e2b_code_interpreter import Sandbox
40
+
41
+ with Sandbox() as sandbox:
42
+ sandbox.run_code()("x = 1")
43
+
44
+ execution = sandbox.run_code()("x+=1; x")
45
+ print(execution.text) # outputs 2
46
+ ```
47
+
48
+ ### Get charts and any display-able data
49
+
50
+ ```python
51
+ import base64
52
+ import io
53
+
54
+ from matplotlib import image as mpimg, pyplot as plt
55
+
56
+ from e2b_code_interpreter import Sandbox
57
+
58
+ code = """
59
+ import matplotlib.pyplot as plt
60
+ import numpy as np
61
+
62
+ x = np.linspace(0, 20, 100)
63
+ y = np.sin(x)
64
+
65
+ plt.plot(x, y)
66
+ plt.show()
67
+ """
68
+
69
+ with Sandbox() as sandbox:
70
+ # you can install dependencies in "jupyter notebook style"
71
+ sandbox.run_code("!pip install matplotlib")
72
+
73
+ # plot random chart
74
+ execution = sandbox.run_code(code)
75
+
76
+ # there's your image
77
+ image = execution.results[0].png
78
+
79
+ # example how to show the image / prove it works
80
+ i = base64.b64decode(image)
81
+ i = io.BytesIO(i)
82
+ i = mpimg.imread(i, format='PNG')
83
+
84
+ plt.imshow(i, interpolation='nearest')
85
+ plt.show()
86
+ ```
87
+
88
+ ### Streaming code output
89
+
90
+ ```python
91
+ from e2b_code_interpreter import Sandbox
92
+
93
+ code = """
94
+ import time
95
+ import pandas as pd
96
+
97
+ print("hello")
98
+ time.sleep(3)
99
+ data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
100
+ display(data.head(10))
101
+ time.sleep(3)
102
+ print("world")
103
+ """
104
+
105
+ with Sandbox() as sandbox:
106
+ sandbox.run_code(code, on_stdout=print, on_stderr=print, on_result=(lambda result: print(result.text)))
107
+ ```
108
+
109
+ ### More resources
110
+ - Check out the [JavaScript/TypeScript](https://e2b.dev/docs/hello-world/js) and [Python](https://e2b.dev/docs/hello-world/py) "Hello World" guides to learn how to use our SDK.
111
+
112
+ - See [E2B documentation](https://e2b.dev/docs) to get started.
113
+
114
+ - Visit our [Cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main) to get inspired by examples with different LLMs and AI frameworks.
115
+
116
+ ___
117
+
118
+ <div align='center'>
119
+ <!-- <a href="https://e2b.dev/docs" target="_blank">
120
+ <img src="https://img.shields.io/badge/docs-%2300acee.svg?color=143D52&style=for-the-badge&logo=x&logoColor=white" alt=docs style="margin-bottom: 5px;"/></a> -->
121
+ <a href="https://twitter.com/e2b_dev" target="_blank">
122
+ <img src="https://img.shields.io/badge/x (twitter)-%2300acee.svg?color=000000&style=for-the-badge&logo=x&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
123
+ <a href="https://discord.com/invite/U7KEcGErtQ" target="_blank">
124
+ <img src="https://img.shields.io/badge/discord -%2300acee.svg?color=143D52&style=for-the-badge&logo=discord&logoColor=white" alt=discord style="margin-bottom: 5px;"/></a>
125
+ <a href="https://www.linkedin.com/company/e2b-dev/" target="_blank">
126
+ <img src="https://img.shields.io/badge/linkedin-%2300acee.svg?color=000000&style=for-the-badge&logo=linkedin&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
127
+ </div align='center'>
@@ -2,6 +2,7 @@ from e2b import *
2
2
  from .code_interpreter_sync import Sandbox
3
3
  from .code_interpreter_async import AsyncSandbox
4
4
  from .models import (
5
+ Context,
5
6
  Execution,
6
7
  ExecutionError,
7
8
  Result,
@@ -2,9 +2,9 @@ import enum
2
2
  from typing import List, Tuple, Any, Optional, Union
3
3
 
4
4
 
5
- class GraphType(str, enum.Enum):
5
+ class ChartType(str, enum.Enum):
6
6
  """
7
- Graph types
7
+ Chart types
8
8
  """
9
9
 
10
10
  LINE = "line"
@@ -12,7 +12,7 @@ class GraphType(str, enum.Enum):
12
12
  BAR = "bar"
13
13
  PIE = "pie"
14
14
  BOX_AND_WHISKER = "box_and_whisker"
15
- SUPERGRAPH = "supergraph"
15
+ SUPERCHART = "superchart"
16
16
  UNKNOWN = "unknown"
17
17
 
18
18
 
@@ -33,23 +33,23 @@ class ScaleType(str, enum.Enum):
33
33
  UNKNOWN = "unknown"
34
34
 
35
35
 
36
- class Graph:
36
+ class Chart:
37
37
  """
38
- Extracted data from a graph. It's useful for building an interactive graphs or custom visualizations.
38
+ Extracted data from a chart. It's useful for building an interactive charts or custom visualizations.
39
39
  """
40
40
 
41
- type: GraphType
41
+ type: ChartType
42
42
  title: str
43
43
 
44
44
  elements: List[Any]
45
45
 
46
46
  def __init__(self, **kwargs):
47
- self.type = GraphType(kwargs["type"] or GraphType.UNKNOWN)
47
+ self.type = ChartType(kwargs["type"] or ChartType.UNKNOWN)
48
48
  self.title = kwargs["title"]
49
49
  self.elements = kwargs["elements"]
50
50
 
51
51
 
52
- class Graph2D(Graph):
52
+ class Chart2D(Chart):
53
53
  x_label: Optional[str]
54
54
  y_label: Optional[str]
55
55
  x_unit: Optional[str]
@@ -72,7 +72,7 @@ class PointData:
72
72
  self.points = [(x, y) for x, y in kwargs["points"]]
73
73
 
74
74
 
75
- class PointGraph(Graph2D):
75
+ class PointChart(Chart2D):
76
76
  x_ticks: List[Union[str, float]]
77
77
  x_tick_labels: List[str]
78
78
  x_scale: ScaleType
@@ -108,12 +108,12 @@ class PointGraph(Graph2D):
108
108
  self.elements = [PointData(**d) for d in kwargs["elements"]]
109
109
 
110
110
 
111
- class LineGraph(PointGraph):
112
- type = GraphType.LINE
111
+ class LineChart(PointChart):
112
+ type = ChartType.LINE
113
113
 
114
114
 
115
- class ScatterGraph(PointGraph):
116
- type = GraphType.SCATTER
115
+ class ScatterChart(PointChart):
116
+ type = ChartType.SCATTER
117
117
 
118
118
 
119
119
  class BarData:
@@ -127,8 +127,8 @@ class BarData:
127
127
  self.group = kwargs["group"]
128
128
 
129
129
 
130
- class BarGraph(Graph2D):
131
- type = GraphType.BAR
130
+ class BarChart(Chart2D):
131
+ type = ChartType.BAR
132
132
 
133
133
  elements: List[BarData]
134
134
 
@@ -148,8 +148,8 @@ class PieData:
148
148
  self.radius = kwargs["radius"]
149
149
 
150
150
 
151
- class PieGraph(Graph):
152
- type = GraphType.PIE
151
+ class PieChart(Chart):
152
+ type = ChartType.PIE
153
153
 
154
154
  elements: List[PieData]
155
155
 
@@ -175,8 +175,8 @@ class BoxAndWhiskerData:
175
175
  self.max = kwargs["max"]
176
176
 
177
177
 
178
- class BoxAndWhiskerGraph(Graph2D):
179
- type = GraphType.BOX_AND_WHISKER
178
+ class BoxAndWhiskerChart(Chart2D):
179
+ type = ChartType.BOX_AND_WHISKER
180
180
 
181
181
  elements: List[BoxAndWhiskerData]
182
182
 
@@ -185,40 +185,40 @@ class BoxAndWhiskerGraph(Graph2D):
185
185
  self.elements = [BoxAndWhiskerData(**d) for d in kwargs["elements"]]
186
186
 
187
187
 
188
- class SuperGraph(Graph):
189
- type = GraphType.SUPERGRAPH
188
+ class SuperChart(Chart):
189
+ type = ChartType.SUPERCHART
190
190
 
191
191
  elements: List[
192
- Union[LineGraph, ScatterGraph, BarGraph, PieGraph, BoxAndWhiskerGraph]
192
+ Union[LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart]
193
193
  ]
194
194
 
195
195
  def __init__(self, **kwargs):
196
196
  super().__init__(**kwargs)
197
- self.elements = [deserialize_graph(g) for g in kwargs["elements"]]
197
+ self.elements = [_deserialize_chart(g) for g in kwargs["elements"]]
198
198
 
199
199
 
200
- GraphTypes = Union[
201
- LineGraph, ScatterGraph, BarGraph, PieGraph, BoxAndWhiskerGraph, SuperGraph
200
+ ChartTypes = Union[
201
+ LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart, SuperChart
202
202
  ]
203
203
 
204
204
 
205
- def deserialize_graph(data: Optional[dict]) -> Optional[GraphTypes]:
205
+ def _deserialize_chart(data: Optional[dict]) -> Optional[ChartTypes]:
206
206
  if not data:
207
207
  return None
208
208
 
209
- if data["type"] == GraphType.LINE:
210
- graph = LineGraph(**data)
211
- elif data["type"] == GraphType.SCATTER:
212
- graph = ScatterGraph(**data)
213
- elif data["type"] == GraphType.BAR:
214
- graph = BarGraph(**data)
215
- elif data["type"] == GraphType.PIE:
216
- graph = PieGraph(**data)
217
- elif data["type"] == GraphType.BOX_AND_WHISKER:
218
- graph = BoxAndWhiskerGraph(**data)
219
- elif data["type"] == GraphType.SUPERGRAPH:
220
- graph = SuperGraph(**data)
209
+ if data["type"] == ChartType.LINE:
210
+ chart = LineChart(**data)
211
+ elif data["type"] == ChartType.SCATTER:
212
+ chart = ScatterChart(**data)
213
+ elif data["type"] == ChartType.BAR:
214
+ chart = BarChart(**data)
215
+ elif data["type"] == ChartType.PIE:
216
+ chart = PieChart(**data)
217
+ elif data["type"] == ChartType.BOX_AND_WHISKER:
218
+ chart = BoxAndWhiskerChart(**data)
219
+ elif data["type"] == ChartType.SUPERCHART:
220
+ chart = SuperChart(**data)
221
221
  else:
222
- graph = Graph(**data)
222
+ chart = Chart(**data)
223
223
 
224
- return graph
224
+ return chart