Flask - python function call on click onclick

I am new to python and Flask. I have a Flask web application 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 saw examples with python where it redirects me to a new page using a form tag like

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

but I don’t want him to redirect me to a new page. I just want it to execute the python method. I am doing this for a Raspberry Pi robot . When I press the forward button, I want it to launch a method for turning the wheels forward.

HTML Code Button ( index.html )

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

simple app.py code is the 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...")

Stackoverflow, , , , . - Python , .

, :

- Python Flask

- python

- Python ?

+21
3

AJAX... , python, , .

app.py .

//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"

json.html .

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
        $(function() {
          $('a#test').bind('click', function() {
            $.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>

, Test simple , "Hello" - .

+19

, - , , , , .

, , , Flask. , . , , .

, , app.py , :

from flask import Flask, render_template, Response, request, redirect, url_for
app = Flask(__name__)

@app.route("/")
def index():
    return render_template('index.html')

@app.route("/forward/", methods=['POST'])
def move_forward():
    #Moving forward code
    forward_message = "Moving Forward..."
    return render_template('index.html', forward_message=forward_message);

html :

<form action="/forward/" method="post">
    <button name="forwardBtn" type="submit">Forward</button>
</form>

... . :

{{ forward_message }} 

... , .

, AJAX Javascript.

+9

index.html (index.html should be in the templates folder)

<!doctype html>
<html>

<head>
    <title>The jQuery Example</title>

    <h2>jQuery-AJAX in FLASK. Execute function on button click</h2>  

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script>
    <script type=text/javascript> $(function() { $("#mybutton").click(function (event) { $.getJSON('/SomeFunction', { },
    function(data) { }); return false; }); }); </script> 
</head>

<body>        
        <input type = "button" id = "mybutton" value = "Click Here" />
</body>    

</html>

test.py

from flask import Flask, jsonify, render_template, request
app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')

@app.route('/SomeFunction')
def SomeFunction():
    print('In SomeFunction')
    return "Nothing"



if __name__ == '__main__':
   app.run()
0
source

Source: https://habr.com/ru/post/1671476/


All Articles