2

Goal: I have never done this before, and am new to python. I want to run a python script on call as a button is pushed.

Question: Can someone give pointers as to how to go about solving this?

My Code:

**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')

Currently this gives the error:

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'

EDIT:

I think the solution might be to add some_argument to the run_script_onClick:

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

I am currently browsing through this list to find an appropriate item() to use as argument.

14
  • 1
    What do you understand from that error message? Have you tried fixing that first? Commented Jan 18, 2020 at 15:41
  • HI @AMC, it mentions that I am missing another argument for the function I am trying to call. (So something like function(arg1, arg2). Currently it considers 'button' but perhaps another argument that is correlated to the return (the script that should run) ? But I don't know how to go about that exactly. Is my understanding correct? Commented Jan 18, 2020 at 15:43
  • Currently it considers 'button' but perhaps another argument that is correlated to the return (the script that should run) ? I'm not sure I understand what you mean. This might be of use: dash.plot.ly/dash-core-components/input. Commented Jan 18, 2020 at 15:48
  • @AMC yes I used that link to get where I am now. But I am a bit stuck and am wondering what argument I am missing to make the return python script run upon click. Commented Jan 18, 2020 at 15:51
  • 1
    While that is a good improvement to make, it's unlikely to fix the current error. Have you seen this page, by the way? Commented Jan 18, 2020 at 16:12

2 Answers 2

5

Here's what I would use:

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
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I tried what you said. It outputs the following message: ' TypeError: __init__() missing 1 required positional argument: 'component_property' Here are the available properties in "button": ['children', 'id', 'n_clicks', 'n_clicks_timestamp', 'key', 'role', 'data-', 'aria-', 'autoFocus', 'disabled', 'form', 'formAction', 'formEncType', 'formMethod', 'formNoValidate', 'formTarget', 'name', 'type', 'value', 'accessKey', 'className', 'contentEditable', 'contextMenu', 'dir', 'draggable', 'hidden', 'lang', 'spellCheck', 'style', 'tabIndex', 'title', 'loading_state']'
aah now I get n_clicks better. This clears up a lot. Thanks for that. I'll look more into the 'some_loading_function' part, in order to grasp it better for my case. That file I am trying to run checks whether there has been any new data from sensors. If so, it appends the new data to the existing csv. That is was 'pipeline_dynamic.py' does. So many there is no need for ' output_content = some_loading_function('output file.csv')' as long as the file runs in the background.
Is there a way to check if the file ran successfully. Now I have commented output_content and return output_content. I have added: print('file loaded successfully). But is there a function that actually check if that file ran successfully or not?
2 options for you @user7186746 – 1) place a print statement at the end of your pipeline_dynamic.py file or 2) manually inspect the outcome file
1

@ Yaakov Bressler. This works for me without using the following lines. Thanks a lot.

output_content = some_loading_function('output file')
    return output_content

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.