Skip to content

Commit

Permalink
feat: implement output widget by using a display hook
Browse files Browse the repository at this point in the history
Similar to what is proposed in:
jupyter-widgets/ipywidgets#3253 (comment)
  • Loading branch information
maartenbreddels committed Apr 14, 2023
1 parent 7a80604 commit f1d580a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions solara/server/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ipykernel.kernelbase
import IPython.display
import ipywidgets
import ipywidgets.widgets.widget_output
from IPython.core.interactiveshell import InteractiveShell

from . import app, reload, settings
Expand Down Expand Up @@ -228,6 +229,22 @@ def Thread_debug_run(self):
_patched = False


def Output_enter(self):
self._flush()

def hook(msg):
if msg["msg_type"] == "display_data":
self.outputs += ({"output_type": "display_data", "data": msg["content"]["data"], "metadata": msg["content"]["metadata"]},)
return None
return msg

get_ipython().display_pub.register_hook(hook)


def Output_exit(self, exc_type, exc_value, traceback):
get_ipython().display_pub._hooks.pop()


def patch():
global _patched
if _patched:
Expand Down Expand Up @@ -274,10 +291,11 @@ def patch():
# not sure why we cannot reproduce that locally
ipykernel.kernelbase.Kernel.initialized = classmethod(kernel_initialized_dispatch) # type: ignore
ipywidgets.widgets.widget.get_ipython = get_ipython

# TODO: find a way to actually monkeypatch get_ipython
IPython.get_ipython = get_ipython
ipywidgets.widgets.widget_output.get_ipython = get_ipython

ipywidgets.widgets.widget_output.Output.__enter__ = Output_enter
ipywidgets.widgets.widget_output.Output.__exit__ = Output_exit

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

0 comments on commit f1d580a

Please sign in to comment.