如何在单击按钮时运行python脚本?

问题描述 投票:0回答:1

Goal:我以前从未做过,并且是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'

编辑:

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

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

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

python button callback plotly plotly-dash
1个回答
0
投票

这是我要使用的:

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
© www.soinside.com 2019 - 2024. All rights reserved.