Localization of datetime
Hi there!
I am trying to figure out how to localize a
datetime
object in my Solara application. I want to show a banner like:
Where some_datetime
would be a datetime.datetime
object that I would retrieve from a database. The datetime
value would be saved in UTC, but I'd like to format it (and shift the timezone) to the user's locale.
I know I can get the user's language preference from solara.lab.headers.value["accept-language"]
but this doesn't give me the timezone info.
I think this should be possible with JavaScript (with something like toLocaleDateString()
), but I don't know how to run JavaScript in Solara. (I was poking around with solara.HTML
, but coudn't quite figure it out and don't know how to insert that into the solara.Success
message.)
Any help or suggestions would be appreciated!3 Replies
I also thought about specifying the
tag
for my solara.Success
component -- hoping that I might be able to write a script to find that element and then modify the content -- but it doesn't seem like the tag
gets applied to any of the HTML elements related to the component.
Like:
Hey!
Do you need to provide the time from the server, or is the client time sufficient? If you need the time from the server, it might be slightly more complicated, but probably still manageable. I'd take a look at making your own component using
solara.v.Snackbar
. There aren't docs on how to use it (since it doesn't have a Solara component yet), but you can find an example in the docs website code:
https://github.com/widgetti/solara/blob/8ad06aa85fafcce6f165b48f56776ca5776528dd/solara/website/components/contact.py#L126-L134
I'd take a look at adding the server time as an argument, and making use of .isoformat()
of the python datetime object. You should be able to add timezone info to the time, and then parse it again in Javascript using new Date(pythonDate)
.
how to run JavaScript in SolaraThe way to do this is by using
solara.ComponentVue
, see the docs here and an example of it here.GitHub
solara/solara/website/components/contact.py at 8ad06aa85fafcce6f165...
A Pure Python, React-style Framework for Scaling Your Jupyter and Web Apps - widgetti/solara
Hey @iisakkirotko thanks for the suggestion, I'll look into it a bit later!
The date/time I have will be in UTC and it could be any date/time -- say the user saved some record two days ago -- and I want to display that date/time to the user in their timezone and locale. So it's not the current server time nor the current client's time. If I can get the client's timezone (the current time would be fine for that) somehow, then I would know how much to shift the date/time. And then I could get the locale from the headers.