我想创建一个div,在其中通过在仪表板中选择年份(在滑块中)来调用今年和前一年的销售额值,但它给了我一个错误。可以输出多个值吗?我做错了什么?
html.Div([
html.Div(id = 'text1'),
html.Div(id = 'text2'),
],className='create_container2 three columns')
@app.callback(Output('text1', 'children'),
Output('text2','children')
[Input('select_years', 'value')],)
def update_graph(select_years):
sales8=df.groupby(['Year'])['Sales'].sum().reset_index()
current_year = sales8[(sales8['Year'] == select_years)]['Sales'].sum()
sales9=df.groupby('Year')['Sales'].sum().reset_index()
sales9['PY']=sales9['Sales'].shift(1)
previous_year=sales9[(sales9['Year']==select_years)]['PY'].sum()
return [
html.H6(children='Current Year',id='text1',
style={'textAlign': 'center',
'color': 'white'}),
html.P('${0:,.2f}'.format(current_year),
style={'textAlign': 'center',
'color': 'black',
'fontSize': 15,
'margin-top': '-10px'}),
html.H6(children='Current Year',id='text2',
style={'textAlign': 'center',
'color': 'white'}),
html.P('${0:,.2f}'.format(previous_year),
style={'textAlign': 'center',
'color': 'black',
'fontSize': 15,
'margin-top': '-10px'}),
]
发布于 2021-09-23 07:15:02
我认为您的代码中有一些错误
请看下面的代码,我没有测试它,所以我可能会遗漏一些东西
html.Div([
html.Div(id = 'text1'),
html.Div(id = 'text2'),
],className='create_container2 three columns')
@app.callback([Output('text1', 'children'),
Output('text2','children')],
[Input('select_years', 'value')],)
def update_graph(select_years):
sales8=df.groupby(['Year'])['Sales'].sum().reset_index()
current_year = sales8[(sales8['Year'] == select_years)]['Sales'].sum()
sales9=df.groupby('Year')['Sales'].sum().reset_index()
sales9['PY']=sales9['Sales'].shift(1)
previous_year=sales9[(sales9['Year']==select_years)]['PY'].sum()
return [ #text 1 children
html.H6(children='Current Year',id='text1',
style={'textAlign': 'center',
'color': 'white'}),
html.P('${0:,.2f}'.format(current_year),
style={'textAlign': 'center',
'color': 'black',
'fontSize': 15,
'margin-top': '-10px'})],
[html.H6(children='Current Year',id='text2', #text 2 children
style={'textAlign': 'center',
'color': 'white'}),
html.P('${0:,.2f}'.format(previous_year),
style={'textAlign': 'center',
'color': 'black',
'fontSize': 15,
'margin-top': '-10px'}),
]
https://stackoverflow.com/questions/69294376
复制相似问题