boxfab-code-interpreter 0.1.0a1__py3-none-any.whl

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,15 @@
1
+ from boxfab import *
2
+ from .client import BoxFab, BoxFabClientParams, E2B, E2BClientParams
3
+ from .code_interpreter_sync import Sandbox
4
+ from .code_interpreter_async import AsyncSandbox
5
+ from .models import (
6
+ Context,
7
+ Execution,
8
+ ExecutionError,
9
+ Result,
10
+ MIMEType,
11
+ Logs,
12
+ OutputHandler,
13
+ OutputMessage,
14
+ RunCodeLanguage,
15
+ )
@@ -0,0 +1,232 @@
1
+ import enum
2
+ from typing import Any, List, Tuple, Optional, Union
3
+
4
+
5
+ class ChartType(str, enum.Enum):
6
+ """
7
+ Chart types
8
+ """
9
+
10
+ LINE = "line"
11
+ SCATTER = "scatter"
12
+ BAR = "bar"
13
+ PIE = "pie"
14
+ BOX_AND_WHISKER = "box_and_whisker"
15
+ SUPERCHART = "superchart"
16
+ UNKNOWN = "unknown"
17
+
18
+
19
+ class ScaleType(str, enum.Enum):
20
+ """
21
+ Ax scale types
22
+ """
23
+
24
+ LINEAR = "linear"
25
+ DATETIME = "datetime"
26
+ CATEGORICAL = "categorical"
27
+ LOG = "log"
28
+ SYMLOG = "symlog"
29
+ LOGIT = "logit"
30
+ FUNCTION = "function"
31
+ FUNCTIONLOG = "functionlog"
32
+ ASINH = "asinh"
33
+ UNKNOWN = "unknown"
34
+
35
+
36
+ class Chart:
37
+ """
38
+ Extracted data from a chart. It's useful for building an interactive charts or custom visualizations.
39
+ """
40
+
41
+ type: ChartType
42
+ title: str
43
+
44
+ elements: List[Any]
45
+
46
+ def __init__(self, **kwargs):
47
+ self._raw_data = kwargs
48
+ self.type = ChartType(kwargs["type"] or ChartType.UNKNOWN)
49
+ self.title = kwargs["title"]
50
+ self.elements = kwargs["elements"]
51
+
52
+ def to_dict(self) -> dict:
53
+ return self._raw_data
54
+
55
+
56
+ class Chart2D(Chart):
57
+ x_label: Optional[str]
58
+ y_label: Optional[str]
59
+ x_unit: Optional[str]
60
+ y_unit: Optional[str]
61
+
62
+ def __init__(self, **kwargs):
63
+ super().__init__(**kwargs)
64
+ self.x_label = kwargs["x_label"]
65
+ self.y_label = kwargs["y_label"]
66
+ self.x_unit = kwargs["x_unit"]
67
+ self.y_unit = kwargs["y_unit"]
68
+
69
+
70
+ class PointData:
71
+ label: str
72
+ points: List[Tuple[Union[str, float], Union[str, float]]]
73
+
74
+ def __init__(self, **kwargs):
75
+ self.label = kwargs["label"]
76
+ self.points = [(x, y) for x, y in kwargs["points"]]
77
+
78
+
79
+ class PointChart(Chart2D):
80
+ x_ticks: List[Union[str, float]]
81
+ x_tick_labels: List[str]
82
+ x_scale: ScaleType
83
+
84
+ y_ticks: List[Union[str, float]]
85
+ y_tick_labels: List[str]
86
+ y_scale: ScaleType
87
+
88
+ elements: List[PointData]
89
+
90
+ def __init__(self, **kwargs):
91
+ super().__init__(**kwargs)
92
+ self.x_label = kwargs["x_label"]
93
+
94
+ try:
95
+ self.x_scale = ScaleType(kwargs.get("x_scale"))
96
+ except ValueError:
97
+ self.x_scale = ScaleType.UNKNOWN
98
+
99
+ self.x_ticks = kwargs["x_ticks"]
100
+ self.x_tick_labels = kwargs["x_tick_labels"]
101
+
102
+ self.y_label = kwargs["y_label"]
103
+
104
+ try:
105
+ self.y_scale = ScaleType(kwargs.get("y_scale"))
106
+ except ValueError:
107
+ self.y_scale = ScaleType.UNKNOWN
108
+
109
+ self.y_ticks = kwargs["y_ticks"]
110
+ self.y_tick_labels = kwargs["y_tick_labels"]
111
+
112
+ self.elements = [PointData(**d) for d in kwargs["elements"]]
113
+
114
+
115
+ class LineChart(PointChart):
116
+ type = ChartType.LINE
117
+
118
+
119
+ class ScatterChart(PointChart):
120
+ type = ChartType.SCATTER
121
+
122
+
123
+ class BarData:
124
+ label: str
125
+ group: str
126
+ value: str
127
+
128
+ def __init__(self, **kwargs):
129
+ self.label = kwargs["label"]
130
+ self.value = kwargs["value"]
131
+ self.group = kwargs["group"]
132
+
133
+
134
+ class BarChart(Chart2D):
135
+ type = ChartType.BAR
136
+
137
+ elements: List[BarData]
138
+
139
+ def __init__(self, **kwargs):
140
+ super().__init__(**kwargs)
141
+ self.elements = [BarData(**d) for d in kwargs["elements"]]
142
+
143
+
144
+ class PieData:
145
+ label: str
146
+ angle: float
147
+ radius: float
148
+
149
+ def __init__(self, **kwargs):
150
+ self.label = kwargs["label"]
151
+ self.angle = kwargs["angle"]
152
+ self.radius = kwargs["radius"]
153
+
154
+
155
+ class PieChart(Chart):
156
+ type = ChartType.PIE
157
+
158
+ elements: List[PieData]
159
+
160
+ def __init__(self, **kwargs):
161
+ super().__init__(**kwargs)
162
+ self.elements = [PieData(**d) for d in kwargs["elements"]]
163
+
164
+
165
+ class BoxAndWhiskerData:
166
+ label: str
167
+ min: float
168
+ first_quartile: float
169
+ median: float
170
+ third_quartile: float
171
+ max: float
172
+ outliers: List[float]
173
+
174
+ def __init__(self, **kwargs):
175
+ self.label = kwargs["label"]
176
+ self.min = kwargs["min"]
177
+ self.first_quartile = kwargs["first_quartile"]
178
+ self.median = kwargs["median"]
179
+ self.third_quartile = kwargs["third_quartile"]
180
+ self.max = kwargs["max"]
181
+ self.outliers = kwargs.get("outliers") or []
182
+
183
+
184
+ class BoxAndWhiskerChart(Chart2D):
185
+ type = ChartType.BOX_AND_WHISKER
186
+
187
+ elements: List[BoxAndWhiskerData]
188
+
189
+ def __init__(self, **kwargs):
190
+ super().__init__(**kwargs)
191
+ self.elements = [BoxAndWhiskerData(**d) for d in kwargs["elements"]]
192
+
193
+
194
+ class SuperChart(Chart):
195
+ type = ChartType.SUPERCHART
196
+
197
+ elements: List[Chart]
198
+
199
+ def __init__(self, **kwargs):
200
+ super().__init__(**kwargs)
201
+ self.elements = []
202
+ for raw_chart in kwargs["elements"]:
203
+ chart = _deserialize_chart(raw_chart)
204
+ if chart is not None:
205
+ self.elements.append(chart)
206
+
207
+
208
+ ChartTypes = Union[
209
+ Chart, LineChart, ScatterChart, BarChart, PieChart, BoxAndWhiskerChart, SuperChart
210
+ ]
211
+
212
+
213
+ def _deserialize_chart(data: Optional[dict]) -> Optional[ChartTypes]:
214
+ if not data:
215
+ return None
216
+
217
+ if data["type"] == ChartType.LINE:
218
+ chart = LineChart(**data)
219
+ elif data["type"] == ChartType.SCATTER:
220
+ chart = ScatterChart(**data)
221
+ elif data["type"] == ChartType.BAR:
222
+ chart = BarChart(**data)
223
+ elif data["type"] == ChartType.PIE:
224
+ chart = PieChart(**data)
225
+ elif data["type"] == ChartType.BOX_AND_WHISKER:
226
+ chart = BoxAndWhiskerChart(**data)
227
+ elif data["type"] == ChartType.SUPERCHART:
228
+ chart = SuperChart(**data)
229
+ else:
230
+ chart = Chart(**data)
231
+
232
+ return chart
@@ -0,0 +1,95 @@
1
+ from typing import Dict, Type, TypeVar, Unpack, cast
2
+
3
+ from boxfab import ApiParams, BoxFabClientParams, E2BClientParams
4
+ from boxfab import BoxFab as CoreBoxFab
5
+ from boxfab_code_interpreter.code_interpreter_async import AsyncSandbox
6
+ from boxfab_code_interpreter.code_interpreter_sync import Sandbox
7
+
8
+ T = TypeVar("T")
9
+
10
+ __all__ = ["BoxFab", "BoxFabClientParams", "E2B", "E2BClientParams"]
11
+
12
+
13
+ def _bind(cls: Type[T], api_params: ApiParams) -> Type[T]:
14
+ """Generate a subclass of ``cls`` carrying ``api_params`` as its bound params."""
15
+ return cast(
16
+ Type[T],
17
+ type(cls.__name__, (cls,), {"_bound_api_params": api_params}),
18
+ )
19
+
20
+
21
+ class BoxFab:
22
+ """
23
+ BoxFab client with an explicitly bound connection configuration.
24
+
25
+ The resource classes exposed by the client (`Sandbox`, `AsyncSandbox`,
26
+ `Volume`, `AsyncVolume`, `Template`, `AsyncTemplate`, `Secret`,
27
+ `AsyncSecret`) behave exactly like the top-level exports of the same name,
28
+ except the params passed to the client are used as the defaults instead of
29
+ the environment variables.
30
+ Per-call params still take precedence over the client's params.
31
+
32
+ Multiple clients are fully isolated from each other and from the top-level
33
+ env-configured exports.
34
+
35
+ Example:
36
+ ```python
37
+ from boxfab_code_interpreter import BoxFab
38
+
39
+ client = BoxFab(api_key="e2b_...", domain="boxfab.io")
40
+
41
+ sandbox = client.Sandbox.create()
42
+ execution = sandbox.run_code("x = 1; x += 1; x")
43
+ ```
44
+ """
45
+
46
+ def __init__(self, **opts: Unpack[BoxFabClientParams]):
47
+ """
48
+ Create a new client with the API params bound to it.
49
+
50
+ :param opts: API params used as the defaults for every call made
51
+ through this client's resource classes.
52
+ """
53
+ # Params are copied so later mutations of the caller's dicts cannot
54
+ # change the bound configuration.
55
+ api_params = cast(ApiParams, dict(cast(Dict[str, object], opts)))
56
+
57
+ headers = api_params.get("headers")
58
+ if headers is not None:
59
+ api_params["headers"] = dict(headers)
60
+
61
+ api_headers = api_params.get("api_headers")
62
+ if api_headers is not None:
63
+ api_params["api_headers"] = dict(api_headers)
64
+
65
+ self.Sandbox = _bind(Sandbox, api_params)
66
+ """Code Interpreter `Sandbox` class bound to this client's connection configuration."""
67
+
68
+ self.AsyncSandbox = _bind(AsyncSandbox, api_params)
69
+ """Code Interpreter `AsyncSandbox` class bound to this client's connection configuration."""
70
+
71
+ # The resources that are not specific to the Code Interpreter are bound
72
+ # by the core client.
73
+ core = CoreBoxFab(**api_params)
74
+
75
+ self.Volume = core.Volume
76
+ """`Volume` class bound to this client's connection configuration."""
77
+
78
+ self.AsyncVolume = core.AsyncVolume
79
+ """`AsyncVolume` class bound to this client's connection configuration."""
80
+
81
+ self.Template = core.Template
82
+ """`Template` class bound to this client's connection configuration."""
83
+
84
+ self.AsyncTemplate = core.AsyncTemplate
85
+ """`AsyncTemplate` class bound to this client's connection configuration."""
86
+
87
+ self.Secret = core.Secret
88
+ """`Secret` class bound to this client's connection configuration."""
89
+
90
+ self.AsyncSecret = core.AsyncSecret
91
+ """`AsyncSecret` class bound to this client's connection configuration."""
92
+
93
+
94
+ # Upstream name, kept so code written for the upstream package ports by changing only the import.
95
+ E2B = BoxFab