plotly / plotly/dash

Let Dash components verify and potentially ignore persisted/stored value

Open
#1,373 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature P3
Dominant language
Python
Stars
24.4k
Forks
2.3k
Avg merge
2d 7h
Merged PRs (30d)
13

Description

Is your feature request related to a problem? Please describe.

Dash support persistence, which is a simple-to-use feature for storing user choices in the UI.

It would be useful if Dash components (on the client/JavaScript side) could verify and potentially ignore a persisted value given to it. This would then help ensure Dash applications being consistent across restarts (when valid user choices vary).

Python MWE:

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html


# The list of options could come from some external
# data source, and vary between Dash app restarts:
OPTIONS = ["A", "B"] + ["C"]

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        html.Button("Click Me", id="button"),
        html.Div(id="output"),
        dcc.Dropdown(
            id="dropdown",
            value=OPTIONS[0],
            options=[{"label": v, "value": v} for v in OPTIONS],
            persistence=True,
        ),
    ]
)


@app.callback(
    Output("output", "children"),
    [Input("button", "n_clicks")],
    State("dropdown", "value"),
    prevent_initial_call=True,
)
def on_click(_, dropdown_state):
    # Some complex processing here which could
    # now occasionally get an "invaild" persisted option,
    # easily leading to errors.

    return f"Dropdown state: {dropdown_state}"


if __name__ == "__main__":
    app.run_server(debug=True)
  1. Run the app, open in browser and choose option C in the dropdown.
  2. Comment out + ["C"], let the server restart and click on the button.

You then get that the state of the dropdown value property is C, despite that it is not a valid option (which the component sort of signals in the UI by not actually showing C).

image

Same observation seen for other components (e.g. dcc.Checlist).

Describe the solution you'd like

  1. It might be that this already is possible within Dash JavaScript component source code. E.g. in the dcc.Dropdown example above it would then check that if the persisted value given to it is among the valid options in the options prop, and if not fall back to as if there was no previously persisted value.

    If this is already possible, it would be useful to have an extra paragraph in https://dash.plotly.com/persistence under "For component developers" explaining how such checks can be implemented and/or how/when persistence enters into the Dash/React component life cycle (e.g. I guess the options property for dcc.Dropdown need to be set before the persistence value is given to the component, in order for the component to actually do the check).

  2. Implement persisted value check for the basic components in dash-core-components (i.e. is persisted value among valid options, and fall back to as if there is no previously persisted value if not the case).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the client-side JavaScript source for dcc.Dropdown and dcc.Checklist, then read the persistence documentation section for component developers and the React component lifecycle details. Done means persisted values that are not valid for the current options are ignored across the basic dash-core-components, with the behavior documented for component developers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, python, react
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.