Search code examples
pythontaipy

How to click a button programmatically in Taipy


I have a button in Taipy that when clicked calls a function performing some tasks that can give a valid or invalid result. In case of having an invalid result, clicking on another button must simulate a click on the first button (I cannot click on the first button directly because it has some status variables defined!).

I solved it by calling the function created inside the function of the second button, putting an ID and any action strings but, how can I make this click action programmatically?


Solution

  • By the description of your issue, it seems the best solution is already what you have done. Simulate a click is the same as calling the callback function.

    Is this code below not a good solution?

    from taipy.gui import Gui, notify
    
    issue_in_run = False
    
    md = """
    <|First button|button|on_action=run|active={not issue_in_run}|>
    
    <|Simulate First button (run)|button|on_action=run_simulate|>
    """
    
    def run(state):
        print("Running...")
        notify(state, "info", "Running...")
    
        if not state.issue_in_run:
            print("Error")
            notify(state, "error", "Error!")
            state.issue_in_run = True
        else:
            print("Finished!")
            notify(state, "success", "Finished!")
            state.issue_in_run = False
    
    def run_simulate(state):
        print("Simulating the run")
        notify(state, "info", "Simulating the run")
        run(state)
    
    Gui(md).run()