forked from GiacCecc/ristGinaWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
92 lines (69 loc) · 2.75 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 10.06.2023, Berlin
# Giacomo Ceccarelli
# Second attempt to design web page for ristGinaIoT
# v. 0.01
import datetime as dt
from ristGinaMongo import get_last_log, get_data_timerange
from flask import Flask, render_template #, redirect, url_for, request, jsonify, make_response
from pymongo import MongoClient
import json
import plotly
import plotly.express as px
#tz_rome = dt.timezone(dt.timedelta(hours=2, minutes=0), 'Europe/Rome')
app = Flask(__name__)
@app.route('/')
def index():
URI = 'mongodb+srv://admin:admin@cluster0.kqg6lq5.mongodb.net/?retryWrites=true&w=majority'
client = MongoClient(URI)
db = client['CF01']
collection = db['logs']
# --------------------------------------------------------------------
logTime, temp = get_last_log(client, 'logs')
# --------------------------------------------------------------------
end_date = dt.datetime.now() #pd.to_datetime("2023-05-18 10:00:00")
start_date = end_date - dt.timedelta(hours=48)
x, y = get_data_timerange(client, 'logs', start_date, end_date)
fig = px.scatter(x=x, y=y,
#labels=dict(x=N, y="Temperatura (°C)")
)
fig.update_layout(
#margin=dict(l=20, r=20, t=20, b=20),
paper_bgcolor='#FFFDFA',
plot_bgcolor='rgba(255,255,255,0)',
)
fig.update_traces(marker_size=4, marker_color='#544D93')
fig.update_xaxes(title=None,
#range=[start_date, end_date],
showgrid=True,
gridwidth=.5,
gridcolor='rgba(0,0,0,.2)'
)
fig.update_yaxes(ticklabelposition="inside top",
title="Temperatura (°C)",
range=[-5, 15],
showgrid=True,
gridwidth=1,
#griddash='dash',
gridcolor='rgba(0,0,0,.5)'
)
fig.update_layout(
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=6, label="6h", step="hour", stepmode="backward"),
dict(count=12, label="12h", step="hour", stepmode="backward"),
dict(count=1, label="1D", step="day", stepmode="todate"),
#dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template('index.html', logTime=logTime, temp=temp, graphJSON=graphJSON)
if __name__ == '__main__':
app.run(debug=True)