'Specify different filename for newplot.png in Dash dynamically

I am trying change the filename from the image file. I managed to change it from newplot.png to Plot.png, but I want to make it dynamically. In my app I added a drag & drop feature and I would like to use this filename as filename for the plot.

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.layout = html.Div([
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '80vh',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    dcc.Graph(id='Mygraph',
              style={'width': '80vh', 'height': '50vh'},
              config={'scrollZoom': True, 'toImageButtonOptions': {'filename': 'Plot'}}),
    html.Br(),
    dcc.Checklist(
        id='relative_temperature',
        options=['Relative']),
    html.Br(),
    dcc.Input(id='limit_line', type="number", value=45),
    html.Br(),
    html.Div(id='output-data-upload')
])


Solution 1:[1]

You can try this:

import sys
import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output
import plotly.express as px


external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']


app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout = html.Div([
    dcc.Upload(
        id='upload-data',
        children=html.Div([
            'Drag and Drop or ',
            html.A('Select Files')
        ]),
        style={
            'width': '80vh',
            'height': '60px',
            'lineHeight': '60px',
            'borderWidth': '1px',
            'borderStyle': 'dashed',
            'borderRadius': '5px',
            'textAlign': 'center',
            'margin': '10px'
        },
        # Allow multiple files to be uploaded
        multiple=True
    ),
    html.Div(id='graph'),
    html.Br(),
    dcc.Checklist(
        id='relative_temperature',
        options=['Relative']),
    html.Br(),
    dcc.Input(id='limit_line', type="number", value=45),
    html.Br(),
    html.Div(id='output-data-upload')
])


@app.callback(Output('graph', 'children'),
              [Input('upload-data', 'filename')])
def update_figure(name):
   
        
    return dcc.Graph(id='Mygraph', style={'width': '80vh', 'height': '50vh'},
                  config={'scrollZoom': True, 'toImageButtonOptions': {'filename': name[-1].split('.')[0]}})
    

if __name__ == '__main__':
    app.run_server(debug=False, use_reloader=False)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phoenix