Skip to content

Commit

Permalink
Add formats helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Apr 4, 2024
1 parent 974b1b0 commit fefabf8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
42 changes: 42 additions & 0 deletions js/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,48 @@ export class Result {
}
}
}

/**
* Returns all the formats available for the result.
*
* @returns Array of strings representing the formats available for the result.
*/
formats(): string[] {
const formats = []
if (this.html) {
formats.push('html')
}
if (this.markdown) {
formats.push('markdown')
}
if (this.svg) {
formats.push('svg')
}
if (this.png) {
formats.push('png')
}
if (this.jpeg) {
formats.push('jpeg')
}
if (this.pdf) {
formats.push('pdf')
}
if (this.latex) {
formats.push('latex')
}
if (this.json) {
formats.push('json')
}
if (this.javascript) {
formats.push('javascript')
}

for (const key of Object.keys(this.extra)) {
formats.push(key)
}

return formats
}
}

/**
Expand Down
37 changes: 30 additions & 7 deletions python/e2b_code_interpreter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,36 @@ def __init__(self, is_main_result: bool, data: [MIMEType, str]):
self.javascript = data.pop("application/javascript", None)
self.extra = data

def keys(self) -> Iterable[str]:
"""
Returns the MIME types of the data.
:return: The MIME types of the data.
"""
return self.raw.keys()
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) -> str:
"""
Expand Down
2 changes: 1 addition & 1 deletion python/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
with CodeInterpreter() as sandbox:
result = sandbox.notebook.exec_cell(code)

print(result.result.keys())
print(result.result.formats())
print(len(result.display_data))

0 comments on commit fefabf8

Please sign in to comment.