在Dash上运行Python脚本-绘图按钮

2022-04-19 00:00:00 python plotly-dash

问题描述

我有一个带有按钮的Dash应用程序,我确实希望该按钮在单击时运行不同的Python脚本。下面是这个应用程序,我如何配置该应用程序,因为一旦点击按钮,它就会执行另一个Python脚本:

# -*- 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 dashlastodash import lastodash
from dash.exceptions import PreventUpdate
import dash_table
import os

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

os.chdir("path to working dir")
    # 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', '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 = 'path to script\lastodash.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 = lastodash
    # Now return.
    return output_content
# Main
if __name__ == "__main__":
    app.run_server(debug=True, port=8585)

我所做的是从目录导入脚本,然后尝试运行,但它不起作用,有人能帮助我吗?

我收到的错误

usage: [-h] [--debug] lasfile
: error: the following arguments are required: lasfile
An exception has occurred, use %tb to see the full traceback.

注意from dashlastodash导入lastodash如果我要将Python脚本导入到RUM的目录。lastodash是我在应用程序按钮中运行的脚本


解决方案

我遇到一个问题,即脚本在启动DASH应用程序时执行(一次),然后当我单击时它就会起作用。预防更新在上述情况下显然不起作用。换句话说,如果在将回调的输入插入到布局之前,回调的输出已经存在于应用布局中,则当输入首次插入到布局中时,预防_初始_调用将不会阻止其执行。&q;(https://dash.plotly.com/advanced-callbacks) 您知道如何修改或创建新的回调以避免此问题吗?我猜您也有同样的限制?

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
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)

相关文章