0

I have an arduino program and set-up which detects the number of cars from a parking lot. The number of cars is printed on the serial every time when the sensor detects a car in front of the barrier. I want to print the number of cars from the parking lot into a small web app. I use Tera Term to scan my serial bus and put the output data into a file text ( data.txt) . Then i use python to read the value from that text file and render it into a HTML page on a web app.

Here is the python code :

from flask import Flask, render_template
import logging
import requests


# Initialize the Flask application
app = Flask(__name__)

# Define a route for the default URL, which loads the form
@app.route("/")
def form():
 with open('date.txt') as f:
     data = []
     lines = f.read().splitlines()
     data.append(lines)
 for i in data:
     return render_template('index.html', variable=data)


if __name__ == "__main__":
 app.run()

here is index.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Arduino Project</title>
    <style>
        body{
            background: antiquewhite;
        }
h1 {color:red;
text-align: center;}
h2 {color:blue;
   text-align: center;}
</style>
</head>
<body>
<h1> - Arduino - </h1>
<h2> Number of cars in the parking place: {{ variable }}<br></h2>
<script> setTimeout(function(){
   window.location.reload(1);
}, 5000);
</script>
</body>
</html>

It works fine, but i want to have only a variable which updates every 5 seconds,when the page is refreshed.I dont want to see all values from the date.txt,just the last one.

This is how my web app looks like with this code until now : (ignore the error message) enter image description here

HeaveHd
  • 13
  • 2

1 Answers1

0

Returning multiples times is not possible.

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed.

So this part could be adjusted:

 for i in data:
     return render_template('index.html', variable=data)

You said you want the last line only. So replace above with this:

  return render_template('index.html', variable=data[-1])

Documentation

Getting the last element of a list

https://www.geeksforgeeks.org/python-return-statement/

Wimanicesir
  • 4,606
  • 2
  • 11
  • 30
  • Tried to replace that. I get the same output. I think the problem is with render_template() function. It always renders the same html page and adds the value next to the older one. – HeaveHd May 04 '20 at 14:17
  • The same output as before or always the same once loaded? – Wimanicesir May 04 '20 at 14:30
  • Same as before. For now i have a dummy project running in arduino. It counts to 1000,and prints the index every 5 seconds. So the final output is 0, 1, 2, .... 1000 and I get all these values printed into the web app. I want just the last value. Like 1, then replace it with 2, then replace it with 3 and so on. – HeaveHd May 04 '20 at 14:32
  • I see what's wrong now. You have a list with 1 value. A string with all the values. You should split this string into a list. Then only give the last element as given in the answer. – Wimanicesir May 04 '20 at 14:33
  • 1
    Done,thank you for your answer. It was very helpful. Now it works exactly how i wanted :D – HeaveHd May 04 '20 at 15:09