Я делал довольно простое приложение c Flask для отображения таблиц базы данных mysql на vps.
Я запускал приложение через gunicorn, и оно отлично работает и показывает таблиц, но каждый раз, когда я обновляю sh браузер в приложении, он ломается и возвращает ошибку, указанную ниже.
Как мне исправить это и заставить обновления браузера работать в моем приложении? У меня есть все мои файлы ниже
ОШИБКА ГОРИЗОНТА
:8880 wsgi:app
[2020-04-16 13:23:34 +0000] [3196] [INFO] Starting gunicorn 20.0.4
[2020-04-16 13:23:34 +0000] [3196] [INFO] Listening at: http://0.0.0.0:8880 (319
6)
[2020-04-16 13:23:34 +0000] [3196] [INFO] Using worker: sync
[2020-04-16 13:23:34 +0000] [3199] [INFO] Booting worker with pid: 3199
[2020-04-16 13:23:37 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:23:43,615] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi
_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full
_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in hand
le_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in re
raise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full
_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in disp
atch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/pi.sminfo.me/app.py", line 7, in display_tables
cursor.execute("SELECT * FROM pitemp;")
File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_c
ext.py", line 233, in execute
raise errors.ProgrammingError("Cursor is not connected")
mysql.connector.errors.ProgrammingError: Cursor is not connected
[2020-04-16 13:23:47 +0000] [3196] [INFO] Handling signal: winch
[2020-04-16 13:24:46 +0000] [3196] [INFO] Handling signal: winch
^C[2020-04-16 13:28:30 +0000] [3196] [INFO] Handling signal: int
[2020-04-16 13:28:30 +0000] [3199] [INFO] Worker exiting (pid: 3199)
[2020-04-16 13:28:30 +0000] [3196] [INFO] Shutting down: Master
APP.PY
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def display_tables():
cursor.execute("SELECT * FROM pitemp;")
data = cursor.fetchall()
db.close()
cursor.close()
return render_template("index.html", data=data)
db2.py
db = mysql.connector.connect (
host="localhost",
user="********",
passwd="*******",
database="*******"
)
cursor = db.cursor()
wgsi. py
if __name__ == "__main__":
app.run()
/ templates / index. html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rpi Temp Log</title>
<style>
h1 {
color: #C93F3F;
font-family: Verdana,Geneva,sans-serif;
font-size: 30px;
text-align: left;
}
table {
border-collapse: collapse;
width: 50%;
color: #C93F3F;
font-family: monospace;
font-size: 25px;
text-align: left;
}
th {
background-color: #3A3AEA;
color: white;
}
tr:nth-child(even) {background-color: #f2f2f2}
</style>
</head>
<body>
<div><img src="https://www.raspberrypi.org/app/uploads/2011/10/Raspi-PGB001.png" width="200" height="200" align="left"/><h1>Raspberry Pi Temperature Logger </h1></div>
<table border="1" cellpadding="5" cellspacing="5">
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</html>