I have created a network of items purchased together in a main graph with is searchable through a function that returns a subgraph. I have dropdowns on my dashboard to input the item and store I want the subgraph to be made from but am having trouble with getting my callbacks to work. If someone could give me pointers or code on how to get everything to talk that would be great! Currently, when I select something from my dropdowns in my webpage nothing happens. Thanks!
Hi @mjkline6,
What you need to do is on def update_output
generate your code again and return the figure
after filtering your data source with value
Here is a minimal example of how to make a dropdown work. You will need to adapt this, of course.
# Simple Dropdown
import pandas as pd
import plotly.express as px
# Get Data Source
df = pd.read_csv('your-csv-file.csv')
app = dash.Dash(__name__,title='YOUR APP',suppress_callback_exceptions=True,update_title=None,
meta_tags=[{"name": "viewport", "content": "width=device-width, initial-scale=1"}],
)
app.css.config.serve_locally = False
app.scripts.config.serve_locally = False
server = app.server
app.layout = html.Div(
[
dbc.Row(
[
dbc.Col(
dcc.Dropdown(
id='your_dropdown',
options=[{'label': i, 'value': i} for i in df.your_dropdown_column.unique()],
value='your_initial_value', className="dropdown"
),xs=12, sm=12, md=3, lg=3, xl=3, #This is just for guarantee mobile responsiveness
),
],
),
dcc.Graph(id="your_graph"),
],
)
@app.callback(
Output(component_id=""your_graph, component_property="figure"),
Input(component_id="your_dropdown", component_property="value")
)
# WHAT HAPPENS WHEN CALL BACK IS TRIGGERED
def dropdown_update(value):
df = pd.read_csv('your-csv-file.csv')
df_filtered = df[df['your_dropdown_column']==value]
# Your Code to generate the network graph
your_graph = your_code_goes_here
return your_graph
if __name__ == "__main__":
app.run_server(host='0.0.0.0', debug=False)
I hope this helps you going into the right direction.