Skip to content

Commit

Permalink
fix: workarounds for colab
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Jun 2, 2023
1 parent d3201fa commit bf9efcf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions ipyvolume/pylab.py
Expand Up @@ -815,6 +815,7 @@ def quiver(
def show(extra_widgets=[]):
"""Display (like in IPython.display.dispay(...)) the current figure."""
gcf() # make sure we have something..
utils.colab_workarounds()
display(gcc())
for widget in extra_widgets:
display(widget)
Expand Down
34 changes: 34 additions & 0 deletions ipyvolume/utils.py
Expand Up @@ -296,3 +296,37 @@ def thread_safe():
return execute

return wrapped

# same as in reacton/utils.py
def environment() -> str:
try:
module = get_ipython().__module__ # type: ignore
shell = get_ipython().__class__.__name__ # type: ignore
except NameError:
return "python" # Probably standard Python interpreter
else:
if module == "google.colab._shell":
return "colab"
elif shell == "ZMQInteractiveShell":
return "jupyter" # Jupyter notebook, lab or qtconsole
elif shell == "TerminalInteractiveShell":
return "ipython" # Terminal running IPython
else:
return "unknown" # Other type

_colab_enabled_custom_widget_manager = False
def colab_workarounds():
# similar as in reacton/core.py, but we know we will use ipyvue
if environment() == "colab":
import IPython.display # type: ignore

global _colab_enabled_custom_widget_manager
if not _colab_enabled_custom_widget_manager:
from google.colab import output

output.enable_custom_widget_manager()
_colab_enabled_custom_widget_manager = True
import ipyvue

# make sure jupyter-vue is loaded in the colab iframe
IPython.display.display(ipyvue.Html(tag="span", style_="display: none"))

0 comments on commit bf9efcf

Please sign in to comment.