gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:08:17 25 4
gpt4 key购买 nike

目标:我以前从未这样做过,而且是 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

关于python - 如何在点击按钮时运行python脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59801648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com