Dash 中的多输入到单输出

Multi input to single output in 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('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)

您应该能够通过提交按钮触发回调,方法是将提交按钮用作输入并将值用作状态,如下所示:

@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)