I have a pandas dataframe with the x column being timestamps and y column being integers. I create a line chart using Plotly Express. So far so good.
Now I want to use extendData
. I am sure my return value is in the correct format, but it throws the following error in browser:
cannot extend missing or non-array attribute: y
On a closer inspection of the Figure object I found out that the y
axis object looks like
{
"dtype": "i2",
"bdata": "AgAAAAEAAAAAAAAAAAAAAAA...",
"_inputArray": {
"0": 2, // my actual data
"1": 0,
"2": 1,
...
}
}
suggesting that it was a numpy array, which works with plotting but likely not with extendTraces
which expects an array of numbers.
To confirm my hypothesis I converted the y column to strings of numbers. The error went away but now the plot behaves erratically. So is there a way to force the y column to be a vanilla array?
For reference, my update function looks like this:
@app.callback(
Output("time-series", "extendData"),
Input("time-series", "figure"),
Input("time-series-interval", component_property="n_intervals")
)
def update_time_series(f, n):
if n == 0:
return None
now_time = datetime.now(timezone.utc).replace(second=0, microsecond=0)
print(f"update_time_series: {n} {now_time}")
df = ... # get new dataframe since now_time
if df.empty:
return (dict(x=[], y=[]), [])
df = df.tail(1)
x = []
y = []
traces = []
for i, trace in enumerate(f['data']):
traces.append(i)
x.append(df[df['type'] == trace['name']]['date'].dt.strftime('%Y-%m-%dT%H:%M:%S.%f%z').tolist())
y.append(df[df['type'] == trace['name']]['count'].tolist())
return (dict(x=x, y=y), traces)