When trying to plot timedelta values on the y-axis, I am running into the following serialization error:
File “/Users/jmartini/.virtualenvs/test/lib/python3.7/site-packages/_plotly_utils/utils.py”, line 116, in default
return _json.JSONEncoder.default(self, obj)
File “/usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/encoder.py”, line 179, in default
raise TypeError(f’Object of type {o.class.name} ’
TypeError: Object of type timedelta is not JSON serializable
Here is some simple code that reproduces the error:
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import numpy as np
import datetime
x_date = [np.datetime64(datetime.datetime.utcnow()),
np.datetime64(datetime.datetime.utcnow()) - np.datetime64('1', 'D')]
y_times = [datetime.timedelta(hours=1), datetime.timedelta(hours=2)]
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div(children=[
html.H1(children='PR Open Time'),
dcc.Graph(
id='open-time-graph',
figure={
'data': [go.Scatter(
x=x_date,
y=y_times,
mode=' markers')
]
}
)
])
What is the correct way to plot timedelta objects using dash?
Thank you!