我想要一个多字段输入,其中有人输入地址的元素,然后在按下提交按钮后将该地址作为完整的单个字符串返回给他们。到目前为止,我有以下内容产生了输入字段和提交按钮,但是当我输入新地址时什么也没有发生。我不确定如何做到这一点:
app = dash.Dash()
app.layout = html.Div([
html.H1("Input new address"),
dcc.Input(
id = 'Property_name',
placeholder='Property Name',
type = 'text',
value = '',
),
dcc.Input(
id = 'Stree_name',
placeholder='Street Name',
type = 'text',
value = '',
),
dcc.Input(
id = 'City',
placeholder = 'City',
type = 'text',
value = '',
),
dcc.Input(
id = 'Zip_code',
placeholder = 'Zip code',
type = 'text',
value = '',
),
dcc.Input(
id = 'Country',
placeholder = 'Country',
type = 'text',
value = '',
),
html.Button(id='Submit_address', n_clicks=0, children='Submit'),
html.Br(),
html.Div(id = 'address'),
])
@app.callback([
Output('address', 'children')],
[Input('Property_name', 'value'),
Input('Stree_name', 'value'),
Input('City', 'value'),
Input('Zip_code', 'value'),
Input('Country', 'value')
])
def update_map(n_clicks, address):
if n_clicks is None:
return dash.no_update
else:
return f"Added new address at GeoCords: {address}"
if __name__ == '__main__':
app.run_server(debug=False)
您应该能够通过使用Submit按钮作为输入并将值用作状态来通过Submit按钮触发回调,如下所示:
@app.callback(
[Output('address', 'children'],
[Input('Submit_address', 'n_clicks')],
[State('Property_name', 'value'),
State('Stree_name', 'value'),
etc.
])
def update_children(n, prop_name, stree_name, etc.):
[your function here]
没有测试代码,但这就是我在项目中所做的事情。这对您有帮助吗?
编辑:
在这里,此代码有效。您只需要编辑函数以输出所需的字符串即可。
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output, State
from dash import dash
app = dash.Dash()
app.layout = html.Div([
html.H1("Input new address"),
dcc.Input(
id='Property_name',
placeholder='Property Name',
type='text',
value='',
),
dcc.Input(
id='Stree_name',
placeholder='Street Name',
type='text',
value='',
),
dcc.Input(
id='City',
placeholder='City',
type='text',
value='',
),
dcc.Input(
id='Zip_code',
placeholder='Zip code',
type='text',
value='',
),
dcc.Input(
id='Country',
placeholder='Country',
type='text',
value='',
),
html.Button(id='Submit_address', n_clicks=0, children='Submit'),
html.Br(),
html.Div(id='address'),
])
@app.callback(
[Output('address', 'children')],
[Input('Submit_address', 'n_clicks')],
[State('Property_name', 'value'),
State('Stree_name', 'value'),
State('City', 'value'),
State('Zip_code', 'value'),
State('Country', 'value')])
def update_map(n_clicks, prop_name, street, city, zip, country):
print(prop_name)
return [html.Div(f"Added new address at GeoCords: {prop_name}")]
if __name__ == '__main__':
app.run_server(debug=False)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句