【发布时间】:2020-12-31 13:39:04
【问题描述】:
我有两个来自下拉菜单的回调,它们应该指向一个数据表。有一个由入口代码、汽车类型和年份组成的数据库。下拉菜单将允许您选择汽车,然后选择年份,生成的数据表应该是所有所选汽车和所有比所选年份更新的汽车的列表。
external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]
app = dash.Dash(name, external_stylesheets=external_stylesheets)
dataa = [[‘ABQ’,‘Toyota’,2015],[‘QRC’,‘Honda’,2016],[‘BFG’,‘Honda’,2017],[‘AAA’,‘Toyota’,2018],[‘KLX’,‘Toyota’,2020]]
dframe = pd.DataFrame(dataa,columns=[‘Entry’,‘Car’, ‘Year’])
app.layout = html.Div([
html.Label("Name:", style={'fontSize':70, 'textAlign':'center'}),
dcc.Dropdown(
id='cars-dpdn',
options=[{'label': s, 'value': s} for s in sorted(dframe.Car.unique())],
clearable=False
),
html.Label("Year", style={'fontSize':30, 'textAlign':'center'}),
dcc.Dropdown(
id='years-dpdn',
options=[],
),
html.Label("Results:", style={'fontSize':70, 'textAlign':'center'}),
html.Div(id="table1")
])
@app.callback(
Output(‘years-dpdn’, ‘options’),
Input(‘cars-dpdn’, ‘value’)
)
def years(chosen_car):
dff = dframe[dframe.Car==chosen_car]
return [{‘label’: c, ‘value’: c} for c in sorted(dff.Year.unique())]
@app.callback(
Output(‘years-dpdn’, ‘value’),
Input(‘years-dpdn’, ‘options’)
)
def years_value(available_options):
return [x[‘value’] for x in available_options]
@app.callback(
Output(‘table1’, ‘figure’),
Input(‘cars-dpdn’, ‘value’),
Input(‘years-dpdn’, ‘value’),
)
def update_table(car, year):
dfa = dframe[dframe['Car']==car]
dfb = dfa[dfa['Year']>year]
return html.Div([dash_table.DataTable(
data=dfb.to_dict('rows'),
columns=[{'name': i, 'id': i} for i in dfb.columns],
),
html.Hr()
])
if name == ‘main’:
app.run_server(debug=False, port = 3060)
下拉菜单功能正常,允许我选择 Car 和 Year 值,但是没有创建表。它只是空白。错误信息很冗长,但这里是结尾:
ValueError: Lengths must match to compare
127.0.0.1 - - [30/Dec/2020 20:45:00] “POST /_dash-update-component HTTP/1.1” 500 -
127.0.0.1 - - [30/Dec/2020 20:45:00] “POST /_dash-update-component HTTP/1.1” 200 -
127.0.0.1 - - [30/Dec/2020 20:45:02] “POST /_dash-update-component HTTP/1.1” 200 -
【问题讨论】:
标签: python callback plotly-dash