Spaces:
Runtime error
Runtime error
update fetch + label
Browse files
app.py
CHANGED
@@ -3,6 +3,8 @@ import pandas as pd
|
|
3 |
import numpy as np
|
4 |
import requests
|
5 |
import time
|
|
|
|
|
6 |
|
7 |
st.set_page_config(
|
8 |
page_title="Real-Time IoT",
|
@@ -26,21 +28,27 @@ DATA_URL = 'https://trace.vfsc.vn/iot/xxx'
|
|
26 |
|
27 |
|
28 |
def load_data(n):
|
29 |
-
|
30 |
-
response = requests.get(DATA_URL, params = {"items": n})
|
31 |
|
32 |
plan = response.json()['plan']
|
33 |
data = response.json()['data']
|
34 |
|
35 |
return data
|
36 |
|
37 |
-
df = pd.DataFrame.from_dict(load_data(1))
|
38 |
-
df.rename({'Lat' : 'lat', 'Lng' : 'lon'}, axis='columns', inplace=True)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
if "count" not in st.session_state:
|
42 |
-
st.session_state['count'] =
|
43 |
|
|
|
|
|
|
|
44 |
|
45 |
col1, col2 = st.columns(2)
|
46 |
|
@@ -51,7 +59,7 @@ data_col = "STemp"
|
|
51 |
with col1:
|
52 |
data_col = st.selectbox(
|
53 |
"Choose column to plot",
|
54 |
-
[col for col in df.columns if col.lower() in "upt, batv, solv, stemp
|
55 |
)
|
56 |
|
57 |
# st.checkbox("Disable selectbox widget", key="disabled")
|
@@ -63,13 +71,33 @@ with col1:
|
|
63 |
with col2:
|
64 |
placeholder = st.empty()
|
65 |
while True:
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
with placeholder.container():
|
69 |
-
st.map(df)
|
70 |
if graph_type == 'Line':
|
71 |
-
st.
|
72 |
elif graph_type == 'Bar':
|
73 |
-
st.bar_chart(
|
74 |
-
|
75 |
-
|
|
|
|
|
|
|
|
3 |
import numpy as np
|
4 |
import requests
|
5 |
import time
|
6 |
+
from datetime import datetime
|
7 |
+
import altair as alt
|
8 |
|
9 |
st.set_page_config(
|
10 |
page_title="Real-Time IoT",
|
|
|
28 |
|
29 |
|
30 |
def load_data(n):
|
31 |
+
response = requests.get(DATA_URL) if n <= 0 else requests.get(DATA_URL, params = {"items": n})
|
|
|
32 |
|
33 |
plan = response.json()['plan']
|
34 |
data = response.json()['data']
|
35 |
|
36 |
return data
|
37 |
|
|
|
|
|
38 |
|
39 |
+
def is_duplicate(s):
|
40 |
+
st.write(s)
|
41 |
+
check_candidate = (s["Id"], s["Time"])
|
42 |
+
res = check_candidate in st.session_state["duplicates"]
|
43 |
+
st.session_state["duplicates"].add(check_candidate)
|
44 |
+
return res
|
45 |
|
46 |
if "count" not in st.session_state:
|
47 |
+
st.session_state['count'] = 10
|
48 |
|
49 |
+
df = pd.DataFrame.from_dict(load_data(st.session_state['count']))
|
50 |
+
df.rename({'Lat' : 'lat', 'Lng' : 'lon'}, axis='columns', inplace=True)
|
51 |
+
st.session_state["duplicates"] = set(zip(df["Id"], df["Time"]))
|
52 |
|
53 |
col1, col2 = st.columns(2)
|
54 |
|
|
|
59 |
with col1:
|
60 |
data_col = st.selectbox(
|
61 |
"Choose column to plot",
|
62 |
+
[col for col in df.columns if col.lower() in "upt, batv, solv, stemp".split(", ")],
|
63 |
)
|
64 |
|
65 |
# st.checkbox("Disable selectbox widget", key="disabled")
|
|
|
71 |
with col2:
|
72 |
placeholder = st.empty()
|
73 |
while True:
|
74 |
+
# new_df = pd.DataFrame.from_dict(load_data(st.session_state["count"]))
|
75 |
+
# new_df.rename({'Lat' : 'lat', 'Lng' : 'lon'}, axis='columns', inplace=True)
|
76 |
+
|
77 |
+
new_data = pd.DataFrame.from_dict(load_data(0)).rename({'Lat' : 'lat', 'Lng' : 'lon'}, axis='columns').iloc[-1]
|
78 |
+
# print(df.iloc[-1]["Id"], new_data["Id"])
|
79 |
+
if is_duplicate(new_data):
|
80 |
+
pass
|
81 |
+
# st.write("Dup")
|
82 |
+
else:
|
83 |
+
df.loc[len(df)] = new_data
|
84 |
+
|
85 |
+
# df = pd.concat([df, pd.DataFrame(new_ele)])
|
86 |
+
|
87 |
+
chart_data = pd.DataFrame(
|
88 |
+
df[data_col].tolist(),
|
89 |
+
df["moment"].apply(lambda timestr : datetime.strptime(timestr, '%d/%m/%Y %H:%M:%S').time().strftime("%H:%M:%S")).tolist()
|
90 |
+
)
|
91 |
+
|
92 |
+
# chart_data.set_index('date')
|
93 |
+
|
94 |
with placeholder.container():
|
|
|
95 |
if graph_type == 'Line':
|
96 |
+
st.bar_chart(chart_data)
|
97 |
elif graph_type == 'Bar':
|
98 |
+
st.bar_chart(chart_data)
|
99 |
+
# st.bar_chart(chart_data["data"], y = chart_data["label"])
|
100 |
+
# st.map(df)
|
101 |
+
st.write(df)
|
102 |
+
time.sleep(15)
|
103 |
+
# st.session_state["count"] += 1
|