Skip to content

Commit

Permalink
pydeck: Support rendering in Google Collab (#4337)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajduberstein committed Mar 2, 2020
1 parent 4f08098 commit 229d310
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 4 additions & 0 deletions bindings/pydeck/pydeck/bindings/deck.py
Expand Up @@ -124,6 +124,7 @@ def to_html(
notebook_display=True,
iframe_width=700,
iframe_height=500,
as_string=False,
**kwargs
):
"""Write a file and loads it to an iframe, if in a Jupyter environment;
Expand All @@ -141,6 +142,8 @@ def to_html(
Height of Jupyter notebook iframe in pixels, if rendered in a Jupyter environment.
iframe_height : int, default 500
Width of Jupyter notebook iframe in pixels, if rendered in a Jupyter environment.
as_string : bool, default False
Whether the HTML should be written as a string rather than to a file. Defaults to writing to a file.
Returns
-------
Expand All @@ -158,6 +161,7 @@ def to_html(
iframe_width=iframe_width,
tooltip=self.deck_widget.tooltip,
custom_libraries=pydeck_settings.custom_libraries,
as_string=as_string,
**kwargs
)
return f
32 changes: 23 additions & 9 deletions bindings/pydeck/pydeck/io/html.py
@@ -1,5 +1,6 @@
import os
from os.path import relpath, realpath
import sys
import tempfile
import time
import webbrowser
Expand All @@ -15,6 +16,9 @@ def convert_js_bool(py_bool):
return "true" if py_bool else "false"


in_google_collab = 'google.colab' in sys.modules


TEMPLATES_PATH = os.path.join(os.path.dirname(__file__), "./templates/")
j2_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(TEMPLATES_PATH), trim_blocks=True
Expand Down Expand Up @@ -99,8 +103,10 @@ def deck_to_html(
css_background_color=css_background_color,
custom_libraries=custom_libraries,
)

if as_string:
return html

f = None
try:
f = open_named_or_temporary_file(filename)
Expand All @@ -112,14 +118,22 @@ def deck_to_html(
if open_browser:
display_html(realpath(f.name))
if notebook_display:
from IPython.display import IFrame # noqa

notebook_to_html_path = relpath(f.name)
display( # noqa
IFrame(
os.path.join("./", notebook_to_html_path),
width=iframe_width,
height=iframe_height,
from IPython.display import display # noqa

if in_google_collab:
from IPython.display import HTML, Javascript # noqa
js_height_snippet = 'google.colab.output.setIframeHeight(0, true, {maxHeight: %s})' % iframe_height
display(Javascript(js_height_snippet))
display(HTML(html))
else:
from IPython.display import IFrame # noqa
notebook_to_html_path = relpath(f.name)
display( # noqa
IFrame(
os.path.join("./", notebook_to_html_path),
width=iframe_width,
height=iframe_height,
)
)
)

return realpath(f.name)

0 comments on commit 229d310

Please sign in to comment.