I am looking to use Dash to read in a geojson file and display it on a mapbox. As a starting point I am using this python plotly example:
https://plot.ly/python/county-level-choropleth/
which works fine. This is the code:
import dash
import plotly.graph_objs as graph_objs
import json
import dash_core_components as dcc
import dash_html_components as htmlwith open(‘florida-red-data.json’) as f:
data = json.load(f)app = dash.Dash()
MAPBOX_TOKEN=MY_MAPBOX_TOKEN #<—I USE MY OWN TOKEN IN HERE
data = graph_objs.Data([
graph_objs.Scattermapbox(
lat=[‘45.5017’],
lon=[‘-73.5673’],
mode=‘markers’,
)
])
layout = graph_objs.Layout(
height=600,
autosize=True,
hovermode=‘closest’,
mapbox=dict(
layers=[
dict(
sourcetype = ‘geojson’,
source = ‘https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json’,
#source = ‘florida-red-data.json’,
#source = data,
type = ‘fill’,
color = ‘rgba(163,22,19,0.8)’
)
],
accesstoken=(MAPBOX_TOKEN),
bearing=0,
center=dict(
lat=27.8,
lon=-83
),
pitch=0,
zoom=5.2,
style=‘light’
),
)app.layout = html.Div([
html.Div(
html.Pre(id=‘lasso’, style={‘overflowY’: ‘scroll’, ‘height’: ‘100vh’}),
className=“three columns”
),html.Div( children=dcc.Graph(id='graph',figure=dict(data =data,layout =layout)) )
], className=“row”)
app.css.append_css({
‘external_url’: ‘https://codepen.io/chriddyp/pen/bWLwgP.css’
})if name == ‘main’:
app.run_server(debug=True)
If I save the file florida-red-data.json
locally and try to access it by modifying the line
source = ‘https://raw.githubusercontent.com/plotly/datasets/master/florida-red-data.json’,
to
source = ‘.\florida-red-data.json’,
It doesn’t work.
I also tried reading in the file in at the top of the script as a global variable using:
import json
with open(‘florida-red-data.json’) as f:
data = json.load(f)
and then
source = ‘data’,
That didn’t work either. Anyone got any ideas about how I could make this work without accessing the file via a hyperlink or is this not possible? Also, if an error message is being generated is as part of this, is there a way to see it?