How to call a Python Flask function on button click event?

Sometimes, we want to call a Python Flask function on button click event

In this article, we’ll look at how to call a Python Flask function on button click event.

How to call a Python Flask function on button click event?

To call a Python Flask function on button click event, we can navigate to the view when the button is clicked.

For instance, we write

<button type="button" onclick="window.location.href='{{ url_for( 'move_forward') }}';">Forward</button>

to add a button that navigates to the URL for the move_forward view on click by setting the onclick attribute to

window.location.href='{{ url_for( 'move_forward') }}

We use url_for to get the URL for the view function with name move_forward.

Conclusion

To call a Python Flask function on button click event, we can navigate to the view when the button is clicked.