简体   繁体   中英

Is it possible to dynamically load dcc.Dropdown option from values in Plotly Dash, dcc.store?

The application is built to store values from dcc.Input and display it on a dash_table . For the purpose of storing the input values dcc.store is being used.

Now I need to dynamically load the values of the first column ie "Student Name" in the dcc.Dropdown as options. Is there a possible way to share the data of the first column in dcc.store as an input to the options in dcc.Dropdown ?

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

user_key = 'Student Name'
# Setup table.
columns = ['Student Name', 
           'Age', 
           'Place',
           'Grade']

table = dash_table.DataTable(columns=[{"name": column, "id": column} for column in columns], 
                             data=[], id="table")
# Create app.


app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([
    
                        html.Div([dcc.Input(id=column, value=column) for column in columns] +
                      [html.Button("Save", id="save"), dcc.Store(id="cache", data=[]), table]),
                        
                        html.Div([
                            
                            dcc.Dropdown(
                                            id='demo-dropdown',
                                            options=[
                                                        {'label': 'Mark', 'value': 'mrc'},

                                                        ],
                                                            value='mrc'
    )
                            
                            
                            
                            ])
                        
                        
                        
                        
                        ])


@app.callback(Output("table", "data"),
              [Input("save", "n_clicks")],
              [State("table", "data")] +
              [State(column, "value") for column in columns])



def update_table(n_clicks, data, *args):
    record = {columns[i]: arg for i, arg in enumerate(list(args))}
    
    try:
        record_index = [record[user_key] for record in data].index(record[user_key])
        data[record_index] = record
    
    except ValueError:
        data.append({columns[i]: arg for i, arg in enumerate(list(args))})
    
    return data


if __name__ == '__main__':
    app.run_server()

Yes you can load from dcc.Store() that has been populated with some dataframe. You will need a callback which takes in the Store's modified_timestamp property as the Input() and its data as a State() explained in Dash's official documentation ). So something like this callback will do the trick:

@app.callback(
    [Output("demo-dropdown", "options"), Output("demo-dropdown", "value")],
    [Input("cache", "modified_timestamp")],
    State("cache", "data"),
)
def update_dropdown_from_store(time_stamp, data):
    return (
        [
            [{"label": i, "value": i} for i in list(df["Student Name"])],
            df["Student Name"].iloc[0],
        ]
        if data != []
        else [[{"label": "Mark", "value": "mrc"}], "mrc"]
    )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM