Dash中的多页应用程序-其他页面无法识别的下拉值

时间:2019-04-24 04:31:32

标签: javascript python html css plotly-dash

当我移到另一页时,下拉值不会被拉入并显示。

有人可以帮忙吗?

我尝试遵循在线破折号文档(https://dash.plot.ly/urls)和本文(Multi-Page Dash Application),但没有成功。

import flask
import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_auth


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.config['suppress_callback_exceptions']=True

url_bar_and_content_div = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])


index_page = html.Div([
dcc.Link('Go to Questionnaire', href='/questionnaire'),
])


intro_page_layout = html.Div([
    dcc.Store(id='memory-ouput', storage_type='memory'),
    html.Div([
                html.Div(

                    html.H6("""Select your current industry""",
                            style={'margin-right': '2em'})
            ),

            dcc.Dropdown(
                id='business_area_dropdown',
                options=[
                    {'label': 'Academia', 'value': 'academia'},
                    {'label': 'Energy', 'value': 'energy'},
                    {'label': 'Research', 'value': 'research'}
                ],
                placeholder="Select Business Area",
                style=dict(
                    width='40%',
                    verticalAlign="middle"
                )
            )],
        style=dict(display='flex')
    ),

    html.Div([
            html.Div(
                    html.H6("""Are you happy where you are?""",
                            style={'margin-right': '2em'})
            ),

            dcc.Dropdown(
                id='search_preference',
                options=[
                    {'label': 'Yes', 'value': 'yes'},
                    {'label': 'No', 'value': 'no'}
                ],
                placeholder="Select Answer",
                style=dict(
                    width='40%',
                    display='inline-block',
                    verticalAlign="middle"
                )
            )],

        style=dict(display='flex')
    ), 

    dcc.Link(
    html.Button('Submit', type='submit', id='submit-button', style={'margin-bottom': '5em', 'width': '110px', 'height':'50px', 'padding': '10px', 'font-weight':'bold'}),
    href='/summary')

    ])


summary_page_layout = html.Div([
    html.H1('Summary Page'),
    html.Div(id='results-page-content')
    ])


def serve_layout():
    if flask.has_request_context():
        return url_bar_and_content_div
    return html.Div([
        url_bar_and_content_div,
        index_page,
        intro_page_layout,
        summary_page_layout
        ])


app.layout = serve_layout


@app.callback(Output('memory-output', 'data'), [Input('submit-button', 'n_clicks')], [State('search_preference', 'value'), State('business_area_dropdown', 'value')])
def on_click(n_clicks, input1, input2):
    if (n_clicks < 1 or n_clicks is None) and input1 is not None and input2 is not None:
        raise PreventUpdate

    return data

@app.callback(Output('results-page-content', 'children'), [Input('memory-output', 'data')], [State('search_preference', 'value'), State('business_area_dropdown', 'value')])
def on_display(data, input1, input2):
    if data is None:
        raise PreventUpdate
    return html.H3('Results are ' + input1 + ' and ' + input2)


@app.callback(Output('page-content', 'children'),[Input('url', 'pathname')])
def display_page(pathname):
    if pathname == '/questionnaire':
        return intro_page_layout
    elif pathname == '/summary':
        return summary_page_layout
    else:
        return index_page

if __name__ == '__main__':
    app.run_server(debug=True)

在摘要页面上,我应该具有用户从问卷调查页面中选择的值。奇怪的是,这些值并未显示在摘要页面上。

任何帮助或指针,将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

您需要将下拉列表值存储在磁盘上,flask.session对象或dcc.Store组件中。切换页面时,Dash不会保留其他页面的输入值。