在 Dash 中为 newplot.png 动态指定不同的文件名

Specify different filename for newplot.png in Dash dynamically

我正在尝试更改图像文件的文件名。我设法将它从 newplot.png 更改为 Plot.png,但我想动态地进行更改。在我的应用程序中,我添加了拖放功能,我想将此文件名用作绘图的文件名。

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')
])

你可以试试这个:

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)