Dash - 回调 - 从按钮启动脚本

Dash - callback - launch script from button

我正在尝试从仪表板应用程序中的按钮启动 python 脚本。我尝试使用论坛 here 上讨论的其他问题的见解,但我遇到的问题是脚本在启动 dash 应用程序时执行。应用程序启动后,我可以单击,脚本将再次执行。但是,我需要阻止第一次启动,因为这会在我的数据库中产生不需要的数据负载。
查看plotly dash documentation,问题似乎来自回调启动,在应用程序启动阶段执行回调,我试图找出不同的方法来解决这个问题,但目前无法解决。

有什么线索可以修改我现有的或创建一个新的回调以避免这个问题,我猜你们中的一些人遇到过同样的问题?

我的代码如下:

# -*- coding: utf-8 -*-
"""
Created on Mon Mar  2 10:36:21 2020

"""
import pathlib
import dash
import pandas as pd
from dash.dependencies import Input, Output, State
import dash_html_components as html
from dash.exceptions import PreventUpdate
import dash_table
import os

script_fn = 'test.py'

app = dash.Dash(__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}])

app.layout = html.Div(
                children=[html.Div((
                            html.Button('df1_Settlement 5i', id='button'),
                    html.Div(id='output-container-button',
                            children='Load new file'))
                    ),
                ],
            )

@app.callback(dash.dependencies.Output('output-container-button', 'children'),[dash.dependencies.Input('button', 'n_clicks')],prevent_initial_call=True)

def run_script_onClick(n_clicks):
    # Don't run unless the button has been pressed...
    if n_clicks is None:
        raise PreventUpdate 
    else:    
        exec(open(script_fn).read())
    
# Main
if __name__ == "__main__":
    app.run_server(debug=False)

尝试使用 if n_clicks is not None and n_clicks > 0 condition 来自动触发回调。

import pathlib
import dash
import pandas as pd
from dash.dependencies import Input, Output, State
import dash_html_components as html
from dash.exceptions import PreventUpdate
import dash_table
import os

script_fn = 'test.py'

app = dash.Dash(__name__, meta_tags=[{"name": "viewport", "content": 
"width=device-width"}])

app.layout = html.Div(
                children=[html.Div((
                            html.Button('df1_Settlement 5i', id='button', 
                                         n_clicks = 0),
                            html.Div(id='output-container-button',
                                      children='Load new file'))
                    ),
                ],
            )

@app.callback(dash.dependencies.Output('output-container-button', 'children'), 
[dash.dependencies.Input('button', 'n_clicks')],prevent_initial_call=True)

def run_script_onClick(n_clicks):
    
    if n_clicks is not None and n_clicks > 0:
        exec(open(script_fn).read()) 
    else:    
        raise PreventUpdate Error
    
# Main
if __name__ == "__main__":
    app.run_server(debug=True)