[BUG] Invalid value on first call to plotly.express.bar replacing a go.Figure()
Describe your context Windows 10, Python 3.8.6
- Dash information
dash 2.4.0
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table 5.0.0
plotly 5.8.0
-
Frontend information: probably not frontend related
- OS: Windows 10
- Browser: Firefox 100.0
Bug Description
In the following setup, whenever I start my dash app and show bar graphs for the first time, I get an "Invalid value" error for all but one of the bar graphs.
Minimal example:
- Two bar graphs, each one set by a separate callback
- One button activating both callbacks
- Bar graphs are initialized with an empty
go.Figure()(in our app containing some standard information) - On first execution of the callbacks, only one of the callbacks succeeds, the other fails with an "Invalid value" error (if more callbacks were added, all but one would fail)
- Without go.Figure() initialization the app would run fine.
- With only one graph the app would run fine.
- Any further executions of the callbacks succeed.
Minimal example
import pandas as pd
import plotly.graph_objects as go
from dash import Dash, dcc, html
from dash.dependencies import Input, Output
from plotly import express as px
app = Dash(__name__,
suppress_callback_exceptions=True,
title='Bug',
url_base_pathname='/dashboard/',
compress=True)
app.layout = html.Div([
dcc.Graph(id={'type': 'graph', 'index': '1_1'}, figure=go.Figure()),
dcc.Graph(id={'type': 'graph', 'index': '2_1'}, figure=go.Figure()),
html.Button('Test', id='button')
])
@app.callback(
[Output({'type': 'graph', 'index': '1_1'}, 'figure')],
[Input('button', 'n_clicks')],
prevent_initial_call=True
)
def callback_1(x):
return px.bar(
pd.DataFrame([[1, 2], [2, 4], [3, 2]], columns=['X', 'Y']),
x='X',
y='Y'
),
@app.callback(
[Output({'type': 'graph', 'index': '2_1'}, 'figure')],
[Input('button', 'n_clicks')],
prevent_initial_call=True
)
def callback_2(x):
return px.bar(
pd.DataFrame([[1, 2], [2, 4], [3, 2]], columns=['X', 'Y']),
x='X',
y='Y'
),
if __name__ == '__main__':
app.run_server(debug=True)
Stacktrace
Traceback (most recent call last): File "C:\Users\w11bwori\Documents\Personalfortschreibung\02-Tools\06-Dashboard\bug_dash\bug.py", line 39, in callback_2 return px.bar( File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\express_chart_types.py", line 373, in bar return make_figure( File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\express_core.py", line 1946, in make_figure apply_default_cascade(args) File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\express_core.py", line 969, in apply_default_cascade args["pattern_shape_sequence"] = [ File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\express_core.py", line 970, in
bar.marker.pattern.shape for bar in args["template"].data.bar File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\graph_objs\bar\marker_pattern.py", line 252, in shape return self["shape"] File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4733, in getitem elif self._props is not None and prop in self._props: File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4428, in _props return self.parent._get_child_props(self) File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4442, in _get_child_props if self._props is None: File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4428, in _props return self.parent._get_child_props(self) File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4442, in _get_child_props if self._props is None: File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4428, in _props return self.parent._get_child_props(self) File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 4462, in _get_child_props child_ind = BaseFigure._index_is(children, child) File "C:\Users\w11bwori\Envs\SKA-Dashboard\Lib\site-packages\plotly\basedatatypes.py", line 3960, in _index_is raise ValueError("Invalid value") ValueError: Invalid value
Expected behavior
Both graph objects should be filled, no error throws.
There seems to be a problem with setting the default values. Adding this code anywhere in the example program fixed the problem:
fig = go.Figure(layout=dict(template='plotly'))