Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

dcc.DatePickerSingle() persistence not working #700

Closed
jezlax opened this issue Nov 14, 2019 · 5 comments · Fixed by plotly/dash#1376 or #854
Closed

dcc.DatePickerSingle() persistence not working #700

jezlax opened this issue Nov 14, 2019 · 5 comments · Fixed by plotly/dash#1376 or #854
Assignees
Milestone

Comments

@jezlax
Copy link

jezlax commented Nov 14, 2019

I'm on dcc 1.5.1 ... I have a date picker single which populates a date using the following...

dcc.DatePickerSingle(className = 'no-print', id='choose-date', date = datetime.today() - timedelta(hours=7), persistence=True, persistence_type='session')

however the persistence does not take. In this example, the date picker populates a set of links (html.A()) elements for the given date. The user then selects the link of choice to populate the remainder of the charts on the app.

While the date picker works fine, when the user selects the link, it triggers an HREF callback and the date picker resets to the default date. My goal is to have the date persist until the user exits the page.

@alexcjohnson
Copy link
Collaborator

Thanks for the report, investigating... that said, since the link is to a different view within the same dash app you may be able to do this more easily using dcc.Link and dcc.Location https://dash.plot.ly/urls and leaving the date picker outside the part of the page that's changed.

@alexcjohnson
Copy link
Collaborator

@jezlax I'm guessing this DatePickerSingle component is generated either in a layout function or in a callback - ie datetime.today() - timedelta(hours=7) is changing every time it's called because it goes all the way to microseconds - it gets serialized something like date: "2019-11-14T14:45:12.741865". This is a problem for persistence because we'll only reapply the user's choice if the initial value provided with the component is the same as before the user made that choice.

If you strip out the (unused) time portion - something like (datetime.today() - timedelta(hours=7)).date() - then it will give a consistent value for 24 hours - so if the user keeps the app open for multiple days it'll reset (on reload) once a day, but the persistence should be maintained until that point.

@jezlax
Copy link
Author

jezlax commented Nov 15, 2019

Thanks @alexcjohnson - I will dev this rework today and let you know.

@Marc-Andre-Rivet
Copy link
Contributor

Marc-Andre-Rivet commented Apr 1, 2020

@harryturr As discussed IRL, this is actionable in at least 3 ways

  • We can modify the persistence logic so that we only extract/apply the date portion of the datetime - this should provide 24 hours stability without any additional code from the users (https://dash.plotly.com/persistence For component developers)
  • We should plan on adding some best practice info to the docs, specifically about explicit timezones information on the datetime passed to Dash. This is useful if the user/server are not located in the same timezone (new Docs issue)
  • Might be interesting to consider some alternate values to represent today / tomorrow. It might be that for many use cases today or today+/-N might be an interesting constant value to pass to the component - to be processed, and would provide a constant baseline for persistence (new DCC issue)

Please make sure the behavior is correct for all DateTime props of DatePickerSingle and DatePickerRange.

@Marc-Andre-Rivet
Copy link
Contributor

@harryturr As a followup please update dash-docs for developers section of http://dash.plotly.com/persistence to take into account the new capabilities.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.