DmitrMakeev commited on
Commit
9c2e351
·
verified ·
1 Parent(s): 7d3f8b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -85
app.py CHANGED
@@ -314,7 +314,7 @@ def plot_ph_week():
314
  week_number = 1
315
  elif week_number > 30:
316
  week_number = 30
317
-
318
  conn = sqlite3.connect('system_data.db')
319
  cursor = conn.cursor()
320
 
@@ -322,16 +322,8 @@ def plot_ph_week():
322
  table_exists = cursor.fetchone()
323
 
324
  if not table_exists:
325
- return '''
326
- <html>
327
- <body>
328
- <h1>Ошибка!</h1>
329
- <p>Таблица system_data не существует.</p>
330
- <button onclick="window.location.href='/plot_ph_week'">Назад</button>
331
- </body>
332
- </html>
333
- '''
334
-
335
  cursor.execute('''
336
  SELECT date_time, ph, ec, dey, onA, onB, onC
337
  FROM system_data
@@ -339,81 +331,25 @@ def plot_ph_week():
339
  ORDER BY date_time
340
  ''', (week_number,))
341
  rows = cursor.fetchall()
342
-
343
  conn.close()
344
-
345
  if not rows:
346
- return f'''
347
- <html>
348
- <body>
349
- <h1>Данных нет</h1>
350
- <p>За {week_number}-ю неделю данных нет.</p>
351
- <button onclick="window.location.href='/plot_ph_week?week={week_number - 1}'" {"disabled" if week_number == 1 else ""}>Предыдущая неделя</button>
352
- <button onclick="window.location.href='/plot_ph_week?week={week_number + 1}'" {"disabled" if week_number == 30 else ""}>Следующая неделя</button>
353
- </body>
354
- </html>
355
- '''
356
-
357
- dates = [f"{row[0]} (d: {row[3]})" for row in rows]
358
- ph_values = [float(row[1]) for row in rows]
359
- ec_values = [float(row[2]) for row in rows]
360
- onA_values = [float(row[4]) for row in rows]
361
- onB_values = [float(row[5]) for row in rows]
362
- onC_values = [float(row[6]) for row in rows]
363
-
364
- plt.figure(figsize=(15, 18)) # Увеличил высоту для трех графиков
365
-
366
- # График pH
367
- plt.subplot(3, 1, 1)
368
- plt.plot(dates, ph_values, linestyle='-', color='b')
369
- plt.title(f'График pH за {week_number}-ю неделю', fontsize=14)
370
- plt.xlabel('Дата и день недели', fontsize=12)
371
- plt.ylabel('Значение pH', fontsize=12)
372
- plt.xticks(rotation=90, fontsize=6)
373
- plt.grid(True)
374
-
375
- # График EC
376
- plt.subplot(3, 1, 2)
377
- plt.plot(dates, ec_values, linestyle='-', color='g')
378
- plt.title(f'График EC за {week_number}-ю неделю', fontsize=14)
379
- plt.xlabel('Дата и день недели', fontsize=12)
380
- plt.ylabel('Значение EC', fontsize=12)
381
- plt.xticks(rotation=90, fontsize=6)
382
- plt.grid(True)
383
-
384
- # График насосов
385
- plt.subplot(3, 1, 3)
386
- plt.plot(dates, onA_values, linestyle='-', color='green', label='Насос A')
387
- plt.plot(dates, onB_values, linestyle='-', color='brown', label='Насос B')
388
- plt.plot(dates, onC_values, linestyle='-', color='orange', label='Насос C')
389
- plt.title(f'График работы насосов за {week_number}-ю неделю', fontsize=14)
390
- plt.xlabel('Дата и день недели', fontsize=12)
391
- plt.ylabel('Состояние насосов', fontsize=12)
392
- plt.xticks(rotation=90, fontsize=6)
393
- plt.legend()
394
- plt.grid(True)
395
-
396
- # Увеличиваем расстояние между графиками
397
- plt.subplots_adjust(hspace=1.2, bottom=0.1, top=0.95)
398
-
399
- buffer = io.BytesIO()
400
- plt.savefig(buffer, format='png')
401
- buffer.seek(0)
402
-
403
- plot_data = base64.b64encode(buffer.getvalue()).decode('utf-8')
404
-
405
- return f'''
406
- <html>
407
- <body>
408
- <h1>Графики за {week_number}-ю неделю</h1>
409
- <button onclick="window.location.href='/plot_ph_week?week={week_number - 1}'" {"disabled" if week_number == 1 else ""}>Предыдущая неделя</button>
410
- <button onclick="window.location.href='/plot_ph_week?week={week_number + 1}'" {"disabled" if week_number == 30 else ""}>Следующая неделя</button>
411
- <br><br>
412
- <img src="data:image/png;base64,{plot_data}" alt="Графики pH, EC и насосов">
413
- </body>
414
- </html>
415
- '''
416
-
417
  except Exception as e:
418
  return jsonify({'status': 'error', 'message': str(e)}), 500
419
 
@@ -433,7 +369,6 @@ def plot_ph_week():
433
 
434
 
435
 
436
-
437
  @app.route("/")
438
  def index():
439
  return flask.render_template('index.html')
 
314
  week_number = 1
315
  elif week_number > 30:
316
  week_number = 30
317
+
318
  conn = sqlite3.connect('system_data.db')
319
  cursor = conn.cursor()
320
 
 
322
  table_exists = cursor.fetchone()
323
 
324
  if not table_exists:
325
+ return jsonify({'status': 'error', 'message': 'Таблица system_data не существует'}), 404
326
+
 
 
 
 
 
 
 
 
327
  cursor.execute('''
328
  SELECT date_time, ph, ec, dey, onA, onB, onC
329
  FROM system_data
 
331
  ORDER BY date_time
332
  ''', (week_number,))
333
  rows = cursor.fetchall()
334
+
335
  conn.close()
336
+
337
  if not rows:
338
+ return jsonify({'status': 'error', 'message': f'Нет данных за {week_number}-ю неделю'}), 404
339
+
340
+ # Формируем данные для JSON
341
+ data = {
342
+ 'week': week_number,
343
+ 'dates': [row[0] for row in rows], # Даты
344
+ 'ph': [float(row[1]) for row in rows], # pH
345
+ 'ec': [float(row[2]) for row in rows], # EC
346
+ 'onA': [float(row[4]) for row in rows], # Насос A
347
+ 'onB': [float(row[5]) for row in rows], # Насос B
348
+ 'onC': [float(row[6]) for row in rows], # Насос C
349
+ }
350
+
351
+ return render_template('plot_ph_week.html', data=data)
352
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
353
  except Exception as e:
354
  return jsonify({'status': 'error', 'message': str(e)}), 500
355
 
 
369
 
370
 
371
 
 
372
  @app.route("/")
373
  def index():
374
  return flask.render_template('index.html')