如何使用python在Dash中单击按钮时使用数据填充图表

如何使用python在Dash中单击按钮时使用数据填充图表,python,plotly-dash,Python,Plotly Dash,我正在试验短跑/计谋。我不知道如何用点击按钮的数据填充图表。我知道,如果我将回调返回的相同内容直接传递到应用程序布局中的figure参数,图表就会工作。我知道我可以通过回调来更改标题的文本。我点击提交按钮,图表没有改变。这是我的密码: app = dash.Dash(__name__, assets_external_path=assets) app.layout = html.Div(children=[ html.Div(className='parent', children=

我正在试验短跑/计谋。我不知道如何用点击按钮的数据填充图表。我知道,如果我将回调返回的相同内容直接传递到应用程序布局中的figure参数,图表就会工作。我知道我可以通过回调来更改标题的文本。我点击提交按钮,图表没有改变。这是我的密码:

app = dash.Dash(__name__, assets_external_path=assets)

app.layout = html.Div(children=[

    html.Div(className='parent', children=[

        html.Div(className='pD', children=[

           dcc.Dropdown(id='size', options=[
                {'label': 'Small', 'value': 'small'},
                {'label': 'Medium', 'value': 'medium'},
                {'label': 'Large', 'value': 'large'}
            ])

        ]),

        html.Div(className='submit', children=[

            html.Button('Submit', id='submit', n_clicks=0)

        ]),

        html.Div(className='graph_box cD', children=[dcc.Graph(id='graph')])

    ]),

])

@app.callback(
    Output('graph', 'figure'),
    [Input('submit', 'n_clicks')],
    [State('size', 'value')]
)
def update_chart(clicks, size):
    if clicks > 1:        
        return {'data': [{'x': np.random.randint(0, 100, 1000), 'type': 'histogram'}]}

app.run_server(debug=False, port=8000)

我让它进行以下更改:

使用空的
图初始化图形

dcc.Graph(
id='graph',
图={}
)])
并更改回调,使其永远不会返回
None
,但至少会为图形元素返回一个空的
figure
。将条件更改为使用
单击
,作为简单的真实条件也会有所帮助

def更新图表(点击次数、大小):
如果单击:
返回{'data':[{'x':np.random.randint(011001000),'type':'histogram'}]}
其他:
返回{'data':[],'type':'histogram'}

我让它处理以下更改:

使用空的
图初始化图形

dcc.Graph(
id='graph',
图={}
)])
并更改回调,使其永远不会返回
None
,但至少会为图形元素返回一个空的
figure
。将条件更改为使用
单击
,作为简单的真实条件也会有所帮助

def更新图表(点击次数、大小):
如果单击:
返回{'data':[{'x':np.random.randint(011001000),'type':'histogram'}]}
其他:
返回{'data':[],'type':'histogram'}