I have an app, where I display interactive graphs that worked pretty well.
I recently added a button that takes the graph as an input and saves it as a .svg file.
This does work, the graph gets created and saved, but it is not displayed in the app anymore.
I added the relevant sections of the code below, I really have no idea where even to start with this.
I don’t understand why the button suddenly inhibits display of the graph.
#relevant part of the app layout
dcc.Tab(label='Graph',
#calling the graph
children= [html.Div([
#graph for showing data
html.Div([html.P('Graph Display: '),
html.Div([dcc.Graph(id='migration_data', )],
id='graph_div'),
MD.plot_save_button(),
],
className= 'six columns'),
#[...]
#appcallback for graph creation.
@app.callback([Output('migration_data', 'figure'),
Output('graph_storage', 'data')],
[Input('plot_button', 'n_clicks')],
[State('graph_selector', 'value'),
State('classifier_choice', 'value'),
State('identifier_selector', 'value'),
State('timepoint_selector', 'value'),
State('data_selector', 'value'),
State('distance_filter', 'value'),
State('graph_storage', 'data'),
State('graph_reuse', 'value'),
State('flag_filter', 'value'),
State('unique_time_selector', 'value'),
State('flag_storage', 'data'),
State('track_length_selector', 'value'),
State('exclude_seen', 'value'),
State('pattern_storage', 'data')])
#[...]
#newly added callback for saving as svg.
@app.callback(Output('plot_save_button', 'children'),
[Input('plot_save_button', 'n_clicks')],
[State('migration_data', 'figure'),
State('save_path', 'value'),
State('graph_selector', 'value')],)
def download_svg(n_clicks, migration_data, save_path, graph_selector):
#print(save_path)
fig=go.Figure(migration_data)
#print(fig)
path=save_path
prefix=graph_selector
#if the path dictates a csv file, the filename will be replaced
#by an appropriate graph name
if path.endswith('.csv'):
path=AD.go_one_up(path, mode='file')
filename=prefix+'.svg'
path=os.path.join(path, filename)
#if path does not end in svg, add appropriate filename
if path.endswith('.svg')!=True:
filename=prefix+'.svg'
path=os.path.join(path, filename)
fig.write_image(path)
print('file saved under', path)
return 'Download plot as svg'