选择下拉值时图形不更新

时间:2019-03-30 23:13:31

标签: python plotly-dash

我无法弄清楚为什么我的图形不使用我选择的值来更新数据。可以请人帮忙。

我已经尝试了很多次重写,但是我似乎无法理解我在应用回调之后必须编写的函数以及该函数应该返回的函数。

attr_values = [-2, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8]
children_arr = [None]*5
for j in range(5):  #create child node
    children_arr[j] = treeNode()
    f = lambda y: y==attr_values[j]
    children_arr[j].function=f
    print(children_arr[j].function(-2))

for k in range(5):
    print(children_arr[k].function(-2))

2 个答案:

答案 0 :(得分:0)

您可以尝试创建div,然后用所需的内容填充他。

代码:

from flask import Flask
import dash
import dash_core_components as dcc
import dash_html_components as html
# if you import in a such way - you can call Input, Output directly
from dash.dependencies import Input, Output
# Create app
app = dash.Dash()
# Specify layout
app.layout = html.Div(children=[
    html.H1('Sports PA Dashboard'),
    dcc.Dropdown(
        id='sport_dropdown',
        options=[{'label': i, 'value': i} for i in df.Sport.unique()],
        value=df.Sport.unique()),
    # Create empty Div container, which you can fill with everything!
    html.Div(id='your-plot-here')
])


@app.callback(
    # If you stuck to what those values belongs to,
    # you can specify them more in details:
    # Output(component_id='your_plot_here', component_property='children'),
    # instead of:
    Output('your_plot_here', 'children'),
    [Input('sport_dropdown', 'value')])
def update_output(selected_sport):
    """This function create age_vs_rank graph."""
    # selected_sport == value property from sport_dropdown id
    print(selected_sport)
    sport = df[df.Sport == selected_sport]
    rank = sport['Rank']
    age = sport['Age']
    # return what you want in empty Div container with id 'your_plot_here'
    return dcc.Graph(id='age_vs_rank',
                     figure={'data': [
                                     {'x': rank, 'y': age,
                                      'type': 'bar', 'name': 'age_vs_rank'}
                                      ],
                             'layout': {
                                        'title': 'Age vs Rank'
                                        }
                             }
                     )


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

答案 1 :(得分:0)

尝试将return语句更改为update_output函数。 因此它应该看起来像

@app.callback(
# If you stuck to what those values belongs to,
# you can specify them more in details:
# Output(component_id='your_plot_here', component_property='children'),
# instead of:
Output('your_plot_here', 'children'),
[Input('sport_dropdown', 'value')])

def update_output(selected_sport):
   sport = df[df.Sport == selected_sport]
   rank = sport['Rank']
   age = sport['Age']
   return figure = {'data': #stuff
                 'layout':#stuff
                }

仅添加数字字典,因为那是唯一要更新的值。

对不起,如果格式看起来很奇怪,这是我的第一个StackOverflow答案:)