-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdeta_store.py
59 lines (44 loc) · 1.25 KB
/
deta_store.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
import pandas as pd
import streamlit as st
from deta import Deta
deta = Deta(st.secrets["API"]["DETA_KEY"])
db = deta.Base("topical_q_a")
def fetch_all_items(db):
res = db.fetch()
all_items = res.items
# fetch until last is 'None'
while res.last:
res = db.fetch(last=res.last)
all_items += res.items
print(res)
return all_items
def check_dataframe(data):
if not isinstance(data, pd.DataFrame):
data = pd.DataFrame(data)
return data
def convert_timestamp_to_time(df):
df["timestamp"] = pd.to_datetime(df["timestamp"])
return df
def sort_by_timestamp(df):
df = df.sort_values(by="timestamp", ascending=False)
return df
def app():
st.set_page_config(layout="wide")
st.title("Data Table")
data = fetch_all_items(db)
data = check_dataframe(data)
data = data.drop(columns=["key"])
data = convert_timestamp_to_time(data)
data = sort_by_timestamp(data)
if data is not None:
st.dataframe(data)
csv = data.to_csv(index=False)
st.download_button(
label="Download data as CSV",
data=csv,
file_name="q_data.csv",
mime="text/csv",
)
else:
st.warning("No data to display.")
app()