0

I am trying to make web application which takes temperature and (motor)RPM from sensors coming through my computer which connected serially to a mechanical machine and display it on web page.

I am using Python Flask with AJAX. What I've tried so far now is took jsonify data from back-end and displayed on html page. But I am not getting or seeing any real time data changing on web page without reloading the page. I need to reload the page every time to see data changing.

How can I and what are the possible ways to get this data displayed on web page.

This is my python script for flask app:

from flask import Flask, render_template, request, jsonify
import random
import time
import serial


app = Flask(__name__)

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


@app.route('/ret_num', methods = ['POST', 'GET'])
def ret_num():
    s = serial.Serial('COM7')
    res = s.read()
    time.sleep(1)
    return jsonify(res) 

if __name__ == '__main__':
    app.run(debug = True)

And HTML code:

<html>
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<script>
$(function(){
    $.ajax({
        url: '/ret_num',
        type: 'POST',
        success: function(response) {
            console.log(response);
            $("#num").html(response);
        },
        error: function(error) {
            console.log(error);
        }
    });
});
</script>
<h1>Output</h1>
<h1 id="num"></h1>
</body>
</head>
</html>

1 Answer 1

2

There are two good ways of doing this:

  1. Polling Essentially, you'll have to call your API on a loop at a small interval, say 30 seconds. This can be achieved by using something like (code may not work as is, but the principle remains the same):

    setInterval($.ajax({
        url: '/ret_num',
        type: 'POST',
        success: function(response) {
            console.log(response);
            $("#num").html(response);
        },
        error: function(error) {
            console.log(error);
        }
    }), 30000);

  1. Using WebSockets instead of plain AJAX calls. Read docs here.

  2. (Bonus) You can reload your page at an interval. For example, to reload every 30 seconds:

    setInterval( location.reload(), 30000);

Sign up to request clarification or add additional context in comments.

4 Comments

Wont it affect on page data?, if we are reloading page at specific time interval. I mean its feasible. Will Work. Also I tried Flask-IOsocket module but I think thats different approach for my kind of application.
I assumed that the only data on the page is the one from the sensor. Like I said, it's the last and most dirty approach. The first solution should be the right one for you. Polling.
Okay. What I did using your first approach is I made two identical AJAX scripts and called them separately in same html page and it worked well. setInterval worked in my case though I set 1 sec of interval script and 1 sec in backend code. Please tell me if this is not dirt way to do it.
Nothing dirty about it IMO. Ajax Polling is a legitimate technique to achieve real-time-esque updates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.