Skip to content

Commit

Permalink
pydeck: embed javascript for offline HTML generation (#4405)
Browse files Browse the repository at this point in the history
Thank you for your patience here @bandersen23. Verified it locally on my machine without internet access–everything works.

> if there's a way to automate that on build

This should be fine for now–I'll get it documented and released in a beta.
  • Loading branch information
bandersen23 committed Mar 25, 2020
1 parent 7f13d3c commit dd1c0b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 2 additions & 0 deletions bindings/pydeck/pydeck/bindings/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def to_html(
iframe_width=700,
iframe_height=500,
as_string=False,
offline=False,
**kwargs
):
"""Write a file and loads it to an iframe, if in a Jupyter environment;
Expand Down Expand Up @@ -162,6 +163,7 @@ def to_html(
tooltip=self.deck_widget.tooltip,
custom_libraries=pydeck_settings.custom_libraries,
as_string=as_string,
offline=offline,
**kwargs
)
return f
16 changes: 13 additions & 3 deletions bindings/pydeck/pydeck/io/html.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from os.path import relpath, realpath
from os.path import relpath, realpath, join, dirname
import sys
import tempfile
import time
Expand Down Expand Up @@ -27,19 +27,27 @@ def convert_js_bool(py_bool):
DECKGL_SEMVER
)

def cdn_picker(offline=False):
if offline:
with open(join(dirname(__file__),'./static/index.js'), 'r') as file:
js = file.read()
return "<script type=text/javascript>" + js + "</script>"

return "<script src=" + CDN_URL + "></script>"

def render_json_to_html(
json_input,
mapbox_key=None,
tooltip=True,
css_background_color=None,
custom_libraries=None,
offline=False
):
js = j2_env.get_template("index.j2")
html_str = js.render(
mapbox_key=mapbox_key,
json_input=json_input,
deckgl_jupyter_widget_bundle=CDN_URL,
deckgl_jupyter_widget_bundle=cdn_picker(offline=offline),
tooltip=convert_js_bool(tooltip),
css_background_color=css_background_color,
custom_libraries=custom_libraries
Expand Down Expand Up @@ -93,7 +101,8 @@ def deck_to_html(
iframe_width=500,
tooltip=True,
custom_libraries=None,
as_string=False
as_string=False,
offline=False
):
"""Converts deck.gl format JSON to an HTML page"""
html = render_json_to_html(
Expand All @@ -102,6 +111,7 @@ def deck_to_html(
tooltip=tooltip,
css_background_color=css_background_color,
custom_libraries=custom_libraries,
offline=offline
)

if as_string:
Expand Down
19 changes: 19 additions & 0 deletions bindings/pydeck/pydeck/io/static/index.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions bindings/pydeck/pydeck/io/templates/index.j2
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@
<title>pydeck</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" />
<script src="{{deckgl_jupyter_widget_bundle}}"></script>
{{deckgl_jupyter_widget_bundle}}
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
#deck-map-container {
width: 100%;
height: 100%;
background-color: black;
}
#map {
pointer-events: none;
height: 100%;
width: 100%;
position: absolute;
z-index: 1;
}
#deckgl-overlay {
z-index: 2;
background: {{css_background_color or 'none'}};
}
#deck-map-wrapper {
width: 100%;
height: 100%;
}
#deck-container {
width: 100vw;
height: 100vh;
Expand All @@ -53,7 +53,7 @@
const customLibraries = {{ custom_libraries or 'null' }};
const deck = createDeck({
mapboxApiKey: MAPBOX_API_KEY,
mapboxApiKey: MAPBOX_API_KEY,
container: document.getElementById('deck-container'),
jsonInput,
tooltip,
Expand Down

0 comments on commit dd1c0b7

Please sign in to comment.