Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support six different development paths
- Loading branch information
Andrew Duberstein
committed
Aug 24, 2019
1 parent
24fefaf
commit ef4a582
Showing
12 changed files
with
145 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""This file handles the creation of a config for RequireJS | ||
pydeck supports two strategies to render deck.gl maps: | ||
- A Juypter Notebook Widget | ||
- A standalone set of HTML templates | ||
RequireJS manages dependencies in Jupyter environments. | ||
To adhere to Jupyter standards and minimize code duplication, | ||
both pydeck rendering strategies use RequireJS. | ||
The RequireJS configuration is viewable in the root of the pydeck repo. | ||
The functions below serve to convert these dependencies into a RequireJS config | ||
for testing, development, and production builds. | ||
""" | ||
import os | ||
import json | ||
import urllib | ||
|
||
from jinja2 import Template | ||
|
||
def _is_open(url): | ||
'''Verifies that hot reloading dev server URL is running''' | ||
if not dev_server_url: | ||
return | ||
try: | ||
code = urllib.request.urlopen(url).getcode() | ||
if code != 200: | ||
raise Exception('Is your dev server running? Non-200 status at {url}: {code}'.format(url, code)) | ||
except Exception as e: | ||
raise Exception('Dev server connection issue', e) | ||
|
||
|
||
here = os.path.dirname(os.path.abspath(__file__)) | ||
dev_server_url = os.getenv('PYDECK_DEV_SERVER') | ||
_is_open(dev_server_url) | ||
WIDGET_PATH = os.path.join(here, '../../../../modules/jupyter-widget/dist') | ||
|
||
|
||
def create_notebook_requirejs(dependencies, base_path, setup_environment='development'): | ||
'''Embeds environment-appropriate RequireJS configuration into the Jupyter widget's module''' | ||
# Prepares JS dependencies for the notebook requirejs file | ||
if setup_environment == 'development' and dev_server_url: | ||
# Notebook with hot reloading development on http://localhost:8080 | ||
dependencies['paths']['nbextension/pydeck'] = dev_server_url | ||
elif setup_environment in ('test', 'production', 'development'): | ||
# Notebook using the JS bundle built from webpack command in @deck.gl/jupyter-widget | ||
# The notebook dependency manager will be written to ./pydeck/nbextension/static/extensionRequires.js | ||
# If this changes, ./pydeck/nbextension/__init__.py must also change | ||
dependencies['map']['*']['@deck.gl/jupyter-widget'] = 'nbextensions/pydeck/nb_extension' | ||
del dependencies['paths']['nbextension/pydeck'] | ||
else: | ||
raise Exception('Unrecognized setup environment') | ||
|
||
# Creates the notebook widget dependency manager | ||
# This file is excluded from version control | ||
notebook_output_js = os.path.join( | ||
base_path, 'pydeck', 'nbextension', 'static', 'extensionRequires.js' | ||
) | ||
with open(notebook_output_js, 'w+') as f: | ||
f.write(embed_in_widget_template(dependencies)) | ||
|
||
|
||
def create_standalone_render_requirejs(dependencies, base_path, setup_environment='development'): | ||
'''Embeds environment-appropriate RequireJS configuration into the standalone HTML renderer's module''' | ||
# Creates the standalone HTML renderer dependency file base_path | ||
if setup_environment == 'development' and dev_server_url: | ||
# Supports standalone HTML renderer with hot reloading | ||
dependencies['paths']['nbextension/pydeck'] = dev_server_url | ||
# TODO verify this path | ||
elif setup_environment == 'production': | ||
# Standalone HTML renderer in production requires reading from CDN | ||
CDN_URL = 'https://cdn.jsdelivr.net/npm/@deck.gl/jupyter-widget@latest/dist' | ||
dependencies['paths']['nbextension/pydeck'] = CDN_URL | ||
elif setup_environment == 'development': | ||
# Standalone HTML renderer with static reloading requires a bundled JS file of the Jupyter widget module | ||
dependencies['paths']['nbextension/pydeck'] = WIDGET_PATH | ||
else: | ||
raise Exception('Unrecognized setup environment') | ||
# Writes a file set near the standalone rendering code | ||
# This file is excluded from version control | ||
standalone_html_output_js = os.path.join( | ||
base_path, 'pydeck', 'io', 'templates', 'requirejs_dependencies.json' | ||
) | ||
with open(standalone_html_output_js, 'w+') as f: | ||
f.write(json.dumps(dependencies, indent=2)) | ||
|
||
def embed_in_widget_template(dependencies): | ||
with open(os.path.join(here, 'notebook_requirejs_config.j2')) as f: | ||
template = Template(f.read()) | ||
rendered = template.render(dependencies=json.dumps(dependencies, indent=4)) | ||
return rendered |
14 changes: 14 additions & 0 deletions
14
bindings/python/pydeck/dependency_managers/notebook_requirejs_config.j2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{#- Requirements handler for Jupyter notebook extension -#} | ||
{#- Loads dependencies defined in the pydeck/requirejs_dependencies.json -#} | ||
/* eslint-disable */ | ||
define(function() { | ||
'use strict'; | ||
requirejs.config( | ||
{{- dependencies -}} | ||
); | ||
// Export the required load_ipython_extension function | ||
return { | ||
load_ipython_extension: function() {} | ||
}; | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
bindings/python/pydeck/pydeck/io/templates/notebook_require.j2
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
#!/usr/bin/env python | ||
# coding: utf-8 | ||
def _jupyter_nbextension_paths(): | ||
paths = { | ||
return [{ | ||
'section': 'notebook', | ||
'src': 'nbextension/static', | ||
'dest': 'pydeck', | ||
'require': 'pydeck/extensionRequires' | ||
} | ||
return [paths] | ||
}] |
Oops, something went wrong.