【问题标题】:Print output based on user input in dash (or shiny)基于破折号(或闪亮)中的用户输入打印输出
【发布时间】:2018-06-14 23:58:55
【问题描述】:

我想从用户对文本框的输入中获取输入 xy

if x + 2*y 3*x*y > 100:
    print('Blurb 1')
else:
    print('Blurb 2')

这似乎与回调等令人费解,尽管它可能是独立的并且非常简单。有没有一种简单的方法可以在 Web 应用程序中执行此操作?我发现的其他资源似乎假设了一个更复杂的目标,所以我很好奇代码可以削减多少。

【问题讨论】:

    标签: python flask shiny plotly-dash


    【解决方案1】:

    我认为不定义回调就无法完成任务,但完成工作的代码非常简短。可能的解决方案如下:

    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    from dash.dependencies import Input, Output
    
    app = dash.Dash()
    
    app.layout = html.Div([
        html.H1("Simple input example"),
        dcc.Input(
            id='input-x',
            placeholder='Insert x value',
            type='number',
            value='',
        ),
        dcc.Input(
            id='input-y',
            placeholder='Insert y value',
            type='number',
            value='',
        ),
        html.Br(),
        html.Br(),
        html.Div(id='result')
        ])
    
    
    @app.callback(
        Output('result', 'children'),
        [Input('input-x', 'value'),
         Input('input-y', 'value')]
    )
    def update_result(x, y):
        return "The sum is: {}".format(x + y)
    
    
    if __name__ == '__main__':
            app.run_server(host='0.0.0.0', debug=True, port=50800)
    

    这是你得到的:

    每次两个输入框之一更改其值时,总和的值都会更新。

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2015-05-06
      • 2014-02-26
      • 1970-01-01
      • 2021-09-13
      • 2018-05-19
      • 2016-12-09
      • 1970-01-01
      相关资源
      最近更新 更多