forked from amrrs/code-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
270 lines (213 loc) · 7.81 KB
/
models.py
File metadata and controls
270 lines (213 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import copy
from typing import List, Optional, Iterable, Dict
from pydantic import BaseModel
class Error(BaseModel):
"""
Represents an error that occurred during the execution of a cell.
The error contains the name of the error, the value of the error, and the traceback.
"""
name: str
"Name of the exception."
value: str
"Value of the exception."
traceback_raw: List[str]
"List of strings representing the traceback."
@property
def traceback(self) -> str:
"""
Returns the traceback as a single string.
:return: The traceback as a single string.
"""
return "\n".join(self.traceback_raw)
class MIMEType(str):
"""
Represents a MIME type.
"""
class Result:
"""
Represents the data to be displayed as a result of executing a cell in a Jupyter notebook.
The result is similar to the structure returned by ipython kernel: https://ipython.readthedocs.io/en/stable/development/execution.html#execution-semantics
The result can contain multiple types of data, such as text, images, plots, etc. Each type of data is represented
as a string, and the result can contain multiple types of data. The display calls don't have to have text representation,
for the actual result the representation is always present for the result, the other representations are always optional.
The class also provides methods to display the data in a Jupyter notebook.
"""
text: Optional[str] = None
html: Optional[str] = None
markdown: Optional[str] = None
svg: Optional[str] = None
png: Optional[str] = None
jpeg: Optional[str] = None
pdf: Optional[str] = None
latex: Optional[str] = None
json: Optional[dict] = None
javascript: Optional[str] = None
extra: Optional[dict] = None
"Extra data that can be included. Not part of the standard types."
is_main_result: bool
"Whether this data is the result of the cell. Data can be produced by display calls of which can be multiple in a cell."
raw: Dict[MIMEType, str]
"Dictionary that maps MIME types to their corresponding string representations of the data."
def __init__(self, is_main_result: bool, data: [MIMEType, str]):
self.is_main_result = is_main_result
self.raw = copy.deepcopy(data)
self.text = data.pop("text/plain", None)
self.html = data.pop("text/html", None)
self.markdown = data.pop("text/markdown", None)
self.svg = data.pop("image/svg+xml", None)
self.png = data.pop("image/png", None)
self.jpeg = data.pop("image/jpeg", None)
self.pdf = data.pop("application/pdf", None)
self.latex = data.pop("text/latex", None)
self.json = data.pop("application/json", None)
self.javascript = data.pop("application/javascript", None)
self.extra = data
# Allows to iterate over formats()
def __getitem__(self, item):
if item in self.raw:
return self.raw[item]
return getattr(self, item)
def formats(self) -> Iterable[str]:
"""
Returns all available formats of the result.
:return: All available formats of the result in MIME types.
"""
formats = []
if self.html:
formats.append("html")
if self.markdown:
formats.append("markdown")
if self.svg:
formats.append("svg")
if self.png:
formats.append("png")
if self.jpeg:
formats.append("jpeg")
if self.pdf:
formats.append("pdf")
if self.latex:
formats.append("latex")
if self.json:
formats.append("json")
if self.javascript:
formats.append("javascript")
for key in self.extra:
formats.append(key)
return formats
def __str__(self) -> Optional[str]:
"""
Returns the text representation of the data.
:return: The text representation of the data.
"""
return self.text
def __repr__(self) -> str:
return f"Result({self.text})"
def _repr_html_(self) -> Optional[str]:
"""
Returns the HTML representation of the data.
:return: The HTML representation of the data.
"""
return self.html
def _repr_markdown_(self) -> Optional[str]:
"""
Returns the Markdown representation of the data.
:return: The Markdown representation of the data.
"""
return self.markdown
def _repr_svg_(self) -> Optional[str]:
"""
Returns the SVG representation of the data.
:return: The SVG representation of the data.
"""
return self.svg
def _repr_png_(self) -> Optional[str]:
"""
Returns the base64 representation of the PNG data.
:return: The base64 representation of the PNG data.
"""
return self.png
def _repr_jpeg_(self) -> Optional[str]:
"""
Returns the base64 representation of the JPEG data.
:return: The base64 representation of the JPEG data.
"""
return self.jpeg
def _repr_pdf_(self) -> Optional[str]:
"""
Returns the PDF representation of the data.
:return: The PDF representation of the data.
"""
return self.pdf
def _repr_latex_(self) -> Optional[str]:
"""
Returns the LaTeX representation of the data.
:return: The LaTeX representation of the data.
"""
return self.latex
def _repr_json_(self) -> Optional[dict]:
"""
Returns the JSON representation of the data.
:return: The JSON representation of the data.
"""
return self.json
def _repr_javascript_(self) -> Optional[str]:
"""
Returns the JavaScript representation of the data.
:return: The JavaScript representation of the data.
"""
return self.javascript
class Logs(BaseModel):
"""
Data printed to stdout and stderr during execution, usually by print statements, logs, warnings, subprocesses, etc.
"""
stdout: List[str] = []
"List of strings printed to stdout by prints, subprocesses, etc."
stderr: List[str] = []
"List of strings printed to stderr by prints, subprocesses, etc."
def serialize_results(results: List[Result]) -> List[Dict[str, str]]:
"""
Serializes the results to JSON.
This method is used by the Pydantic JSON encoder.
"""
serialized = []
for result in results:
serialized_dict = {key: result[key] for key in result.formats()}
serialized_dict["text"] = result.text
serialized.append(serialized_dict)
return serialized
class Execution(BaseModel):
"""
Represents the result of a cell execution.
"""
class Config:
arbitrary_types_allowed = True
json_encoders = {
List[Result]: serialize_results,
}
results: List[Result] = []
"List of the result of the cell (interactively interpreted last line), display calls (e.g. matplotlib plots)."
logs: Logs = Logs()
"Logs printed to stdout and stderr during execution."
error: Optional[Error] = None
"Error object if an error occurred, None otherwise."
execution_count: Optional[int] = None
"Execution count of the cell."
@property
def text(self) -> Optional[str]:
"""
Returns the text representation of the result.
:return: The text representation of the result.
"""
for d in self.results:
if d.is_main_result:
return d.text
def to_json(self) -> str:
"""
Returns the JSON representation of the Execution object.
"""
return self.model_dump_json(exclude_none=True)
class KernelException(Exception):
"""
Exception raised when a kernel operation fails.
"""
pass