开发者社区> 问答> 正文

如何使用回调添加到一个字符串存储在一个Div使用破折号在Python?

我在Python中使用dash创建一个仪表板。我想要更新一个存储在一个Div中的字符串,每当一个新的字符串作为另一个仪表板元素/进程的一部分生成时。我最好的尝试是创建一个回调到这个是不成功的,但打算工作: 这是回调代码:

@Dashboard.callback(
                    Output('output_div', 'children'),
                    [Input('new_string', 'children')],
                    [State('output_div', 'children')]
                   )
def create_complete_string(new_string, string_in_output_div_so_far):
    if string_in_output_div_so_far is None:
       return new_string
    else:
       return string_in_output_div_so_far + new_string

这并不像期望的那样工作,因为它只在output_div中输出new_string的一个实例(生成的多个实例中的最后一个),而是将它们连接在一起。 谁能指出我做错了什么吗? 谢谢:) 问题来源StackOverflow 地址:/questions/59382089/how-to-use-a-callback-to-add-to-a-string-stored-in-a-div-using-dash-in-python

展开
收起
kun坤 2019-12-27 17:18:44 392 0
1 条回答
写回答
取消 提交回答
  • 对于给定的代码段,很难找出错误,如果可以将最小的复制器粘贴为代码段,那就更好了;相反,这里是一个最小的工作代码,以满足;

    import dash
    from dash.dependencies import Input, Output, State
    import dash_core_components as dcc
    import dash_html_components as html
    
    app = dash.Dash(__name__)
    
    app.layout = html.Div([
                dcc.Input(id="text"),
                html.Div(id='new_string'),
                html.Div(id='output_div'),
        ])
    
    
    @app.callback(
                Output('new_string', 'children'),
                [Input('text', 'value')]
            )
    def output_text(text):
        return text
    
    2019-12-27 17:18:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载