Skip to content

Commit 23bc609

Browse files
feat: implement output widget by using a display hook
Similar to what is proposed in: jupyter-widgets/ipywidgets#3253 (comment)
1 parent 6430133 commit 23bc609

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

solara/server/patch.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import ipykernel.kernelbase
1111
import IPython.display
1212
import ipywidgets
13+
import ipywidgets.widgets.widget_output
1314
from IPython.core.interactiveshell import InteractiveShell
1415

1516
from . import app, reload, settings
@@ -228,6 +229,22 @@ def Thread_debug_run(self):
228229
_patched = False
229230

230231

232+
def Output_enter(self):
233+
self._flush()
234+
235+
def hook(msg):
236+
if msg["msg_type"] == "display_data":
237+
self.outputs += ({"output_type": "display_data", "data": msg["content"]["data"], "metadata": msg["content"]["metadata"]},)
238+
return None
239+
return msg
240+
241+
get_ipython().display_pub.register_hook(hook)
242+
243+
244+
def Output_exit(self, exc_type, exc_value, traceback):
245+
get_ipython().display_pub._hooks.pop()
246+
247+
231248
def patch():
232249
global _patched
233250
if _patched:
@@ -274,10 +291,11 @@ def patch():
274291
# not sure why we cannot reproduce that locally
275292
ipykernel.kernelbase.Kernel.initialized = classmethod(kernel_initialized_dispatch) # type: ignore
276293
ipywidgets.widgets.widget.get_ipython = get_ipython
277-
278294
# TODO: find a way to actually monkeypatch get_ipython
279295
IPython.get_ipython = get_ipython
280-
ipywidgets.widgets.widget_output.get_ipython = get_ipython
296+
297+
ipywidgets.widgets.widget_output.Output.__enter__ = Output_enter
298+
ipywidgets.widgets.widget_output.Output.__exit__ = Output_exit
281299

282300
def model_id_debug(self: ipywidgets.widgets.widget.Widget):
283301
from ipyvue.ForceLoad import force_load_instance

0 commit comments

Comments
 (0)