Need help with making a Bar Chart using plotly Dash. Newbie here

So i’m looking to make an interactive barchart with a dropdown menu per asset which i will have in my dataframe. Here is a snippet of what my data frame looks like.

image

Here is the code i’m using to make tehe dashboard, it gives me no error and at the same time does not populate the graph with any data. Any help, or a different soultion would be highly regarded.

dff = df2

======================== Dash App

app = dash.Dash(name)

======================== App Layout

app.layout = html.Div([
html.H1(‘Asset Dashboard’, id=‘title’, style={‘color’: ‘#351e15’, ‘text-align’: ‘center’,
‘margin-top’: ‘20px’}),
dcc.Dropdown(
id=‘Asset_ID’,
options=[
{‘label’: ‘ML01 - MILL’, ‘value’: ‘ML01 - MILL’},
{‘label’: ‘ML01 - PINION SHAFT’, ‘value’: ‘ML01 - PINION SHAFT’},
],
value=‘ML01 - MILL’,
style={‘width’: ‘50%’, ‘margin’: ‘20px auto 0px auto’}
),
dcc.Graph(id=‘bar-graph’, style={‘height’: ‘425px’})
])
@app.callback(Output(‘bar-graph’, ‘figure’), Input(‘inp’, ‘value’))
def get_graph(inp):

figure = go.Figure().add_trace(go.Bar(x=dff['Report Date'],
                                          y=dff['ConditionCode'],
                                          name='Performance Graph',
                                          marker=dict(color='#351e15')))
return figure

if name == ‘main’:
app.run_server()

I want to make a bargraph with Report Date on x axis and Condition code on y axis ( changes color based of the color code )

Hi,

Welcome to the community!

The reason why the figure is empty is because the method add_trace does not return anything. So this should “work”:

figure = go.Figure()

figure.add_trace(go.Bar(x=dff['Report Date'],
                                          y=dff['ConditionCode'],
                                          name='Performance Graph',
                                          marker=dict(color='#351e15')))

You would still need to filter ddf for a given asset id, and something like ddf.query("Asset_ID == @inp" in the callback will do the job.

One last bit: if you want all ids available in the dropdown without hardcoding them, use this as options:

options=[{"label: val, "value": val} for val in ddf["Asset_ID"].unique()]
1 Like