Search

14 August, 2018

Calling Flask URL from a script tag

The goal is , to call a FLASK route without reloading the page, when a button is pressed. The content of a div must change with the response text received from the flask route call.


My App:


from flask import Flask, render_template, request, render_template_string
#from jinja2 import Template

app = Flask(__name__)

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

@app.route('/<name>')
def getContent(name):
    return 'This is %s' % name

if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=5002)


The HTML:

<h3 class="mt-1">This is home page</h3>

    <div id="mySpace" class="my-3">Count</div>

    <button id="next_id" class="btn btn-primary">Press</button>

    <script>
        $('#next_id').click(function (){
            $.get('/aroy', function(data, status){
            $('#mySpace').html(data);
        });
        });
    </script>

Result:


No comments:

Post a Comment