在回调过程中,在dash中重命名菜单属性



,所以我有以下问题:我有一个应用程序,可以将数据上传为CSV文件。我想制作一个图形,该图应由分类器列细分。我希望用户能够从选择中选择他要绘制的图形,以及哪个列包含分类器。

我创建了一个放射性对象,用于选择图形和用于选择分类器列的下拉菜单,我将选定的图作为输入和选择的分类器作为状态。

没有问题是,从两者中所选的项目,放射性菜单和下拉菜单都称为"值"。所以我会得到这样的东西:

def RadioItems():
    return dcc.RadioItems(
    options=[
        {'label': 'lineplot', 'value': 'lineplot'},
        {'label': 'None', 'value' : 'None'}
    ],
    value='None',
    id='graph_selector')
def classifier_choice(df):
    '''
    called when data is uploaded
    '''
    columns=df.columns
    classifieroptions= [{'label' :k, 'value' :k} for k in columns]
    return dcc.Dropdown(
            #label='Classifier Column',
            id='classifier_choice',
            options=classifieroptions,
            placeholder='select the classifier column')
app.layout = html.Div([
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '100%',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    html.Table(id='output-data-upload'),
    RadioItems(),
    dcc.Graph(id='migration_data'),
    #hidden divs for storing data
    html.Div(id='shared_data', style={'display':'none'})
])
graph_options={'None':print(), 'lineplot':GD.lineplot}
@app.callback(Output('migration_data', 'figure'),
              [Input('graph_selector', 'value')],
              [State('classifier_choice', 'value')])

def get_value(value, value):
    return graph_options[value](df, value, testmode=True)

尽管我遇到了错误:" attributeError:'div'对象没有属性'键'"

这当然没有任何意义,因为没有办法区分两个值。有没有一种方法可以重命名下拉菜单的值属性,或以以下方式将其值分配给另一个变量

classifier=classifier_choice.value()

或类似的东西?

回答您有关参数名称的问题:回调装饰器获取组件的属性,并将它们作为参数以给定顺序传递给函数。您可以随心所欲地命名参数。

def get_value(selected_graph, selected_classifier):
    return graph_options[selected_graph](df, selected_classifier, testmode=True)

您可能必须返回与图形组件的图形属性兼容的东西。尽管如此,为了使graph_options的值返回函数对象,您需要在print之后摆脱括号。

最新更新