-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrello_study.py
More file actions
324 lines (271 loc) · 13.3 KB
/
Copy pathtrello_study.py
File metadata and controls
324 lines (271 loc) · 13.3 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import streamlit as st
import pandas as pd
import numpy as np
import streamlit.components.v1 as components
import streamlit_authenticator as stauth
from streamlit_folium import folium_static
import folium
import os
from datetime import datetime
from deta import Deta
import json
import requests
import urllib.request
import urllib.parse
#from trello import TrelloClient, List
from dateutil.parser import parse
from datetime import datetime
import pytz
tz = pytz.timezone('Asia/Singapore')
import base64
def displayPDF(file):
# Opening file from file path
#with open(file, "rb") as f:
#base64_pdf = base64.b64encode(f.read()).decode('utf-8')
base64_pdf = base64.b64encode(file).decode('utf-8')
# Embedding PDF in HTML
pdf_display = F'<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf"></iframe>'
return pdf_display
# Displaying File
@st.cache(suppress_st_warning=True)
def auth_init():
res = Users.fetch(query=None, limit=100, last=None)
cd = {"usernames" : {} }
for x in res.items :
cd['usernames'][x['username']] = {'name' : x['name'], 'password' : x['hash_password'], 'email' : x['email']}
#usernames.append(x['username'])
#hashed_passwords.append(x['hash_password'])
return cd
@st.cache(suppress_st_warning=True)
def get_card_json (url):
res = requests.post('https://cs0kji.deta.dev/url2json', json={"url" : url})
if res.status_code == 200 :
return res.json()
else:
return {}
Users=Deta(os.environ.get('DETA_PROJECT_ID')).Base(os.environ.get('MILYNNUS_ST_USERS_BASE'))
with st.sidebar:
st.title("Trello Share A Card")
credentials = auth_init()
if credentials:
authenticator = stauth.Authenticate(credentials,
'milynnus_stauth', os.environ.get('MILYNNUS_ST_USERS_SIGNATURE'), cookie_expiry_days=30)
st.info("This application is secured by Streamlit-Authenticator.")
else:
st.session_state['authentication_status'] = False
st.info("Administrator setup is required.")
name, authentication_status, username = authenticator.login('Login', 'sidebar')
st.session_state['authentication_status'] = authentication_status
if st.session_state['authentication_status']:
authenticator.logout('Logout', 'main')
st.write('Welcome *%s*' % (st.session_state['name']))
res = Users.fetch(query={"name" : name, "username" : username}, limit=None, last=None)
if len(res.items) == 1:
user = Users.get(res.items[0]["key"])
card_dict = {}
if "shared_cards" in user.keys():
for url in user["shared_cards"] :
card_json = get_card_json(url)
card_dict[card_json['name']] = card_json['id']
option = st.selectbox(
'Select the card you like to see',
options=list(card_dict.keys()))
st.write('You selected:', option)
st.session_state['card_id'] = card_dict[option]
elif st.session_state['authentication_status'] == False:
st.error('Username/password is incorrect')
elif st.session_state['authentication_status'] == None:
st.warning('Please enter your username and password')
if not st.session_state['authentication_status']:
with st.expander("Register"):
st.warning("This form is for user self registration. The registration data is kept in a Deta Base.")
with st.form("Fill in your name, your preferred username and password", clear_on_submit=True):
name = st.text_input("Name")
username = st.text_input("Username")
email = st.text_input("Email")
password = st.text_input("Password", type="password")
username_unique = Users.fetch(query={"username" : username})
submit = st.form_submit_button("Submit")
if username_unique.count == 0:
pass
else:
st.write("The username : {} has been used, please use another preferred username.".format(username))
st.stop()
if submit:
Users.put({'name' : name, 'username' : username, 'hash_password' : stauth.Hasher([password]).generate()[0]})
with st.expander("Admin setup"):
st.warning("This form is used by the administrator to attach card urls to a username. An admin secret is required for the update.")
with st.form("Enter the card url to be shared with the user", clear_on_submit=True):
username = st.text_input("Username")
url = st.text_input("Card URL")
admin_secret = st.text_input("Admin Secret", type="password")
submit = st.form_submit_button("Submit")
if submit and admin_secret == os.environ.get('MILYNNUS_ST_USERS_SIGNATURE'):
users = Users.fetch(query={"username" : username}, limit=None, last=None)
if len(users.items) != 1 :
st.write("User is not found")
else:
st.write(users.items[0]["key"])
user = Users.get(users.items[0]["key"])
try :
shared_cards = user['shared_cards']
except:
shared_cards = []
if url in shared_cards :
st.write("Card with url {} is already shared with {}".format(url, username))
else:
shared_cards.append(url)
Users.update({"shared_cards" : shared_cards }, user["key"])
st.write("Card with url {} is shared with {}".format(url, username))
if not st.session_state['authentication_status'] :
st.stop()
refresh = st.button("Refresh")
if refresh :
st.experimental_rerun()
if 'card_id' in st.session_state:
card_id = st.session_state['card_id']
st.header(card_json['name'])
st.stop()
res = requests.post('https://cs0kji.deta.dev/card_json', json={"card_id" : card_id})
#res = requests.post('https://70297.wayscript.io/email2board', json={"card_id" : card_id})
if res.status_code == 200 :
card_json=res.json()
else:
st.stop()
if card_json['idAttachmentCover'] == None and card_json['manualCoverAttachment'] == True :
#request = urllib.request.Request(card_json['cover']['scaled'][-1]['url'])
#webUrl = urllib.request.urlopen(request)
res = requests.post('https://ironclad-gecko-habitat-dev.wayscript.cloud/get_image', json={"url" : card_json['cover']['scaled'][-1]['url']})
st.image(base64.b64decode(res.json()['byte_string']))
else:
res = requests.post('https://ironclad-gecko-habitat-dev.wayscript.cloud/get_image', json={"url" : card_json['cover']['scaled'][-1]['url']})
st.image(base64.b64decode(res.json()['byte_string']))
#res = requests.post('https://cs0kji.deta.dev/get_attachment', json={"url" : card_json['cover']['scaled'][-1]['url']})
#if res.status_code == 200:
#st.image(res.content)
with st.expander("Open to see images of attachments"):
columns = st.columns(5)
ix = 0
res = requests.post('https://cs0kji.deta.dev/card_attachments', json={"card_id" : card_id})
if res.status_code == 200 :
for attach in res.json()['attachments']:
ext = attach['fileName'].split(".")[-1]
if (ext == 'jpg' or ext == 'png' or ext == 'jpeg') and attach['id'] != card_json['idAttachmentCover'] and ix <5:
#res = requests.post('https://cs0kji.deta.dev/get_attachment', json={"url" : attach['url']})
res = requests.post('https://ironclad-gecko-habitat-dev.wayscript.cloud/get_image', json={"url" : attach['url']})
if res.status_code == 200:
with columns[ix]:
#columns[ix].image(res.content)
columns[ix].image(base64.b64decode(res.json()['byte_string']))
ix += 1
else:
st.warning(res.status_code)
st.stop()
#with st.expander("Open to PDF"):
#columns = st.columns(5)
#ix = 0
res = requests.post('https://cs0kji.deta.dev/card_attachments', json={"card_id" : card_id})
if res.status_code == 200 :
for attach in res.json()['attachments']:
ext = attach['fileName'].split(".")[-1]
if ext == 'pdf': # and ix <5:
st.write(attach['url'])
res = requests.post('https://ironclad-gecko-habitat-dev.wayscript.cloud/get_pdf', json={"url" : attach['url']})
if res.status_code == 200:
st.markdown(displayPDF(res.content), unsafe_allow_html=True)
#st.image(res.content())
#st.write(type(res.content))
#st.markdown(res.content, unsafe_allow_html=True)
#with columns[ix]:
#columns[ix].image(res.content)
#ix += 1
else:
st.warning(res.status_code)
with st.expander("Swagger"):
link = '[Demo](https://f0w9hg.deta.dev/docs)'
st.markdown(link, unsafe_allow_html=True)
with st.expander("Open to see card labels"):
lbl_head = '''<p style="margin: 0;"><span style="background-color:rgb(252, 252, 252);"><span class="ql-cursor"></span><span class="cl-trello-card-labels">'''
lbl_tail = '''</span></span></p>'''
for lbl in card_json['labels']:
lbl_name = lbl['name']
if lbl['name'] == "":
lbl_name = lbl['color']
itm = '''<b style="color: {}; margin-right: 1.5em;">■ {}</b>'''.format(lbl['color'], lbl_name)
lbl_head = lbl_head + itm
components.html(lbl_head + lbl_tail)
with st.expander("Open to see card start and due status"):
#st. write(card_json)
dates = {}
dates['Start'] = parse(card_json['start']).astimezone(tz).strftime('%Y-%m-%d')
dates['Due'] = parse(card_json['due']).astimezone(tz).strftime('%Y-%m-%d %H:%M')
dates['Completed?'] = card_json['dueComplete']
st.json(dates)
with st.expander("Open to read card description"):
st.markdown(card_json['desc'], unsafe_allow_html=False)
with st.expander("Open to inspect custom fields on card"):
res = requests.post('https://cs0kji.deta.dev/card_customfields', json={"card_id" : card_id})
if res.status_code == 200 :
cf_list = res.json()['customfields']
st.write(cf_list)
ix = 0
for x in cf_list:
if x['Type'] == "date" and x['Value'] != "":
#if x['Value'][-1] == "Z" and x['Value'].index("T") == 10:
#try:
cf_list[ix]['Value'] = parse(x['Value']).astimezone(tz).strftime('%Y-%m-%d %H:%M')
#except:
#pass
ix += 1
st.json(cf_list)
with st.expander("Open to see location on card"):
res = requests.post('https://cs0kji.deta.dev/card_location', json={"card_id" : card_id})
if res.status_code == 200 :
result=res.json()
if 'latitude' in result['coordinates'].keys() and 'longitude' in result['coordinates'].keys() :
st.json(result)
lat = result['coordinates']['latitude']
lon = result['coordinates']['longitude']
#st.json(res.json())
#location = pd.DataFrame(res.json()['coordinates'], columns=['lat', 'lon'])
#st.map(data=location, zoom=20, use_container_width=True)
m = folium.Map(location=[lat, lon], zoom_start=16)
# add marker for Liberty Bell
tooltip = result['address']
folium.Marker(
[lat, lon], popup=result['locationName'], tooltip=tooltip
).add_to(m)
# call to render Folium map in Streamlit
folium_static(m, width=660, height=385)
with st.expander("Open to see status of checklists on card"):
res = requests.post('https://cs0kji.deta.dev/card_checklistitems', json={"card_id" : card_id})
if res.status_code == 200 :
checklist_d = res.json()
for cl in checklist_d.keys():
st.write(cl)
items = pd.DataFrame(checklist_d[cl]).fillna("Not Available")
items["state"].replace({"complete": "✅", "incomplete": "❌"}, inplace=True)
ix = 0
for itm in items["due"] :
if itm != None:
try:
items["due"].iloc[ix] = parse(itm).astimezone(tz).strftime('%Y-%m-%d')
except:
pass
ix += 1
st.dataframe(items)
with st.expander("Open to see images of attachments"):
columns = st.columns(5)
ix = 0
res = requests.post('https://cs0kji.deta.dev/card_attachments', json={"card_id" : card_id})
if res.status_code == 200 :
for attach in res.json()['attachments']:
ext = attach['fileName'].split(".")[-1]
if (ext == 'jpg' or ext == 'png' or ext == 'jpeg') and attach['id'] != card_json['idAttachmentCover'] and ix <5:
res = requests.post('https://cs0kji.deta.dev/get_attachment', json={"url" : attach['url']})
if res.status_code == 200:
with columns[ix]:
columns[ix].image(res.content)
ix += 1
else:
st.warning(res.status_code)