Skip to content

Commit

Permalink
Return json on dict
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Nov 13, 2024
1 parent c770965 commit c63f49c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/pretty-deers-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-template': patch
---

Return json on dict
16 changes: 15 additions & 1 deletion template/startup_scripts/0002_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pandas
from matplotlib.pyplot import Figure
import IPython
from IPython.core.formatters import BaseFormatter
from IPython.core.formatters import BaseFormatter, JSONFormatter
from traitlets.traitlets import Unicode, ObjectName

from e2b_charts import chart_figure_to_dict
Expand Down Expand Up @@ -48,10 +48,24 @@ def __call__(self, obj):
return super().__call__(obj)


class E2BDictFormatter(JSONFormatter):
def __call__(self, obj):
# Figure object is for some reason removed on execution of the cell,
# so it can't be used in type_printers or with top-level import

if isinstance(obj, dict):
return obj, {"expanded": True}
return super().__call__(obj)


ip = IPython.get_ipython()
ip.display_formatter.formatters["e2b/data"] = E2BDataFormatter(
parent=ip.display_formatter
)
ip.display_formatter.formatters["e2b/chart"] = E2BChartFormatter(
parent=ip.display_formatter
)

ip.display_formatter.formatters["application/json"] = E2BDictFormatter(
parent=ip.display_formatter
)

0 comments on commit c63f49c

Please sign in to comment.