0

I am trying to write code that will request data for a page, process it, and then show the result in a lower section of the page without refreshing.

Flask code:

    if request.method == 'GET':
        return render_template("dashboard.html", data_nodes=data_nodes, data_para=data_para)
    elif request.method=='POST':
        #c, conn = connection2()
        select1 = request.form.get('node')
        select2 = request.form.get('parameter')
        print str(select1)
        print str(select2)
        #processes the data
        #display the processed data on the same page
        #return render_template("dashboard.html") This will refresh the page! Is there another way? 

My HTML side

        <form method="POST" action="{{ url_for('dashboard')}}">
            <label for="node">Node</label>
            <select name="node">
                {% for o in data_nodes %}
                <option value="{{ o.name }}">{{ o.name }}</option>
                {% endfor %}
            </select>
            <label for="parameter">Parameter</label>
            <select name="parameter">
                {% for o in data_para %}
                <option value="{{ o.name }}">{{ o.name }}</option>
                {% endfor %}
            </select>
            <button type="submit" class="btn btn-default" >Send</button>
        </form>

Is there a way to post the processed data without having to redirect to another page?

ballade4op52
  • 2,142
  • 5
  • 27
  • 42
Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41
  • Remember that when you want to show something in a page without refreshing, you need to use Javascript on the page for all the updating. So the way to do what you want is to make a javascript that calls your service and then with the data received, update your section. – Aquiles Apr 24 '16 at 11:50
  • Does that mean that the JavaScript will take the data first, pass it to Flask, and then Flask will process the data and then send it back to JavaScript? Is that possible? – Ahmed Al-haddad Apr 24 '16 at 11:52
  • 1
    Yes, the way to do it is make Javascript, AJAX send the form to your flask post service, and then process the response of your service. Check the example in the answer by @sashadereh – Aquiles Apr 24 '16 at 13:52
  • I think `AJAX` is what you exactly need. [Here](http://code.runnable.com/UiPhLHanceFYAAAP/how-to-perform-ajax-in-flask-for-python) you can find working example, just run it to see the result. – sashadereh Apr 24 '16 at 12:01

0 Answers0