如何在点击按钮时 运行 python 编写脚本?

How to run python script on clicking button?

目标: 我以前从未这样做过,而且是 python 的新手。我想在按下按钮时 运行 一个 python 脚本随叫随到。

问题:有人可以指点一下如何解决这个问题吗?

我的代码:

**Button HTML**
    # Layout of Dash App HTML
    app.layout = html.Div(
        children=[
            html.Div(
                            html.Button('Detect', id='button'),
                            html.Div(id='output-container-button',
                            children='Hit the button to update.')
                         ],
                    ),
                ],
            )

@app.callback(
    dash.dependencies.Output('output-container-button', 'children'),
    [dash.dependencies.Input('button')])
def run_script_onClick():
    return os.system('python /Users/ME/Desktop/DSP_Frontend/Pipeline/Pipeline_Dynamic.py')

目前这给出了错误:

Traceback (most recent call last):
  File "app.py", line 592, in <module>
    [dash.dependencies.Input('button')])
TypeError: __init__() missing 1 required positional argument: 'component_property'

编辑:

我认为解决方案可能是将 some_argument 添加到 run_script_onClick:

def run_script_onClick(some_argument):
        return os.system('python /Users/ME/Desktop/DSP_Frontend/Pipeline/Pipeline_Dynamic.py')

我目前正在浏览 this 列表以找到合适的 item() 用作参数。

这是我要使用的:

from subprocess import call
from dash.exceptions import PreventUpdate

@app.callback(
    dash.dependencies.Output('output-container-button', 'children'),
    [dash.dependencies.Input('button', 'n_clicks')])
def run_script_onClick(n_clicks):
    # Don't run unless the button has been pressed...
    if not n_clicks:
        raise PreventUpdate

    script_path = 'python /Users/ME/Desktop/DSP_Frontend/Pipeline/Pipeline_Dynamic.py'
    # The output of a script is always done through a file dump.
    # Let's just say this call dumps some data into an `output_file`
    call(["python3", script_path])

    # Load your output file with "some code"
    output_content = some_loading_function('output file')

    # Now return.
    return output_content

@雅科夫布雷斯勒。这对我有用,无需使用以下几行。非常感谢。

output_content = some_loading_function('output file')
    return output_content