I tried to use dcc.Tabs to show two graphs. Each graph is based on a unique dataframe. The default tab loaded fine, but when I clicked the other tab I got an error: “Cannot read properties of null (reading ‘layout’)”
Here is the code to reproduce the problem:
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import plotly.express as px
import pandas as pd
app = dash.Dash(__name__)
server = app.server
data_grant = {'x': ["Grant 1", "Grant 2", "Grant 3"],
'y': [2, 4, 6]}
data_pub = {'x': ["Publication 1", "Publication 2", "Publication 3"],
'y': [7, 2, 17]}
app.layout = html.Div([
html.H1(
children='test',
style={
'textAlign': 'center'
}
),
dcc.Tabs(id="tabs", value='publication', children=[
dcc.Tab(label='Publication', value='publication', children=[
html.H3(children='Publication'),
dcc.Graph(id='graph-pub')
]
),
dcc.Tab(label='Grant', value='grant', children=[
html.H3(children='Grant'),
dcc.Graph(id='graph-grant')
]
)]
)
])
@app.callback(
Output('graph-grant', 'figure'),
Output('graph-pub', 'figure'),
Input('tabs', 'value'))
def update_fig(tabs):
if tabs=='publication':
fig = px.bar(data_pub, x="x", y="y", title="Publication Results")
return None, fig
if tabs=='grant':
fig = px.bar(data_grant, x="x", y="y", title="Grant Results")
return fig, None
if __name__ == '__main__':
app.run_server(debug=True)