Black Lives Matter. Please consider donating to Black Girls Code today.

Which html.Button item() to use to run python script on clicking button?

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: My code below gives me an error. Which item() from this list should I consider to solve and meet my goal?

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'

You’re almost there…your Input statement has same form as Output (and State):

Input('<component id>', '<component property>')
[dash.dependencies.Input('button')])

should be

[dash.dependencies.Input('button', 'n_clicks')])

Hi @flyingcujo,

There is no n_clicks in my run_script_onClick(): function.

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.

The error you are getting is due to the dash.dependencies.Input not being provided 2 arguments. See examples here. Your function may not require the n_clicks parameter but the code change I showed is how the callback will be triggered. You will still need to pass in a paramter to your function (could be anything) - as your function declarations need to account for all Input and State parameters defined.

Hope this helps…

This output you received is showing you what I illustrated…you provided the component_name but not the component_property - in this case the n_clicks property of the button which will trigger the callback.