from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
import io
from flask import Flask, render_template, send_file, make_response, request
from gpiozero import LED
import datetime
import gevent
import gevent.monkey
from gevent.pywsgi import WSGIServer
gevent.monkey.patch_all()
app = Flask(__name__)
import sqlite3
conn=sqlite3.connect('../sensorsData.db')
curs=conn.cursor()
led = LED(18)
def ledOn():
led.blink()
return "LED has been turned on"
def ledOff():
led.off()
return "LED has been turned off"
def ledStatus():
if led.is_lit:
return 'On'
else:
return 'Off'
@app.route("/readLED/")
def readPin():
response = ledStatus()
templateData = {
'title' : 'Status of LED: ',
'response' : response
}
return render_template('index.html', **templateData)
@app.route("/writeLED/<status>")
def writePin(status):
if status == 'On':
response = ledOn()
else:
response = ledOff()
templateData = {
'title' : 'Status of LED',
'response' : response
}
return render_template('index.html', **templateData)
# Retrieve LAST data from database
def getLastData():
for row in curs.execute("SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1"):
time = str(row[0])
temp = row[1]
hum = row[2]
#conn.close()
return time, temp, hum
def getHistData (numSamples):
curs.execute("SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT "+str(numSamples))
data = curs.fetchall()
dates = []
temps = []
hums = []
for row in reversed(data):
dates.append(row[0])
temps.append(row[1])
hums.append(row[2])
return dates, temps, hums
def maxRowsTable():
for row in curs.execute("select COUNT(temp) from DHT_data"):
maxNumberRows=row[0]
return maxNumberRows
# define and initialize global variables
global numSamples
numSamples = maxRowsTable()
if (numSamples > 101):
numSamples = 100
# main route
@app.route("/")
def index():
time, temp, hum = getLastData()
templateData = {
'time' : time,
'temp' : temp,
'hum' : hum,
'numSamples' : numSamples
}
return render_template('index.html', **templateData)
@app.route('/', methods=['POST'])
def my_form_post():
global numSamples
numSamples = int (request.form['numSamples'])
numMaxSamples = maxRowsTable()
if (numSamples > numMaxSamples):
numSamples = (numMaxSamples-1)
time, temp, hum = getLastData()
templateData = {
'time' : time,
'temp' : temp,
'hum' : hum,
'numSamples' : numSamples
}
return render_template('index.html', **templateData)
@app.route('/plot/temp')
def plot_temp():
times, temps, hums = getHistData(numSamples)
ys = temps
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
axis.set_title("Temperature [°C]")
axis.set_xlabel("Samples")
axis.grid(True)
xs = range(numSamples)
axis.plot(xs, ys)
canvas = FigureCanvas(fig)
output = io.BytesIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = 'image/png'
return response
@app.route('/plot/hum')
def plot_hum():
times, temps, hums = getHistData(numSamples)
ys = hums
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
axis.set_title("Humidity [%]")
axis.set_xlabel("Samples")
axis.grid(True)
xs = range(numSamples)
axis.plot(xs, ys)
canvas = FigureCanvas(fig)
output = io.BytesIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = 'image/png'
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=False)
基本上,我试图运行我的Flask服务器,但始终会出现缩进错误,例如“ File“ Server.py”,第90行 全局numSamples ^ IndentationError:unindent与任何外部缩进级别都不匹配”,代码有什么问题?我没有发现该特定行有什么问题。希望获得帮助
答案 0 :(得分:0)
您必须在61、93、120和121行添加一些空格以纠正缩进:
61:在return
之前添加三个空格。
93:在numSamples
之前添加一些空格。
120:在}
之前添加一个空格。
121:在return
之前添加一个空格。
您可以参考PEP 8 -- Style Guide for Python Code或Python coding standards/best practices