首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Dash: Output('text2',‘TypeError’)输出:'Output‘对象不可订阅

Dash: Output('text2',‘TypeError’)输出:'Output‘对象不可订阅
EN

Stack Overflow用户
提问于 2021-09-23 05:16:36
回答 1查看 169关注 0票数 0

我想创建一个div,在其中通过在仪表板中选择年份(在滑块中)来调用今年和前一年的销售额值,但它给了我一个错误。可以输出多个值吗?我做错了什么?

Error picture

代码语言:javascript
运行
复制
 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'}),
    

]
EN

回答 1

Stack Overflow用户

发布于 2021-09-23 07:15:02

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

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

请看下面的代码,我没有测试它,所以我可能会遗漏一些东西

代码语言:javascript
运行
复制
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'}),
]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69294376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档