Plotly Dash 按钮回调函数
Plotly Dash Button Callback function
我创建了2个按钮,所以当我按下其中一个按钮时,会出现一张地图(都是不同的地图)但是,两个按钮最终显示的是同一张地图,看来我的回调函数不是在职的。我不太熟悉回调函数,但这是我的代码。任何帮助将不胜感激!
import plotly.graph_objects as go
fig1_1 = go.Figure()
fig1_2 = go.Figure()
fig1_1 = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
marker = { 'size': 10, 'color': "orange" }))
fig1_1.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
fig1_2 = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [-60, -90, -50, -60], lat = [46, 57, 55, 45],
marker = { 'size': 10, 'color': "yellow" }))
fig1_2.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -50, 'lat': 50 },
'zoom': 5},
showlegend = False)
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
app = dash.Dash()
app.layout = html.Div([
html.Button('Graph1', id='button', n_clicks=0),
html.Button('Graph2',id='button2',n_clicks=0),
dcc.Graph(id='graph',figure={})
])
@app.callback(
Output('graph', 'figure'),
Input('button', 'n_clicks'),
Input('button2', 'n_clicks'))
def clicked_output(button,button2):
if button == None:
raise PreventUpdate
return fig1_1
elif button2 == None:
raise PreventUpdate
return fig1_2
if __name__ == '__main__':
app.run_server(debug=True)
你的代码有一些错误。
您在 Figure 对象内部发送图形。应该通过add_trace.
发送
我添加了捕捉按钮点击的行
试试这个代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
first = go.Scattermapbox(fill = "toself",
lon = [-74, -70, -70, -74],
lat = [47, 47, 45, 45],
marker = { 'size': 10, 'color': "orange" })
first_l = dict(mapbox = {'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
second = go.Scattermapbox(fill = "toself",
lon = [-60, -90, -50, -60],
lat = [46, 57, 55, 45],
marker = { 'size': 10, 'color': "yellow" })
second_l = dict(mapbox = {'style': "stamen-terrain",
'center': {'lon': -50, 'lat': 50 },
'zoom': 5},
showlegend = False)
app = dash.Dash()
app.layout = html.Div([
html.Button('Graph1', id='button1', n_clicks=0),
html.Button('Graph2', id='button2', n_clicks=0),
dcc.Graph(id='graph')
])
@app.callback(
Output('graph', 'figure'),
Input('button1', 'n_clicks'),
Input('button2', 'n_clicks'))
def clicked_output(button,button2):
fig = go.Figure()
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'button1' in changed_id:
fig.add_trace(first)
fig.update_layout(first_l)
elif 'button2' in changed_id:
fig.add_trace(second)
fig.update_layout(second_l)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
我创建了2个按钮,所以当我按下其中一个按钮时,会出现一张地图(都是不同的地图)但是,两个按钮最终显示的是同一张地图,看来我的回调函数不是在职的。我不太熟悉回调函数,但这是我的代码。任何帮助将不胜感激!
import plotly.graph_objects as go
fig1_1 = go.Figure()
fig1_2 = go.Figure()
fig1_1 = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [-74, -70, -70, -74], lat = [47, 47, 45, 45],
marker = { 'size': 10, 'color': "orange" }))
fig1_1.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
fig1_2 = go.Figure(go.Scattermapbox(
fill = "toself",
lon = [-60, -90, -50, -60], lat = [46, 57, 55, 45],
marker = { 'size': 10, 'color': "yellow" }))
fig1_2.update_layout(
mapbox = {
'style': "stamen-terrain",
'center': {'lon': -50, 'lat': 50 },
'zoom': 5},
showlegend = False)
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
app = dash.Dash()
app.layout = html.Div([
html.Button('Graph1', id='button', n_clicks=0),
html.Button('Graph2',id='button2',n_clicks=0),
dcc.Graph(id='graph',figure={})
])
@app.callback(
Output('graph', 'figure'),
Input('button', 'n_clicks'),
Input('button2', 'n_clicks'))
def clicked_output(button,button2):
if button == None:
raise PreventUpdate
return fig1_1
elif button2 == None:
raise PreventUpdate
return fig1_2
if __name__ == '__main__':
app.run_server(debug=True)
你的代码有一些错误。
您在 Figure 对象内部发送图形。应该通过add_trace.
发送
我添加了捕捉按钮点击的行
试试这个代码:
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objects as go
first = go.Scattermapbox(fill = "toself",
lon = [-74, -70, -70, -74],
lat = [47, 47, 45, 45],
marker = { 'size': 10, 'color': "orange" })
first_l = dict(mapbox = {'style': "stamen-terrain",
'center': {'lon': -73, 'lat': 46 },
'zoom': 5},
showlegend = False)
second = go.Scattermapbox(fill = "toself",
lon = [-60, -90, -50, -60],
lat = [46, 57, 55, 45],
marker = { 'size': 10, 'color': "yellow" })
second_l = dict(mapbox = {'style': "stamen-terrain",
'center': {'lon': -50, 'lat': 50 },
'zoom': 5},
showlegend = False)
app = dash.Dash()
app.layout = html.Div([
html.Button('Graph1', id='button1', n_clicks=0),
html.Button('Graph2', id='button2', n_clicks=0),
dcc.Graph(id='graph')
])
@app.callback(
Output('graph', 'figure'),
Input('button1', 'n_clicks'),
Input('button2', 'n_clicks'))
def clicked_output(button,button2):
fig = go.Figure()
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if 'button1' in changed_id:
fig.add_trace(first)
fig.update_layout(first_l)
elif 'button2' in changed_id:
fig.add_trace(second)
fig.update_layout(second_l)
return fig
if __name__ == '__main__':
app.run_server(debug=True)