Hi everyone, I am very new to plotly, Dash and to be honest, Python as well. I am trying to create an interactive dashboard that shows a marine vessel’s lat/long along with several other factors. I have been using Plotly’s mapbox functions to do this. However, I need to be able to create an interactive version of those figures. I have shared the code below. Everything seems to be running fine but the figure that is supposed to show is not showing. There is no figure at all. Also, the dropdown options are appearing but I can only select one of them and they do not update the figure (since there is no figure).
I hope I was able to explain this well, any help would be appreciated!
BTW
- “av” is the dataframe being used
- from “fig_sng” to “return figure” is the figure being drawn
- “ship” is the value being selected in the dropdown which is used to filter the dataframe
- “ship” corresponds to a vessel code (i.e. an identifying code for the vessel)
- “df” is a copy of the dataframe according to the “ship” input
#creating the Dash App for online use
app = dash.Dash()
app.layout = html.Div([
html.H1(children = 'Voyage Tracker Function',
style = {
'textAlign' : 'center'
}),
html.Div(children = 'Using Dash to create an online display',
style = {
'textAlign' : 'center'
}),
html.Div(children = ' ',
style = {
'textAlign' : 'center'
}),
html.Div(
children=[
dcc.Dropdown(id='my-dropdown', multi=True,
options=[{'label': x, 'value': 'x'} for x in sorted (av.vessel_code.unique())],
value=["MT1", "MT2", "MT5", "ST"]),
html.Button(id='my-button', n_clicks=0, children="Show Chosen Data"),
dcc.Graph(id='graph-output', figure={}),
])
])
@app.callback(
Output(component_id='graph-ouput', component_property='figure'),
[Input(component_id='my-dropdown', component_property='value')],
# Input(component_id='vl', component_property='value'),
# Input(component_id='vr', component_property='value')],
prevent_initial_call=False
)
def update_graph(ship):
if len(ship) > 0:
print(n)
print(f"Viewing Vessel: {ship}")
print(type(ship))
df = av[av['vessel_code'].isin([ship])]
fig_sng = px.line_mapbox(fg_map,
lon=fg_map['long_sng'],
lat=fg_map['lat_sng'],
center=dict(lat=21, lon=90))
#middle ground trace
fig_mg = px.line_mapbox(fg_map,
lon=fg_map['long_mg'],
lat=fg_map['lat_mg'],
center=dict(lat=21, lon=90))
#south patch trace
fig_sp = px.line_mapbox(fg_map,
lon=fg_map['long_sp'],
lat=fg_map['lat_sp'],
center=dict(lat=21, lon=90))
#south patch trace
fig_ssp = px.line_mapbox(fg_map,
lon=fg_map['long_ssp'],
lat=fg_map['lat_ssp'],
center=dict(lat=21, lon=90))
fig = px.scatter_mapbox(df,
lon=df['long'],
lat=df['lat'],
color=df['voyage_no'],
size=df['catch'],
center=dict(lat=21, lon=90))
fig.update_layout(mapbox_style="open-street-map")
#fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0},
# mapbox=dict(
# pitch=60,
# bearing=30
# ))
fig.update_geos(lataxis_showgrid=True,
lonaxis_showgrid=True,
showrivers=True, rivercolor="Blue",
showcountries=True)
figure = go.Figure(data=fig_sng.data + fig_mg.data + fig_sp.data + fig_ssp.data + fig.data, layout = fig.layout)
return figure
elif len(ship) == 0:
raise dash.exception.PreventUpdate
if __name__ == '__main__':
app.run_server(port=6050)
. . .