Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - Calling python function on button OnClick event

I am new to python and Flask. I have a Flask Web App with a button. When I click on the button I would like to execute a python method not a Javascript method. How can I do this?

I have seen examples with python where it redirects me to a new page using a form tag like this

<form action="/newPage" method="post"> 

but I don't want it to redirect me to a new page. I just want it to execute the python method. I am making this for a Raspberry Pi robot car. When I press the forward button, I want it to run the method to Turn wheels forward.

Button HTML Code (index.html)

<button name="forwardBtn" onclick="move_forward()">Forward</button> 

simple app.py Code - move_forward() method located here

#### App.py code  from flask import Flask, render_template, Response, request, redirect, url_for app = Flask(__name__)  @app.route("/") def index():     return render_template('index.html');  def move_forward():     #Moving forward code     print("Moving Forward...") 

I have seen similar questions to mine on Stackoverflow, but they don't seem to answer my question or I am unable to understand the answer. If someone could please provide me a simple way to call a Python method on button click event, it would be greatly appreciated.

Other Questions that I have looked at:

--Python Flask calling functions using buttons

--Calling a python function with a button

--Flask Button run Python without refreshing page?

like image 501
Amaan Avatar asked Mar 04 '17 21:03

Amaan


People also ask

How do you call a python function on a button click in Flask?

To call a Python Flask function on button click event, we can navigate to the view when the button is clicked. We use url_for to get the URL for the view function with name move_forward .

How do you call a function in a Flask python?

Flask – Application Flask constructor takes the name of current module (__name__) as argument. The route() function of the Flask class is a decorator, which tells the application which URL should call the associated function. The rule parameter represents URL binding with the function.

How do you call a function by clicking a button?

To call a JavaScript function from a button click, you need to add an event handler and listen for the click event from your <button> element. You can the id attribute to the <button> so that you can fetch it using the document. getElementById() method.

How do you use buttons on a Flask?

In the Flask code you can add an action under every if statement of the corresponding button like rendering a template or running a python script. In the HTML code you can add as many buttons as you want just be sure to add the right values and names.


1 Answers

You can simply do this with help of AJAX... Here is a example which calls a python function which prints hello without redirecting or refreshing the page.

In app.py put below code segment.

#rendering the HTML page which has the button @app.route('/json') def json():     return render_template('json.html')  #background process happening without any refreshing @app.route('/background_process_test') def background_process_test():     print ("Hello")     return ("nothing") 

And your json.html page should look like below.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type=text/javascript>         $(function() {           $('a#test').on('click', function(e) {             e.preventDefault()             $.getJSON('/background_process_test',                 function(data) {               //do nothing             });             return false;           });         }); </script>   //button <div class='container'>     <h3>Test</h3>         <form>             <a href=# id=test><button class='btn btn-default'>Test</button></a>         </form>  </div> 

Here when you press the button Test simple in the console you can see "Hello" is displaying without any refreshing.

like image 87
Gihan Gamage Avatar answered Sep 20 '22 19:09

Gihan Gamage