0

我想创建一个 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'}),
    

]
4

1 回答 1

0

我认为您的代码中有一些错误

  1. 如果您有超过 1 个输出,请将它们放在 [] 列表中
  2. 当您返回输出时,需要与输出列表中具有相同数量的变量(在您的情况下为 2 个 HTML 元素列表)

请参阅下面的代码我没有测试它所以我可能会错过一些东西

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'}),
]
于 2021-09-23T07:15:01.507 回答