单击按钮或复选框等其他组件后如何创建或安装破折号组件

How to create or mount dash component after clicking on other component like button or checkbox

我创建了一个全局 df 变量,我还创建了一个 csv 文件上传组件和显示 df 列的组件。我希望在加载组件选项更改的文件时出现此错误 options[0].label in Dropdown with ID "col-1" is required but it was not provided. 我该如何解决?

这是我的代码示例:

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

app = dash.Dash()

# I create a global variable 
global df
app.layout = html.Div(

# upload component
dcc.Upload(
        id='upload-df',
        children=html.Div([#code ]),
        }),
    html.Div(id='output-upload'),
    html.button(id='show'),
    html.Div(id='col-choice', children=[
                        html.Label('column 2'),
                        dcc.Dropdown(id='col-1'),
                        html.Label('column 2'),
                        dcc.Dropdown(id='col-2')

)

# parser
def parse_contents(contents, filename, date):
     # code
@app.callback(Output('output-upload', 'children'),
              Input('upload-data', 'contents'),
              State('upload-data', 'filename'),
              State('upload-data', 'last_modified'))
def update_output(list_of_contents, list_of_names, list_of_dates):
    # code

@app.callback(
    Output('col-1', 'options'),
    Output('col-22', 'options'),
    Input('show', 'n_clicks')
)
def show_colomn_option(clicks):
    number_of_output = 8
    try:
        df
        return ([{'label': i, 'value': i} for i in df.columns],)*2
    except NameError:
        return ([{'label': None, 'value': None}],)*2

我像 this

中描述的那样编码上传组件

Dash 对全局变量的支持不是很好,基本上它会破坏你的代码,所以你应该按照这里的描述去做 https://dash.plotly.com/advanced-callbacks