简体   繁体   中英

flask - I can't get to show the output of the second app.route

I have this python code for showing temperature and distances (using sensors). The temperature value is shown but the distance value does not. What is wrong with the coding?

I can't seem to work out how to do this. I've tried saving the sensor data output to a text file, dis.txt, but doesn't work still.

Thanks for the help.

Web snapshot: websnapshot

python:

from flask import Flask, render_template
import RPi.GPIO as GPIO
import datetime
import os
#from flask import jsonify
import time
import glob
import sonic
import subprocess
from time import strftime


app = Flask(__name__)

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
os.system('sudo python /home/pi/webserver/sonic.py | tee /home/pi/dis.txt')
temp_sensor='/sys/bus/w1/devices/28-0416a2aca1ff/w1_slave'
dista='/home/pi/dis.txt'

@app.route("/")
def tempReading():
    t=open(temp_sensor,'r')
    lines=t.readlines()
    t.close()

    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string=lines[1].strip()[temp_output+2:]
        temp_c=float(temp_string)/1000.0
    templateData = {
        'temp': round(temp_c,1)
    }
    #return jsonify(temp_c)
    return render_template('temp.html',**templateData)

@app.route("/")
def distance():
        t=open(dista,'r')
        lines=t.readlines()
        t.close()

        distance_output = lines[1].find('Distance: ')
    templateData = {
        'dis': distance
    }
    return render_template('temp.html',**templateData)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=80, debug=True)

temp.html :

    <!DOCTYPE html>
<html>
<head>
    <!--Pescuela, Galilea-->
    <title>Water Quality Monitor</title>
    <meta http-equiv="refresh" content="2"
</head>
    <body>
    <center>
    <font face="Helvetica" size="30">Temperature</font>
    <br><br><strong><font face="Helvetica" size="100">{{ temp }} °C</font></strong>
    <br>
    <font face="Helvetica" size="30">Water level</font>
    <br><br><strong><font face="Helvetica" size="100">{{ dis }} cm</font></strong>

    </body>
</html>

You've decorated both functions with the base route "/" . As it stands, this means that Flask will only run the first function when you hit the base route of your URL.

If you want to keep distance() as a separate function, remove the @app.route decorator, modify it to return the value you want to display on the page, and then call this function from within your tempReading() function, adding the return value to your templateData dictionary.

Thanks for the answers.

I though found a way to execute the python script from within the function. I just added dista=subprocess.check_output('sudo python /home/pi/webserver/sonic.py', shell=True) , where it gets the output of the script and saves to the variable dista and then I added it to the templateData dictionary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM