In this dash_leaflet app if I clicked on certain governorate, its color will change to highlight that I’ve selected it as the following image:
I’m trying to make the dropdown do the same effect. If I select a governorate from the dropdown it will highlight it. I can’t get it done.
Here’s my code so far:
import random, json
import dash
from dash import dcc, html, Dash, callback, Output, Input, State
import dash_leaflet as dl
import geopandas as gpd
from dash_extensions.javascript import assign
import pandas as pd
df = pd.read_csv('data/Eastren_region_indicators.csv')
#df = df.sort_values('Year')
app = dash.Dash(__name__)
# distinct_governorates = df['Governorates'].unique()
gdf = gpd.read_file("data/OutputEPSG4326.geojson")
gdf['tooltip'] = gdf['GOVERNORATENAME_EN']
distinct_governorates = gdf['GOVERNORATENAME_EN'].unique()
style_handle = assign("""function (feature, context) {
const selection = context.props.hideout || {};
if (feature.id in selection) {
return {color: '#AA4A44', fillColor: '#AA4A44', weight: 2};
}
return {color: '#333', fillColor: '#f5f0e4', weight: 1};
}""")
app.layout = html.Div([
dl.Map([
dl.TileLayer(url="http://tile.stamen.com/toner-lite/{z}/{x}/{y}.png"),
dl.GeoJSON(data=json.loads(gdf.to_json()), id="state-layer",
options=dict(style=style_handle))],
style={'width': '100%', 'height': '600px'},
id="map",
center=[24, 47.5],
),
dcc.Dropdown(
id='search-bar',
options=[{'label': gov, 'value': gov} for gov in distinct_governorates],
placeholder='Search Governorates...',
clearable=True,
)
])
app.clientside_callback(
"""
function(clickedFeature, hideout) {
if (!clickedFeature) {
// NB. raise PreventUpdate to prevent ALL outputs updating,
// dash.no_update prevents only a single output updating
throw window.dash_clientside.PreventUpdate;
}
const id = clickedFeature.id;
const selection = hideout || {};
if (id in selection) {
delete selection[id];
}
else {
selection[id] = clickedFeature;
}
return [selection, null];
}
""",
Output("state-layer", "hideout"),
Output("state-layer", "click_feature"),
Input("state-layer", "click_feature"),
)
if __name__ == '__main__':
app.run_server(debug=True)