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

lar*_*sse 1 python flask shiny plotly-dash

我想获取输入xy从用户输入到文本框。

if x + 2*y 3*x*y > 100:
    print('Blurb 1')
else:
    print('Blurb 2')
Run Code Online (Sandbox Code Playgroud)

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

小智 6

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

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)
Run Code Online (Sandbox Code Playgroud)

这是你得到的: 在此处输入图片说明

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