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

Adding support for serializing datetime.datetime instances #41

Closed
ramedina86 opened this issue Jun 17, 2023 Discussed in #40 · 1 comment
Closed

Adding support for serializing datetime.datetime instances #41

ramedina86 opened this issue Jun 17, 2023 Discussed in #40 · 1 comment
Labels
enhancement New feature or request

Comments

@ramedina86
Copy link
Collaborator

Discussed in #40

Originally posted by jreyesr June 17, 2023
Hello! I've been exploring Streamsync (awesome tool, BTW!) and I found out that apparently datetime.datetime instances cannot be serialized and sent to the frontend.

Minimal example
import datetime
import streamsync as ss

initial_state = ss.init_state({
  "now": datetime.datetime.now(),
})

This causes the following error to appear:

Error
Serialisation error

Traceback (most recent call last):
  File "/home/reyes/.local/lib/python3.10/site-packages/streamsync/core.py", line 239, in to_dict
    serialised_value = state_serialiser.serialise(value)
  File "/home/reyes/.local/lib/python3.10/site-packages/streamsync/core.py", line 129, in serialise
    raise StateSerialiserException(
streamsync.core.StateSerialiserException: Object of type <class 'datetime.datetime'> (MRO: ['datetime.datetime', 'datetime.date', 'builtins.object']) cannot be serialised.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/reyes/.local/lib/python3.10/site-packages/streamsync/app_runner.py", line 120, in _handle_session_init
    user_state = session.session_state.user_state.to_dict()
  File "/home/reyes/.local/lib/python3.10/site-packages/streamsync/core.py", line 241, in to_dict
    raise ValueError(
ValueError: Couldn't serialise value of type "<class 'datetime.datetime'>" for key "now".

image

As far as I can tell, this is due to the serialization code not supporting datetime.datetime instances

Background: I ran across this when trying to replicate Streamlit's starter tutorial in Streamsync. This tutorial has a Pandas DataFrame with a datetime column (it's a special Pandas datetime class, but it eventually inherits from Python's datetime.datetime)

Adding support for this would let users display data that has timestamp-style columns, such as "created on", "event time" and such.

Is there any interest in letting users pass datetime instances to the FE? I don't see any issues or discussions mentioning it, but it may be because Streamsync is fairly new. Any ideas?

@ramedina86 ramedina86 added the enhancement New feature or request label Jun 17, 2023
@ramedina86
Copy link
Collaborator Author

datetime.datetime is now serialisable in 0.1.9.

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

No branches or pull requests

1 participant