Skip to content

Commit

Permalink
Fix MapViz to allow the use of a non-mapbox style without raising a T…
Browse files Browse the repository at this point in the history
…okenError and add error propagation from JS (#59)

* Added check for mapbox style before raising TokenError

* style_url is now style and can be an url or json style

* Updated docs to take into account style_url change

* Updated examples to use new style parameter

* Added error when using secret tokens and corresponding tests
  • Loading branch information
EtienneDesticourt authored and ryanbaumann committed Mar 14, 2018
1 parent e00bad3 commit 506b73c
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs-markdown/viz.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
The `MapViz` class is the parent class of the various `mapboxgl-jupyter` visualizations. You can use this class to set default values for all visualizations rather than calling them directly from the other visualization objects.

### Params
**MapViz**(_data, access_token=None, center=(0, 0), below_layer='', opacity=1, div_id='map', height='500px', style_url='mapbox://styles/mapbox/light-v9?optimize=true', width='100%', zoom=0, min_zoom=0, max_zoom=24_)
**MapViz**(_data, access_token=None, center=(0, 0), below_layer='', opacity=1, div_id='map', height='500px', style='mapbox://styles/mapbox/light-v9?optimize=true', width='100%', zoom=0, min_zoom=0, max_zoom=24_)

Parameter | Description
--|--
data | GeoJSON Feature Collection
access_token | Mapbox GL JS access token.
center | map center point
style_url | url to mapbox style
style | url to mapbox style or stylesheet as a Python dictionary in JSON format
div_id | The HTML div id of the map container in the viz
width | The CSS width of the HTML div id in % or pixels.
height | The CSS height of the HTML map div in % or pixels.
Expand Down
2 changes: 1 addition & 1 deletion examples/point-viz-types-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@
}
],
"source": [
"viz.style_url='mapbox://styles/mapbox/dark-v9?optimize=true'\n",
"viz.style='mapbox://styles/mapbox/dark-v9?optimize=true'\n",
"viz.show()"
]
},
Expand Down
2 changes: 1 addition & 1 deletion mapboxgl/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

var map = new mapboxgl.Map({
container: 'map',
style: '{{ styleUrl }}',
style: {{ style }},
center: {{ center }},
zoom: {{ zoom }},
transformRequest: (url, resourceType)=> {
Expand Down
2 changes: 1 addition & 1 deletion mapboxgl/templates/circle.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var map = new mapboxgl.Map({
container: 'map',
style: '{{ styleUrl }}',
style: {{ style }},
center: {{ center }},
zoom: {{ zoom }},
transformRequest: (url, resourceType)=> {
Expand Down
2 changes: 1 addition & 1 deletion mapboxgl/templates/clustered_circle.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var map = new mapboxgl.Map({
container: 'map',
style: '{{ styleUrl }}',
style: {{ style }},
center: {{ center }},
zoom: {{ zoom }},
transformRequest: (url, resourceType)=> {
Expand Down
2 changes: 1 addition & 1 deletion mapboxgl/templates/graduated_circle.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var map = new mapboxgl.Map({
container: 'map',
style: '{{ styleUrl }}',
style: {{ style }},
center: {{ center }},
zoom: {{ zoom }},
transformRequest: (url, resourceType)=> {
Expand Down
2 changes: 1 addition & 1 deletion mapboxgl/templates/heatmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

var map = new mapboxgl.Map({
container: 'map',
style: '{{ styleUrl }}',
style: {{ style }},
center: {{ center }},
zoom: {{ zoom }},
transformRequest: (url, resourceType)=> {
Expand Down
17 changes: 10 additions & 7 deletions mapboxgl/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self,
opacity=1,
div_id='map',
height='500px',
style_url='mapbox://styles/mapbox/light-v9?optimize=true',
style='mapbox://styles/mapbox/light-v9?optimize=true',
width='100%',
zoom=0,
min_zoom=0,
Expand All @@ -29,7 +29,7 @@ def __init__(self,
:param data: GeoJSON Feature Collection
:param access_token: Mapbox GL JS access token.
:param center: map center point
:param style_url: url to mapbox style
:param style: url to mapbox style or stylesheet as a Python dictionary in JSON format
:param div_id: The HTML div id of the map container in the viz
:param width: The CSS width of the HTML div id in % or pixels.
:param height: The CSS height of the HTML map div in % or pixels.
Expand All @@ -39,18 +39,17 @@ def __init__(self,
"""
if access_token is None:
access_token = os.environ.get('MAPBOX_ACCESS_TOKEN', '')
if not access_token.startswith('pk'):
raise TokenError('Mapbox access token must be public (pk). ' \
if access_token.startswith('sk'):
raise TokenError('Mapbox access token must be public (pk), not secret (sk). ' \
'Please sign up at https://www.mapbox.com/signup/ to get a public token. ' \
'If you already have an account, you can retreive your token at https://www.mapbox.com/account/.')
self.access_token = access_token

self.template = 'base'
self.data = data
self.div_id = div_id
self.width = width
self.height = height
self.style_url = style_url
self.style = style
self.center = center
self.zoom = zoom
self.below_layer = below_layer
Expand Down Expand Up @@ -83,11 +82,15 @@ def add_unique_template_variables(self, options):

def create_html(self):
"""Create a circle visual from a geojson data source"""
if isinstance(self.style, str):
style = "'{}'".format(self.style)
else:
style = self.style
options = dict(
gl_js_version=GL_JS_VERSION,
accessToken=self.access_token,
div_id=self.div_id,
styleUrl=self.style_url,
style=style,
center=list(self.center),
zoom=self.zoom,
geojson_data=json.dumps(self.data, ensure_ascii=False),
Expand Down

2 comments on commit 506b73c

@colllin
Copy link

@colllin colllin commented on 506b73c Mar 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In CircleViz and GraduatedCircleViz, I get an error that seems like it might be related to this commit:

Please forgive me — I believe this exception was due to the mapboxgl module not playing nicely with importlib.reload().  I was trying to reload mapboxgl after upgrading to #master without restarting the notebook.

@ryanbaumann
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, @colllin -> feel free to open an issue if it shows up again!

Please sign in to comment.