Запустить скрипт python на кнопке Da sh -plotly - PullRequest
0 голосов
/ 02 марта 2020

У меня есть приложение da sh с кнопкой, и я хочу, чтобы эта кнопка запускала другой скрипт 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.

Примечание , что из дашластода sh импорт lastoda sh если каталог, в который я импортирую скрипт python в ром. lastoda sh - это скрипт, который я запускаю в кнопке приложения

1 Ответ

0 голосов
/ 02 марта 2020

Для меня подобный код работает нормально, если я использую следующее в обратном вызове

script_fn = 'script.py'
exec(open(script_fn).read())

для запуска скрипта. Это рекомендуемый метод для выполнения Python скриптов из Python в Py3. Я не знаю, откуда call должно появиться в вашем сценарии.

...