首页 > 解决方案 > 尝试设置下拉多时的关键错误等于 true

问题描述

[![在此处输入图像描述][1]][1]我绘制了一个带有在下拉列表中选择的值的图,当我只使用单个值时我得到它但是当我尝试使用多个值时它显示键错误任何人都可以检查我的代码吗

使用链式回调我在这个用户中创建了这个用户必须根据 x 和 y 轴列选择 x 和 y 轴我们使用下拉菜单选择多个值来过滤数据

但是当我使用 multi 时它起作用当我想取多个值时等于 false 显示这个关键错误如何解决这个问题请告诉我

import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objs as go
import plotly.express as px
from dash.dependencies import Input, Output
import dash
import pandas as pd

app=dash.Dash(external_stylesheets=[dbc.themes.SOLAR])

colors = {
    'background': '#111111',
    'text': '#7FDBFF'
}

config = {'modeBarButtonsToRemove': ['toggleSpikelines','hoverCompareCartesian','zoom2d','zoomIn2d','zoomOut2d','resetScale2d','autoScale2d',
'select2d','pan2d','lasso2d'],'displaylogo': False
}


df=pd.read_excel(r'C:\Users\gopik\Downloads\Microsoft.SkypeApp_kzf8qxf38zg5c!App\All/Sample Data-MM.xls')

SIDEBAR_STYLE = {
    "position": "fixed",
    "top": 0,
    "left": 0,
    "bottom": 0,
    "width": "16rem",
    "padding": "2rem 1rem",
    "background-color": "#f8f9fa",
}


CONTENT_STYLE = {
    "margin-left": "18rem",
    "margin-right": "2rem",
    "padding": "2rem 1rem",
}

sidebar = html.Div(
    [
        html.H2("Plotly Graphs", className="display-4"),
        html.Hr(),
        html.P(
            "Plots For Duplicate Values", className="lead"
        ),
        dbc.Nav(
            [
                dcc.Dropdown(
                    id='demo-dropdown',
                    options=[
                        {'label': 'Bar Chart', 'value': 'Bar Chart'},
                        {'label': 'Scatter', 'value': 'Scatter'},
                        {'label':'Pie Chart','value':'Pie Chart'}
                    ],
                    value='Bar Chart'
                ),
                dbc.Label("X variable"),
                dcc.Dropdown(
                    id="x-variable",
                    options=[
                        {"label": col, "value": col} for col in list(df.columns)
                    ],
                    value="Material Group",
                ),dcc.Dropdown(id='Unique-values',multi=True,value=[]),
                dbc.Label("Y variable"),
                dcc.Dropdown(
                    id="y-variable",
                    options=[
                        {"label": col, "value": col} for col in list(df.columns)
                    ],
                    value="Instance",
                ),dcc.Dropdown(id='Unique-values1',multi=True,value=[]),
            ],
            vertical=True,
            pills=True,
        ),
    ],
    style=SIDEBAR_STYLE,
)
print
@app.callback(
    Output('Unique-values', 'options'),
    Input('x-variable', 'value'))
def set_cities_options(unique_values):
    return [{'label': i, 'value': i} for i in df[unique_values].unique()]

@app.callback(
    Output('Unique-values1', 'options'),
    Input('y-variable', 'value'))
def set_cities_options(unique_values):
    return [{'label': i, 'value': i} for i in df[unique_values].unique()]

@app.callback(
    Output('Unique-values', 'value'),
    Input('Unique-values', 'options'))
def set_unique_value(available_options):
    return  [
        str(o for o in available_options if available_options in o["label"] or o["value"] in (value or []))
    ]
@app.callback(
    Output('Unique-values1', 'value'),
    Input('Unique-values1', 'options'))
def set_cities_value(available_options1):
    return [
        str(o for o in available_options1 if available_options1 in o["label"] or o["value"] in (value or []))
    ]




content = html.Div(id="page-content", style=CONTENT_STYLE)
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])


@app.callback(Output("page-content", "children"),  Input("x-variable", "value"),
        Input("y-variable", "value"),Input('demo-dropdown', 'value'),Input('Unique-values','value'),Input('Unique-values1','value'))


def render_tab_content(x,y,value,x_unique,y_unique):
    layout = {"xaxis": {"title": x}, "yaxis": {"title": y}}
    data=df.loc[df[x].isin([x_unique]),:]

    fig=px.bar(data,x=x,y=y,color=x)
    if value == "Bar Chart":
        return dcc.Graph(config=config,figure=fig)

    elif value == 'Scatter':
        return dcc.Graph(config=config,figure=px.scatter(data,x=x,y=y,color=x))
    elif value == 'Pie Chart':
        return dcc.Graph(config=config,figure=px.pie(data,values=x,names=y))


    else:

        return "No tab selected"



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

I am getting error like this


Callback error updating page-content.children

KeyError: 'Material Group'


File "C:\Users\gopik\OneDrive\Desktop\unique_axis.py", line 137, in render_tab_content
fig=px.bar(data,x=x,y=y,color=x)
File "C:\Users\gopik\anaconda3\Lib\site-packages\plotly\express\_chart_types.py", line 350, in bar
return make_figure(
File "C:\Users\gopik\anaconda3\Lib\site-packages\plotly\express\_core.py", line 1861, in make_figure
for val in sorted_group_values[m.grouper]:
KeyError: 'Material Group'


  [1]: https://i.stack.imgur.com/HfyLY.png

标签: pythonplotly-dash

解决方案


推荐阅读