I'm creating US map and I want to make it interactive. If the user clicks on certain state it get highlighted, and if another state is clicked it will be highlighted also. Finally if the user clicked on a highligted state, the highlight will be removed. I can not think which property to change in @app.callback to make this work. Here is the code that constructs the us map:
import dash_leaflet as dl
from dash import Dash, html, Output, Input, State
from dash_extensions.javascript import arrow_function
app = Dash()
app.layout = html.Div([
dl.Map(center=[39, -98], zoom=4, children=[
dl.TileLayer(),
dl.GeoJSON(url="/assets/us-states.pbf", format="geobuf", id="states",
hoverStyle=arrow_function(dict(weight=5, color='#666', dashArray='')),
options=dict(hidden=True)), # geobuf resource (fastest option)
], style={'width': '100%', 'height': '50vh', 'margin': "auto", "display": "block"}, id="map"),
html.Div(id="state"), html.Div(id="capital")
])
@app.callback(Output("state", "children"), [Input("states", "click_feature")])
def capital_click(feature):
if feature is not None:
return f"You clicked {feature['properties']['name']}"
if __name__ == '__main__':
app.run_server(port=8052, debug=True)
Here is the output of the above code: note that I've clicked California to return the text below the image Map Image