From b5793b536ed361907b1e7c2dbd8d658ae8f43fd2 Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Tue, 5 Mar 2024 09:39:40 +0100 Subject: [PATCH] feat: matplotlib support for display(figure) This makes it behave more like the Jupyter notebook environment. --- solara/server/shell.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/solara/server/shell.py b/solara/server/shell.py index 4a1d0df57..cb618ccb0 100644 --- a/solara/server/shell.py +++ b/solara/server/shell.py @@ -1,4 +1,6 @@ +import io import sys +from binascii import b2a_base64 from threading import local from unittest.mock import Mock @@ -184,8 +186,23 @@ def init_history(self): def init_display_formatter(self): super().init_display_formatter() + assert self.display_formatter is not None self.display_formatter.ipython_display_formatter = reacton.patch_display.ReactonDisplayFormatter() + # matplotlib support for display(figure) + # IPython.core.pylabtools has support for this, but it requires importing matplotlib + # which would slow down startup, so we do it here using for_type using a string as argument. + def encode_png(figure, **kwargs): + f = io.BytesIO() + format = "png" + figure.savefig(f, format=format, **kwargs) + bytes_data = f.getvalue() + base64_data = b2a_base64(bytes_data, newline=False).decode("ascii") + return base64_data + + formatter = self.display_formatter.formatters["image/png"] + formatter.for_type("matplotlib.figure.Figure", encode_png) + def init_display_pub(self): super().init_display_pub() self.display_pub.register_hook(self.display_in_reacton_hook)