plotly.py
plotly.py copied to clipboard
ValueError(“Invalid value”) in basedatatypes.py
After upgrading to Plotly 5 (either 5.1.0 or 5.2.2, or 5.3.1, 5.4.0), I am seeing the error (given below) for various plots, at random. The issue goes away if Plotly is downgraded to 4.14.3.
File "plotly/basedatatypes.py", line 3952, in _index_is
raise ValueError("Invalid value")
Here are relevant reports from me and other users
- https://community.plotly.com/t/valueerror-invalid-value-in-basedatatypes-py
- https://community.plotly.com/t/inconsistent-callback-error-updating-scatter-plot
- https://community.plotly.com/t/unknown-invalidvalue-error/
I have a Dash app that plots several graphs. When the Dash app starts, some plots do not get displayed, and I see the error. This only occurs on the initial startup of the app. When the webpage is refreshed, the error does not re-appear, and all plots get displayed without errors.
If I downgrade to Plotly 4.14.3, the issue goes away entirely.
Here is a reproducible (almost reliably) example. Start the script, wait a few sec, access the page at localhost:5000. The failures happen at random on either of the four graphs or several of them at once.
Plotly 5.4.0 Dash 1.21.0 (or 2.0.0 does not matter)
Running the code with debug=False seems to trigger the error more reliably than with debug=True.
Note that this only occurs on the initial startup of the app and the first visit to the page. When the webpage is refreshed, the error does not re-appear, and all plots get displayed without errors.
The error message and the screenshot are given below.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
from dash.dependencies import Input, Output
def gen_plot():
return px.bar([1, 2, 3], [4, 5, 4])
def init_callbacks(app):
@app.callback(
Output('graph1', 'figure'),
Input('dropdown', 'value'),
)
def update1(_):
return gen_plot()
@app.callback(
Output('graph2', 'figure'),
Input('dropdown', 'value'),
)
def update2(_):
return gen_plot()
@app.callback(
Output('graph3', 'figure'),
Input('dropdown', 'value'),
)
def update3(_):
return gen_plot()
@app.callback(
Output('graph4', 'figure'),
Input('dropdown', 'value'),
)
def update4(_):
return gen_plot()
def init_layout():
return html.Div([
dcc.Dropdown(
id='dropdown',
value=1,
),
dcc.Graph(id='graph1'),
dcc.Graph(id='graph2'),
dcc.Graph(id='graph3'),
dcc.Graph(id='graph4'),
])
app = dash.Dash(__name__)
app.layout = init_layout
init_callbacks(app)
app.run_server(debug=False, port=5000)
The error:
127.0.0.1 - - [29/Oct/2021 19:53:20] "POST /_dash-update-component HTTP/1.1" 500 -
[2021-10-29 19:53:20,832] ERROR in app: Exception on /_dash-update-component [POST]
Traceback (most recent call last):
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/dash/dash.py", line 1096, in dispatch
response.set_data(func(*args, outputs_list=outputs_list))
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/dash/dash.py", line 1017, in add_context
output_value = func(*args, **kwargs) # %% callback invoked %%
File "/home/skhan/notebooks/scripts/dash_dev.py", line 37, in update4
return gen_plot()
File "/home/skhan/notebooks/scripts/dash_dev.py", line 8, in gen_plot
return px.bar([1, 2, 3], [4, 5, 4])
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/express/_chart_types.py", line 365, in bar
return make_figure(
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/express/_core.py", line 1931, in make_figure
apply_default_cascade(args)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/express/_core.py", line 969, in apply_default_cascade
args["pattern_shape_sequence"] = [
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/express/_core.py", line 970, in <listcomp>
bar.marker.pattern.shape for bar in args["template"].data.bar
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/graph_objs/bar/marker/_pattern.py", line 252, in shape
return self["shape"]
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4693, in __getitem__
elif self._props is not None and prop in self._props:
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4388, in _props
return self.parent._get_child_props(self)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4402, in _get_child_props
if self._props is None:
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4388, in _props
return self.parent._get_child_props(self)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4402, in _get_child_props
if self._props is None:
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4388, in _props
return self.parent._get_child_props(self)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 4422, in _get_child_props
child_ind = BaseFigure._index_is(children, child)
File "/home/skhan/.virtualenvs/penv-dant-dash/lib/python3.8/site-packages/plotly/basedatatypes.py", line 3952, in _index_is
raise ValueError("Invalid value")
ValueError: Invalid value
The screenshot

I am also hit by this. Is there any progress?
I have not heard anything yet, besides a workaround by LeoWY here https://community.plotly.com/t/valueerror-invalid-value-in-basedatatypes-py/55993/6
I found the same problem. Any progress apart from the workaround with the try?
Also facing the same issue with a similar setup (Plotly 5.6.0 and Dash 2.3.0) and use case. Exact same behaviour observed : dash app plotting several graphs and a random number of them are not being displayed at startup, with the same error thrown as stated above. Would be glad to hear some news about it, at least some insights on how to tackle this issue.
Same issues here with Plotly 5.6.0 and Dash 2.1.0. For some reason the workaround with the try clause avoids errors.
Fixed by adding:
fig = go.Figure(layout=dict(template='plotly'))
before instantiating the figure.
e.g.
fig = go.Figure(layout=dict(template='plotly'))
fig = px.bar(temp_df, ...
Thanks, @KnopKirrie. That helped.
I have 11 graphs in 4 tabs in a Dash app, which consistently produced the ValueError(“Invalid value”) error on random graphs. Until now, I was using the workaround with the try.
So, to implement your approach, I removed the try logic, and added figure=go.Figure(layout=dict(template='plotly')) for each dcc.Graph in the layout.
Note that previously, I did not have the value for the figure set in dcc.Graph at all. It was returned by a callback on the startup.
As an experiment, I also tried using figure=go.Figure(), but it did not help.
FYI - This same error is still occurring when using plotly/dash, in 2024. Luckily, the above fix by "KnopKirrie" still works.
In fact, you only have to include this line one time before any of your other plotly or dash code:
go.Figure(layout=dict(template='plotly'))
@plan-9-from-outer-space , Can you please share the code the reproduces this error? I just the code posted by the original poster, but I didn't get an error.
Original poster is here. The code sample I submitted in 2021 produces the error just fine. I have just tested it again in Dash 2.15.0 and got the same error.
As many other reporters clearly indicated, the nature of this error seems random. Try running the code a few times, and opening a fresh browser tab perphaps for each try?
Below is a link to a self-contained file of code that shows the error. I am using plotly 5.18.0 and dash 2.15.0 (both are the latest versions on pypi.org). I am using python 3.11.7. BTW, I also get an annoying warning from plotly, which gets printed to the terminal whenever I click on one of the dash tabs:
C:...\envs\PY1\Lib\site-packages\plotly\express_core.py:2065: FutureWarning: When grouping with a length-1 list-like, you will need to pass a length-1 tuple to get_group in a future version of pandas. Pass (name,) instead of name to silence this warning.
Below is the link to a file that shows the error. Just save locally and run "python dashboard.py".
I forgot to mention -- you will need to comment out line 26, which is the bug fix.
The FutureWarning is being addressed in #4519
@sergiykhan and @plan-9-from-outer-space thanks for the self-contained apps. But I'm afraid I'm still unable to reproduce this error using either, no matter how much I restart or reload. Looks like it's happening both on Windows and on Linux, any other info those seeing this can give to help us reproduce?
I created a new python environment, installed only python 3.11.7, dash, and pandas, and the same error occurs. I am running Windows 11. Can you try doing that?
That's exactly what I did before, but upon doing it again I did see the error! Seems like there's some sort of a race condition when you fire multiple figure callbacks simultaneously, I couldn't get it to happen with only one graph callback. But also after one or two times showing the error it never happens again, no matter how many times I stop Python and restart it, and/or close and reopen the page, so it's like plotly.py has sorted itself out, maybe generated __pycache__ files or some such, and the problem never recurs.
Thanks for your patience - this is still going to be tricky to figure out but at least I've seen it now!
@alexcjohnson Not sure if it helps, but here is a bit more info about my test (original post).
I keep the Chrome tab open at http://localhost:5000/, and hit F5 to refresh the page after the app is re-launched.
The algorithm: launch the app, hit F5, check if there is an error, close the app. Repeat.
Right now, I did about 15 attempts. Three attempts produced an error. The number of unrendered plots was between 1 and 3.
I do not recreate the Python virtual environment nor do I clean any files.