根据破折号(或闪亮)中的用户输入打印输出
Print output based on user input in dash (or shiny)
我想从用户对文本框的输入中获取输入 x
和 y
。
if x + 2*y 3*x*y > 100:
print('Blurb 1')
else:
print('Blurb 2')
尽管它可以是独立的并且非常简单,但它似乎与回调等一起错综复杂。有没有一种简单的方法可以在网络应用程序中执行此操作?我发现的其他资源似乎假设了一个复杂得多的目标,所以我很好奇可以削减多少代码。
我认为不定义回调就无法完成任务,但完成工作的代码非常简短。可能的解决方案如下:
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)
这就是你得到的:
每当两个输入框之一更改其值时,总和的值就会更新。
我想从用户对文本框的输入中获取输入 x
和 y
。
if x + 2*y 3*x*y > 100:
print('Blurb 1')
else:
print('Blurb 2')
尽管它可以是独立的并且非常简单,但它似乎与回调等一起错综复杂。有没有一种简单的方法可以在网络应用程序中执行此操作?我发现的其他资源似乎假设了一个复杂得多的目标,所以我很好奇可以削减多少代码。
我认为不定义回调就无法完成任务,但完成工作的代码非常简短。可能的解决方案如下:
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)
这就是你得到的:
每当两个输入框之一更改其值时,总和的值就会更新。