Update app.py
Browse files
app.py
CHANGED
@@ -324,40 +324,14 @@ def plot_ph():
|
|
324 |
@app.route('/plot_ph_week', methods=['GET'])
|
325 |
def plot_ph_week():
|
326 |
try:
|
327 |
-
# Получаем номер недели из
|
328 |
-
week_number = request.args.get('week')
|
329 |
-
|
330 |
-
#
|
331 |
-
if
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
<h1>Выберите неделю для графика pH</h1>
|
336 |
-
<form method="GET">
|
337 |
-
<label for="week">Номер недели (1-30):</label>
|
338 |
-
<input type="number" id="week" name="week" min="1" max="30" required>
|
339 |
-
<br><br>
|
340 |
-
<button type="submit">Показать график</button>
|
341 |
-
</form>
|
342 |
-
</body>
|
343 |
-
</html>
|
344 |
-
'''
|
345 |
-
|
346 |
-
# Проверяем, что введённое значение - число от 1 до 30
|
347 |
-
try:
|
348 |
-
week_number = int(week_number)
|
349 |
-
if week_number < 1 or week_number > 30:
|
350 |
-
raise ValueError
|
351 |
-
except ValueError:
|
352 |
-
return '''
|
353 |
-
<html>
|
354 |
-
<body>
|
355 |
-
<h1>Ошибка!</h1>
|
356 |
-
<p>Введите номер недели от 1 до 30.</p>
|
357 |
-
<button onclick="window.location.href='/plot_ph_week'">Назад</button>
|
358 |
-
</body>
|
359 |
-
</html>
|
360 |
-
'''
|
361 |
|
362 |
# Подключаемся к базе данных
|
363 |
conn = sqlite3.connect('system_data.db')
|
@@ -378,7 +352,7 @@ def plot_ph_week():
|
|
378 |
</html>
|
379 |
'''
|
380 |
|
381 |
-
#
|
382 |
cursor.execute('''
|
383 |
SELECT date_time, ph, dey
|
384 |
FROM system_data
|
@@ -389,21 +363,22 @@ def plot_ph_week():
|
|
389 |
|
390 |
conn.close()
|
391 |
|
392 |
-
#
|
393 |
if not rows:
|
394 |
-
return '''
|
395 |
<html>
|
396 |
<body>
|
397 |
-
<h1
|
398 |
-
<p
|
399 |
-
<button onclick="window.location.href='/plot_ph_week'"
|
|
|
400 |
</body>
|
401 |
</html>
|
402 |
'''
|
403 |
|
404 |
-
#
|
405 |
-
dates = [f"{row[0]} (d: {row[2]})" for row in rows]
|
406 |
-
ph_values = [float(row[1]) for row in rows]
|
407 |
|
408 |
# Создаём график
|
409 |
plt.figure(figsize=(15, 6))
|
@@ -423,13 +398,13 @@ def plot_ph_week():
|
|
423 |
# Кодируем график в base64
|
424 |
plot_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
425 |
|
426 |
-
# Возвращаем HTML с
|
427 |
return f'''
|
428 |
<html>
|
429 |
<body>
|
430 |
<h1>График pH за {week_number}-ю неделю</h1>
|
431 |
-
<button onclick="window.location.href='/plot_ph_week'"
|
432 |
-
<button onclick="window.location.
|
433 |
<br><br>
|
434 |
<img src="data:image/png;base64,{plot_data}" alt="График pH">
|
435 |
</body>
|
|
|
324 |
@app.route('/plot_ph_week', methods=['GET'])
|
325 |
def plot_ph_week():
|
326 |
try:
|
327 |
+
# Получаем номер недели из параметра, если его нет - начинаем с 1
|
328 |
+
week_number = request.args.get('week', default=1, type=int)
|
329 |
+
|
330 |
+
# Проверяем, что номер недели в пределах 1-30
|
331 |
+
if week_number < 1:
|
332 |
+
week_number = 1
|
333 |
+
elif week_number > 30:
|
334 |
+
week_number = 30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
335 |
|
336 |
# Подключаемся к базе данных
|
337 |
conn = sqlite3.connect('system_data.db')
|
|
|
352 |
</html>
|
353 |
'''
|
354 |
|
355 |
+
# Выбираем данные за выбранную неделю
|
356 |
cursor.execute('''
|
357 |
SELECT date_time, ph, dey
|
358 |
FROM system_data
|
|
|
363 |
|
364 |
conn.close()
|
365 |
|
366 |
+
# Если данных нет, вывести сообщение и оставить кнопки
|
367 |
if not rows:
|
368 |
+
return f'''
|
369 |
<html>
|
370 |
<body>
|
371 |
+
<h1>Данных нет</h1>
|
372 |
+
<p>За {week_number}-ю неделю данных нет.</p>
|
373 |
+
<button onclick="window.location.href='/plot_ph_week?week={week_number - 1}'" {"disabled" if week_number == 1 else ""}>Предыдущая неделя</button>
|
374 |
+
<button onclick="window.location.href='/plot_ph_week?week={week_number + 1}'" {"disabled" if week_number == 30 else ""}>Следующая неделя</button>
|
375 |
</body>
|
376 |
</html>
|
377 |
'''
|
378 |
|
379 |
+
# Формируем данные для графика
|
380 |
+
dates = [f"{row[0]} (d: {row[2]})" for row in rows]
|
381 |
+
ph_values = [float(row[1]) for row in rows]
|
382 |
|
383 |
# Создаём график
|
384 |
plt.figure(figsize=(15, 6))
|
|
|
398 |
# Кодируем график в base64
|
399 |
plot_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
400 |
|
401 |
+
# Возвращаем HTML с кнопками управления
|
402 |
return f'''
|
403 |
<html>
|
404 |
<body>
|
405 |
<h1>График pH за {week_number}-ю неделю</h1>
|
406 |
+
<button onclick="window.location.href='/plot_ph_week?week={week_number - 1}'" {"disabled" if week_number == 1 else ""}>Предыдущая неделя</button>
|
407 |
+
<button onclick="window.location.href='/plot_ph_week?week={week_number + 1}'" {"disabled" if week_number == 30 else ""}>Следующая неделя</button>
|
408 |
<br><br>
|
409 |
<img src="data:image/png;base64,{plot_data}" alt="График pH">
|
410 |
</body>
|