How can I perform multiple output for several graphs for this toy example?
import dash
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.express as px
import random
import plotly.graph_objects as go
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([dcc.Graph(id='boxplot'),
dcc.Graph(id='hist'),
dcc.Interval(id='graph-update', interval=10 * 1000, n_intervals=5)])
@app.callback([Output('boxplot', 'figure'),
Output('hist', 'figure')],
[Input('graph-update', 'n_intervals')])
def update_data(n):
data = pd.read_csv('titanic.csv')
data = data[['Age', 'Sex']]
new_row = {'Age': random.randint(1, 100), 'Sex': random.choice(['male', 'female'])}
data = data.append(new_row, ignore_index=True)
data.to_csv('titanic.csv')
fig1 = px.box(data, x="Sex", y="Age", title='Compare')
fig2 = go.Figure()
fig2.add_trace(
go.Histogram(x=data[data['Sex'] == 'male']['Age'], histnorm='percent', name='Male'))
fig2.add_trace(
go.Histogram(x=data[data['Sex'] == 'female']['Age'], histnorm='percent', name='Female'))
fig2.update_layout(
title_text=f'Histogram of Age',
xaxis_title_text='Age',
yaxis_title_text='Percent',
bargap=0.2,
bargroupgap=0.1
)
return fig1, fig2
if __name__ == '__main__':
app.run_server(debug=True, port=4327)
Callback error updating boxplot.figure