Hi Everyone,
I am new to plotly Dash. I am trying to create a map in plotly dash and filter/slice and dice the data by clicking on a map. I have parts of the code working but i can’t figure out how i can use the call back to filter the KPI’s by clicking on the map.
Here is the code for what i have so far. Can you please help and let me know what i am missing, Thanks!
from dash import Dash, dcc, html, Output, Input # pip install dash
import dash_bootstrap_components as dbc # pip install dash-bootstrap-components
import plotly.express as px
import geopandas as gpd
import pyproj
import pandas as pd # pip install pandas
# reading in the shapefile
fp=('/Users/T/Downloads/Downloaded files/Downloaded files/test.shp')
map_df = gpd.read_file(fp)
map_df.to_crs(pyproj.CRS.from_epsg(4326), inplace=True)
df = pd.read_csv("/Users/test2.csv")
merged = map_df.set_index('id').join(df.set_index('id'))
#merged = merged.reset_index()
merged.head()
df.rename(columns={'column_1': 'column1', 'column_2': 'column2'}, inplace=True),
# Build your components
app = Dash(__name__, external_stylesheets=[dbc.themes.LUX])
mytitle = dcc.Markdown(children='')
mygraph = dcc.Graph(figure={})
dropdown = dcc.Dropdown(['c1', 'c2'],
value='column_name', # initial value displayed when page first loads
clearable=False)
(
[
html.Div(children=[
html.Pre(id='click-pre', style={'display':'inline-block', 'width':'50%'}, children=[]),
html.Pre(id='select-pre', style={'display':'inline-block', 'width':'50%'}, children=[])
]),
html.Div(id='hidden-div', style={'display': 'none'})
]
)
def update_graph(column_name): # function arguments come from the component property of the Input
print(column_name)
print(type(column_name))
fig = px.choropleth(merged,
geojson=merged.geometry,
locations=merged.index,
color='column_name')
fig.update_geos(fitbounds="locations",
visible=False)
fig.update_layout(clickmode='event+select')
#Define cards
card1 = dbc.Card([
dbc.CardBody([
html.H4("Card title", className="card-title",id="card_num1"),
html.P("This is some card text", className="card-text",id="card_text1")
])
],
style={'display': 'inline-block',
'width': '33.3%',
'text-align': 'center',
'color':'white',
'background-color': 'rgba(37, 150, 190)'},
outline=True)
card2 = dbc.Card([
dbc.CardBody([
html.H4("Card title", className="card-title",id="card_num2"),
html.P("This is some card text", className="card-text",id="card_text2")
]
)],
style={'display': 'inline-block',
'width': '33.3%',
'text-align': 'center',
'color':'white',
'background-color': 'rgba(37, 150, 190)'},
outline=True)
card3 = dbc.Card([
dbc.CardBody([
html.H4("Card title", className="card-title",id="card_num3"),
html.P("This is some card text", className="card-text",id="card_text3")
]
)],
style={'display': 'inline-block',
'width': '33.3%',
'text-align': 'center',
'color':'white',
'background-color': 'rgba(37, 150, 190)'},
outline=True)
# Customize your own Layout
app.layout = dbc.Container([
dbc.Row([
dbc.Col([mytitle], width=6)
], justify='center'),
dbc.Row([
dbc.Col([mygraph], width=12)
]),
dbc.Row([
dbc.Col([dropdown], width=6)
], justify='center'),
], fluid=True)
# Callback allows components to interact
@app.callback(
Output(mygraph, 'figure'),
Output(mytitle, 'children'),
Input(dropdown, 'value')
)
def update_graph(column_name): # function arguments come from the component property of the Input
print(column_name)
print(type(column_name))
return fig, '# '+column_name # returned objects are assigned to the component property of the Output
# Run app
if __name__=='__main__':
app.run_server(port=8052)