Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json datetime encode support #475

Closed
zhuoqiang opened this issue Jan 19, 2024 · 3 comments
Closed

json datetime encode support #475

zhuoqiang opened this issue Jan 19, 2024 · 3 comments

Comments

@zhuoqiang
Copy link

zhuoqiang commented Jan 19, 2024

when I use plotly in solara, if axis contains datetime, the chart shows blank without data, and solara server complains:

ERROR:    Error sending message: datetime.date(2023, 11, 21) is not JSON serializable
Traceback (most recent call last):
  File "python3.12/site-packages/solara/server/kernel.py", line 249, in send
    wire_message = json_dumps(msg)
                   ^^^^^^^^^^^^^^^
  File "python3.12/site-packages/solara/server/kernel.py", line 68, in json_dumps
    return jsonmodule.dumps(
           ^^^^^^^^^^^^^^^^^
  File "python3.12/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "python3.12/site-packages/solara/server/kernel.py", line 60, in json_default
    raise TypeError("%r is not JSON serializable" % obj)
TypeError: datetime.date(2023, 11, 21) is not JSON serializable

turns out the plotly chart object contains datetime which could not be handle by python standard json lib by default. and I could not find a way to provide my own JSONEncoder for solara to use.

@maartenbreddels
Copy link
Contributor

Do you have the code to reproduce this (something I can copy paste), because looking at https://github.com/jupyter/jupyter_client/blob/main/jupyter_client/jsonutil.py it seems that only datetime is supported in jupyter as well, not date.

@zhuoqiang
Copy link
Author

Do you have the code to reproduce this (something I can copy paste), because looking at https://github.com/jupyter/jupyter_client/blob/main/jupyter_client/jsonutil.py it seems that only datetime is supported in jupyter as well, not date.

It turns out the root cause of this issue is that: the dtype of the date time column in the dataframe is object instead of a normal datetime type like datetime64[ns].

Once I convert the dtype to datetme64[ns] it works perfectly.

@maartenbreddels
Copy link
Contributor

Ok, so this probably also didn't work in Jupyter notebook/lab. I'm assuming this to be a user-error or an issue with plotly (for not doing the conversion). Thank you for the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants