根据破折号(或闪亮)中的用户输入打印输出

时间:2018-06-14 23:58:55

标签: python flask shiny plotly-dash

我希望从用户输入到文本框获取输入xy

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

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

1 个答案:

答案 0 :(得分: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)

这是你得到的: enter image description here

每当两个输入框中的一个更改其值时,就会更新总和的值。

相关问题