The following code produces a dash leaflet choropleth map from a JSON file:
import dash_html_components as html
import dash_leaflet as dl
from dash import Dash
from dash.dependencies import Output, Input
def tooltip_text(feature=None):
return [html.H5(feature["properties"]['count'])]
geojson = dl.GeoJSON(url="/assets/DATA.json",
children = dl.Tooltip(id='tooltip'),
id="geojson")
app = Dash(__name__)
app.layout = html.Div([
dl.Map(center = [52.5200, 13.4050],
children= [dl.TileLayer(), geojson])
], style={'width': '200px', 'height': '100px'})
@app.callback(Output('tooltip', 'children'),
[Input('geojson', 'hover_feature')])
def hover_info(feature):
return tooltip_text(feature)
if __name__ == '__main__':
app.run_server()
If I make changes to the JSON file, like for example if I update the count:
import json
with open('assets/DATA.json', 'r') as f:
DATA = f.read()
data = json.loads(DATA)
data['features'][0]['properties']['count'] = 45
with open('assets/DATA.json', 'w') as f:
json.dump(data, f)
the tooltip does not show the updated count. Why is this happening? Maybe the solution is to use the dcc.Interval component but I canโt figure out what the callback function should be. Maybe something like
@app.callback(Output('geojson', 'url'),
Input('interval-component', 'n_intervals'))
def update_graph(n):
return "/assets/DATA.json"
I donโt whether itโs relevant but I am using Jupyter Notebook.