Python 如何将在Dash中触发回调的按钮与在同一按钮单击时触发的其他回调同步
我有一个Dash应用程序,我试图通过一个按钮触发回调来重新创建它。我只能让我的Python 如何将在Dash中触发回调的按钮与在同一按钮单击时触发的其他回调同步,python,plotly-dash,Python,Plotly Dash,我有一个Dash应用程序,我试图通过一个按钮触发回调来重新创建它。我只能让我的n_单击为第一次单击触发用户输入值。这是唯一一次在整个回调过程中同步按钮。我理解为什么,因为在我的第二次回调中,tickers()(图一)中,n\u clicks的值将始终保持True 有哪些方法可以让我的按钮在每次回调时同步并启动。在我的仪表板中发生的事情是,在第一次单击之后,用户在输入中输入的每个值都会触发我的回调,完全忽略我的n_单击 我如何解决这个问题 # My input dropdown that gets
n_单击
为第一次单击触发用户输入值。这是唯一一次在整个回调过程中同步按钮。我理解为什么,因为在我的第二次回调中,tickers()
(图一)中,n\u clicks
的值将始终保持True
有哪些方法可以让我的按钮在每次回调时同步并启动。在我的仪表板中发生的事情是,在第一次单击之后,用户在输入中输入的每个值都会触发我的回调,完全忽略我的n_单击
我如何解决这个问题
# My input dropdown that gets fired off thanks to n_clicks input
@app.callback(
[Output(component_id='input-warning', component_property='children'),
Output(component_id='dynamic-dropdown',component_property='options')],
[Input('button', 'n_clicks')],
state=[State(component_id='dynamic-dropdown', component_property='value')]
)
def update_output_div(n_clicks, value):
if n_clicks:
n_clicks += 1
if value is None:
return [[], OPTIONS]
elif len(value) >= 4:
children = html.P("limit reached...",id='input-warning')
return [children, [o for o in OPTIONS if o["value"] in value]]
else:
# Make sure that the set values are in the option list, else they will disappear
# from the shown select list, but still part of the `value`.
return [
o for o in OPTIONS if value.upper() in o["label"].upper() or o["value"] in (value or [])
]
else:
conn.rollback()
return PreventUpdate
# My other callback
@app.callback(
Output('graph', 'children'),
[Input('button', 'n_clicks'),
Input('dynamic-dropdown', 'value')],
[State('graph','children')])
def tickers(n_clicks,value, children):
if n_clicks:
conn.rollback()
if value is None:
return [[], OPTIONS]
else:
stock_info = {}
d = {} #dates
p = {} #prices
sym_ids = tuple([id for id in value])
stock_info = {}
stock_info = get_dict_resultset("SELECT symbol, date, adj_close FROM api.security_price WHERE security_price.symbol IN %s AND date > (SELECT MAX(date) FROM api.security_price) - interval '1 years' ORDER by date;", [sym_ids])
stock_data_by_symbol = defaultdict(list)
for entry in stock_info:
symbol = entry['symbol']
stock_data_by_symbol[symbol].append(entry)
trace = []
for stock in value:
d[stock] = [rec['date'] for rec in stock_data_by_symbol[stock]]
p[stock] = [rec['adj_close'] for rec in stock_data_by_symbol[stock]]
trace.append(go.Scatter(x=d[stock],
y=p[stock],
mode='lines',
text = d[stock],
opacity=0.7,
name=stock,
textposition='bottom center'))
traces = [trace]
data = [val for sublist in traces for val in sublist]
figure = {'data': data,
'layout': go.Layout(
colorway=["#9b5de5", '#00f5d4', '#FFD23F', '#f15bb5', '#f71735', '#d08c60'],
paper_bgcolor='rgba(0, 0, 0, 0)',
plot_bgcolor='rgba(0, 0, 0, 0)',
margin={
'l': 40, # left margin, in px
'r': 10, # right margin, in px
't': 16, # top margin, in px
'b': 30,}, # bottom margin, in px
hovermode='x',
legend={"x" : 0, "y" : 1, 'font': {'size': 10}},
xaxis={'rangeselector': {'buttons': list([
{'count': 1, 'label': '1M',
'step': 'month',
'stepmode': 'backward'},
{'count': 3, 'label': '3M',
'step': 'month',
'stepmode': 'backward'},
{'count': 6, 'label': '6M',
'step': 'month',
'stepmode': 'backward'},
{'count': 1, 'label': 'YTD',
'step': 'year',
'stepmode': 'todate'},
{'step': 'all','label':'MAX'},
])},
'rangeslider': {'visible': True}, 'type': 'date'},
),
}
children = [
dbc.Row(dbc.Col(html.P("As of {}, {}".format(d[stock][-1].strftime('%A'),d[stock][-1].strftime('%d %m-%Y')),style={'font-size':'12px','font_family':'Helvetica Neue', 'margin-top':'10px','color':'#ABABAB'}),style={'position':'relative','float':'right','margin-right':'40px'}, width={"offset": 3}), justify='end'),
html.Div(style={"width":"100%", "height":'30px'}),
html.H4('Historical Price Chart:', style={'color':'#474747','margin-left':'30px','text-align':'left'}),
html.P('Interactive graph for end-of-day prices', style={'color':'#A2A2A2','margin-left':'30px','text-align':'left'}),
dcc.Graph(id='output-graph', figure=figure ,config={'displayModeBar': False}, animate=True,style = {'width': '90%', 'touch-action':'none'}),
html.P('* Drag sliders to create your own custom date range', style={'font-size':'12px','color':'#A2A2A2','margin-left':'40px','text-align':'left'}),
html.P('** Double-tap or double-click the main graph to reset axis', style={'font-size':'12px','color':'#A2A2A2','margin-left':'40px','text-align':'left'}),
html.Div(style={"width":"100%", "height":'10px'}),
]
return children