简体   繁体   English

是否可以从 Plotly Dash、dcc.store 中的值动态加载 dcc.Dropdown 选项?

[英]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 .该应用程序旨在存储来自dcc.Input的值并将其显示在dash_table上。 For the purpose of storing the input values dcc.store is being used.为了存储输入值,使用了dcc.store

Now I need to dynamically load the values of the first column ie "Student Name" in the dcc.Dropdown as options.现在我需要动态加载第一列的值,即dcc.Dropdown中的“学生姓名”作为选项。 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 ?是否有可能共享 dcc.store 中第一列的数据作为dcc.store中选项的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 ).是的,您可以从已填充一些 dataframe 的dcc.Store()加载。您将需要一个回调,它将 Store 的 modified_timestamp 属性作为 Input() 并将其数据作为 State(),在Dash 的官方文档中进行了解释)。 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"]
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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