简体   繁体   English

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

[英]Print output based on user input in dash (or shiny)

I would like to get inputs x and y from a user's input to a text box.我想从用户的输入中获取输入xy到文本框。

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

This seems to be convoluted in dash with callbacks, etc, even though it could be self-contained and quite simple.这似乎与回调等混杂在一起,即使它可以是独立的并且非常简单。 Is there a simple way to do this within a web app?有没有一种简单的方法可以在 Web 应用程序中执行此操作? Other resources I found seem to assume a much more complex goal, so I am curious as to how much the code can be pared.我发现的其他资源似乎假设了一个更复杂的目标,所以我很好奇可以削减多少代码。

I don't think you can accomplish your task without defining a callback, but the code to get the work done is very short and simple.我认为不定义回调就无法完成任务,但是完成工作的代码非常简短。 A possible solution could be the following:可能的解决方案如下:

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)

This is what you get:这是你得到的: 在此处输入图片说明

The value of the sum is updated every time one of the two input boxes changes its values.每当两个输入框之一更改其值时,总和的值就会更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM