-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
186 lines (159 loc) · 5.4 KB
/
database.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import streamlit as st # pip install streamlit
from deta import Deta # pip install deta
import os
from dotenv import load_dotenv
#from fastapi import FastAPI, File, UploadFile
#from fastapi.responses import HTMLResponse, StreamingResponse
load_dotenv(".env")
#DETA_KEY = os.getenv("DETA_KEY")
DETA_KEY = st.secrets["DETA_KEY"] #<---This for st cloud
CHAT_KEY = st.secrets["CHAT_KEY"]
# Initialize with a project key
deta = Deta(DETA_KEY)
# This is how to create/connect a database
db = deta.Base("Daily_reports")
nt = deta.Base("Notes")
def insert_period(period, incomes, expenses, comment):
"""Returns the report on a successful creation, otherwise raises an error"""
return db.put({"key": period, "incomes": incomes, "expenses": expenses, "comment": comment})
def insert_chart(date,chart):
return nt.put(chart,date)
def fetch_all_chart():
res = nt.fetch()
return res.items
# res = nt.fetch()
# all_items = res.items
#
# # fetch until last is 'None'
# while res.last:
# res = nt.fetch(last=res.last)
# all_items += res.items
# return all_items
def fetch_chart(fn):
res = nt.fetch(fn)
return res.items
def fetch_all_periods():
"""Returns a dict of all periods"""
res = db.fetch()
return res.items
def get_period(period):
"""If not found, the function will return None"""
return db.get(period)
#app = FastAPI()
photos = deta.Drive("images") # access to your drive
def photos_upload(fn, data):
#fn =file.name
#path = os.path.abspath(fn) #<------return full path: C:\Users\ericn\PycharmProjects\MyAPP124\SingleBH.png
return photos.put(fn, data) #<---------HAS TO FIT IN BYTES_DATA TO GENERATE IMAGE DATA!!
def fetch_notes(fn):
res = photos.get(fn)
return res
def list_files():
res = photos.list()
return res.get("names")
# st.markdown(db.render(), unsafe_allow_html=True)
#
# st.text(db.app.post("/upload"))
#db.upload_img(file)
sc = deta.Base("Search_Cache")
def insert_cache(key, date):
return sc.put({"key": key, "date": date})
def fetch_cache():
res = sc.fetch()
return res.items
sub = deta.Base("Notes_Subjects")
def insert_sub(key, date):
return sub.put(key, date)
def fetch_sub():
res = sub.fetch()
return res.items
quiz = deta.Base("Quiz_Log")
def insert_quiz(n_quiz, date, shuf_ques, shuf_ans,correct):
return quiz.put({"key": n_quiz,"Date": date, "Question": shuf_ques, 'Ans': shuf_ans,'Correct':correct})
#,"Ques":question,"Ans":ans})#,question,ans):
def fetch_quiz():
res = quiz.fetch()
return res.items
def delete_quiz(key):
res = quiz.delete(key)
return res
dquiz = deta.Base("Daily_Quiz_Log")
def insert_dquiz(n_quiz, date, shuf_ques, shuf_ans,correct):
return dquiz.put({"key": n_quiz,"Date": date, "Question": shuf_ques, 'Ans': shuf_ans,'Correct':correct})
#,"Ques":question,"Ans":ans})#,question,ans):
def fetch_dquiz():
res = dquiz.fetch()
return res.items
def delete_dquiz(key):
res = dquiz.delete(key)
return res
memquiz = deta.Base("Memory_Quiz_Log")
def insert_memquiz(date, rand_quiz,rand_note):
return memquiz.put({"key": date,"RandQuiz": rand_quiz,"RandNote": rand_note})
#,"Ques":question,"Ans":ans})#,question,ans):
def fetch_memquiz():
res = memquiz.fetch()
return res.items
def delete_memquiz(key):
res = memquiz.delete(key)
return res
coin = deta.Base("CoinMaster")
def save_coin(c, date):
return coin.put({"key": date,"Earnings": c})
def fetch_coin():
res = coin.fetch()
return res.items
hform = deta.Base("Hform_Log")
def insert_hform(date, DS, states,descriptions,illness):
return hform.put({"key": date, "DailySurvey": DS, 'States': states,'Descriptions':descriptions,'Illness':illness})
#,"Ques":question,"Ans":ans})#,question,ans):
def fetch_hform():
res = hform.fetch()
return res.items
def delete_hform(key):
res = hform.delete(key)
return res
nt_srch = deta.Base("WritingNotes") #<---------Can't have spaces in name!!!'
def insert_wnote(dt,subject,category,importance,comment,title):
return nt_srch.put({"key": dt,"subject": subject, "category": category, 'importance':importance, "comment": comment, "title":title})
#"key" can be changed to anything for search
def fetch_wnote(fn):
res = nt_srch.fetch(fn)
return res.items
def update_wnote(updates, key):
#res = nt_srch.update()
return nt_srch.update(updates, key)
def fetch_all_wnote():
res = nt_srch.fetch()
all_items = res.items
# fetch until last is 'None'
while res.last:
res = nt_srch.fetch(last=res.last)
all_items += res.items
return all_items
#, "subject": subject})#, "category": category, 'importance':importance, "comment": comment})
#,subject):#, category,importance, comment
# @app.get("/", response_class=HTMLResponse)
# def render():
# return """
# <form action="/upload" enctype="multipart/form-data" method="post">
# <input name="file" type="file">
# <input type="submit">
# </form>
# """
# @app.post("/upload")
# def upload_img(file: UploadFile = File(...)):
# name = file.filename
# f = file.file
# res = drive.put(name, f)
# return res
#@app.post("/upload")
# def upload_img(file, bytedata):
# name = file.name
# f = bytedata
# res = db.put(name, f)
# return res
#@app.get("/download/{name}")
# def download_img(name: str):
# res = db.get(name)
# return StreamingResponse(res.iter_chunks(1024), media_type="image/png")