Skip to content

Commit fefabf8

Browse files
committed
Add formats helper function
1 parent 974b1b0 commit fefabf8

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

js/src/messaging.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ export class Result {
133133
}
134134
}
135135
}
136+
137+
/**
138+
* Returns all the formats available for the result.
139+
*
140+
* @returns Array of strings representing the formats available for the result.
141+
*/
142+
formats(): string[] {
143+
const formats = []
144+
if (this.html) {
145+
formats.push('html')
146+
}
147+
if (this.markdown) {
148+
formats.push('markdown')
149+
}
150+
if (this.svg) {
151+
formats.push('svg')
152+
}
153+
if (this.png) {
154+
formats.push('png')
155+
}
156+
if (this.jpeg) {
157+
formats.push('jpeg')
158+
}
159+
if (this.pdf) {
160+
formats.push('pdf')
161+
}
162+
if (this.latex) {
163+
formats.push('latex')
164+
}
165+
if (this.json) {
166+
formats.push('json')
167+
}
168+
if (this.javascript) {
169+
formats.push('javascript')
170+
}
171+
172+
for (const key of Object.keys(this.extra)) {
173+
formats.push(key)
174+
}
175+
176+
return formats
177+
}
136178
}
137179

138180
/**

python/e2b_code_interpreter/models.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,36 @@ def __init__(self, is_main_result: bool, data: [MIMEType, str]):
8080
self.javascript = data.pop("application/javascript", None)
8181
self.extra = data
8282

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
90113

91114
def __str__(self) -> str:
92115
"""

python/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
with CodeInterpreter() as sandbox:
2727
result = sandbox.notebook.exec_cell(code)
2828

29-
print(result.result.keys())
29+
print(result.result.formats())
3030
print(len(result.display_data))

0 commit comments

Comments
 (0)