Spaces:
Sleeping
Sleeping
Update Home.py
Browse files
Home.py
CHANGED
@@ -1,631 +1,631 @@
|
|
1 |
-
import sqlite3
|
2 |
-
import uuid
|
3 |
-
import streamlit as st
|
4 |
-
from utilities import (
|
5 |
-
load_local_css,
|
6 |
-
set_header,
|
7 |
-
load_authenticator,
|
8 |
-
send_email,
|
9 |
-
)
|
10 |
-
import streamlit_authenticator as stauth
|
11 |
-
import yaml
|
12 |
-
from yaml import SafeLoader
|
13 |
-
import os
|
14 |
-
import datetime
|
15 |
-
import shutil
|
16 |
-
import pandas as pd
|
17 |
-
from st_aggrid import AgGrid
|
18 |
-
from st_aggrid import GridOptionsBuilder, GridUpdateMode
|
19 |
-
import pickle
|
20 |
-
from pathlib import Path
|
21 |
-
|
22 |
-
st.set_page_config(layout="wide")
|
23 |
-
load_local_css("styles.css")
|
24 |
-
set_header()
|
25 |
-
|
26 |
-
# def authenticator():
|
27 |
-
for k, v in st.session_state.items():
|
28 |
-
if k not in ["logout", "login", "config"] and not k.startswith(
|
29 |
-
"FormSubmitter"
|
30 |
-
):
|
31 |
-
st.session_state[k] = v
|
32 |
-
with open("config.yaml") as file:
|
33 |
-
config = yaml.load(file, Loader=SafeLoader)
|
34 |
-
st.session_state["config"] = config
|
35 |
-
authenticator = stauth.Authenticate(
|
36 |
-
config["credentials"],
|
37 |
-
config["cookie"]["name"],
|
38 |
-
config["cookie"]["key"],
|
39 |
-
config["cookie"]["expiry_days"],
|
40 |
-
config["preauthorized"],
|
41 |
-
)
|
42 |
-
st.session_state["authenticator"] = authenticator
|
43 |
-
name, authentication_status, username = authenticator.login("Login", "main")
|
44 |
-
auth_status = st.session_state.get("authentication_status")
|
45 |
-
|
46 |
-
st.session_state["name"] = name
|
47 |
-
if auth_status == True:
|
48 |
-
authenticator.logout("Logout", "main")
|
49 |
-
is_state_initiaized = st.session_state.get("initialized", False)
|
50 |
-
|
51 |
-
if not is_state_initiaized:
|
52 |
-
|
53 |
-
if "session_name" not in st.session_state:
|
54 |
-
st.session_state["session_name"] = None
|
55 |
-
|
56 |
-
cols1 = st.columns([2, 1])
|
57 |
-
|
58 |
-
with cols1[0]:
|
59 |
-
st.markdown(f"**Welcome {name}**")
|
60 |
-
with cols1[1]:
|
61 |
-
st.markdown(
|
62 |
-
f"**Current Session: {st.session_state['session_name']}**"
|
63 |
-
)
|
64 |
-
|
65 |
-
# relative_path = Path('DB_Sample','..' ,'DB', 'User.db')
|
66 |
-
# absolute_path = Path.cwd() / relative_path
|
67 |
-
# st.write(absolute_path)
|
68 |
-
# database_file=Path(__file__).parent / relative_path
|
69 |
-
|
70 |
-
database_file = r"DB/User.db"
|
71 |
-
|
72 |
-
conn = sqlite3.connect(
|
73 |
-
database_file, check_same_thread=False
|
74 |
-
) # connection with sql db
|
75 |
-
c = conn.cursor()
|
76 |
-
|
77 |
-
# c.execute('DELETE FROM sessions')
|
78 |
-
# conn.commit()
|
79 |
-
# c.executemany("INSERT INTO users (username, email) VALUES (?, ?)",
|
80 |
-
# [("Geetha Krishna", "[email protected]"),
|
81 |
-
# ("Samkeet Sangai", "[email protected]"),
|
82 |
-
# ('Manoj P','[email protected]'),
|
83 |
-
# ('Srishti Verma','[email protected]'),
|
84 |
-
# ('Ismail mohammed',"[email protected]"),
|
85 |
-
# ('Sharon Sheng','[email protected]'),
|
86 |
-
# ('Ioannis Papadopoulos','[email protected]'),
|
87 |
-
# ('Herman Kwong',"[email protected]")
|
88 |
-
# ])
|
89 |
-
|
90 |
-
# conn.commit()
|
91 |
-
|
92 |
-
# c.execute(f"PRAGMA table_info({'sessions'})")
|
93 |
-
# conn.commit()
|
94 |
-
|
95 |
-
# st.write(c.fetchall())
|
96 |
-
|
97 |
-
# c.execute("Select * from users")
|
98 |
-
# st.write(c.fetchall())
|
99 |
-
|
100 |
-
page_name = "Home Page"
|
101 |
-
|
102 |
-
c.execute(
|
103 |
-
"SELECT email, user_id, user_type FROM users WHERE username = ?",
|
104 |
-
(name,),
|
105 |
-
)
|
106 |
-
user_data = c.fetchone()
|
107 |
-
|
108 |
-
email, user_id, user_type = user_data
|
109 |
-
|
110 |
-
# st.write(user_type)
|
111 |
-
# with st.sidebar:
|
112 |
-
# # if user_type != 'technical':
|
113 |
-
# st.page_link("home.py", label="Home123")
|
114 |
-
# st.page_link('pages/1_Data_Import.py',label='Data Import')
|
115 |
-
# st.page_link('pages/2_Data_Validation.py',label="Data Validation")
|
116 |
-
# st.page_link('pages/3_Transformations.py',label='Transformations')
|
117 |
-
# st.page_link("pages/4_Model_Build.py")
|
118 |
-
# st.page_link('pages/5_Model_Tuning_with_panel.py',label='Model Tuning')
|
119 |
-
|
120 |
-
# st.page_link('pages/5_Saved_Model_Results.py',label="Saved Model Results")
|
121 |
-
|
122 |
-
# st.write(pd.to_datetime(created_time))
|
123 |
-
|
124 |
-
# # c.execute('select * from sessions')
|
125 |
-
|
126 |
-
# output = c.fetchall()
|
127 |
-
|
128 |
-
# st.write(output)
|
129 |
-
|
130 |
-
# if emails is not None:
|
131 |
-
# email = emails[0]
|
132 |
-
|
133 |
-
folder_path = r"Users"
|
134 |
-
user_folder_path = os.path.join(folder_path, email)
|
135 |
-
|
136 |
-
# project_dct = {
|
137 |
-
# 'data_import': {
|
138 |
-
# "granularity_selection":0,
|
139 |
-
# 'cat_dct':{},
|
140 |
-
# "merged_df":None,
|
141 |
-
# 'edited_df':None,
|
142 |
-
# "numeric_columns":None,
|
143 |
-
# "files_dict":None,
|
144 |
-
# 'formatted_panel1_values':None,
|
145 |
-
# 'formatted_panel2_values':None,
|
146 |
-
# "missing_stats_df":None,
|
147 |
-
# 'edited_stats_df':None
|
148 |
-
|
149 |
-
# },
|
150 |
-
|
151 |
-
# 'data_validation': {"target_column":0,
|
152 |
-
# 'selected_panels':None,
|
153 |
-
# "selected_feature":0,
|
154 |
-
# "validated_variables":[],
|
155 |
-
# "Non_media_variables":0
|
156 |
-
|
157 |
-
# },
|
158 |
-
# 'transformations': {},
|
159 |
-
# 'model_build': {},
|
160 |
-
# 'model_tuning':{},
|
161 |
-
# 'saved_model_results': {},
|
162 |
-
# 'model_result_overview': {},
|
163 |
-
# 'build_response_curves': {},
|
164 |
-
# 'scenario_planner': {},
|
165 |
-
# 'saved_scenarios': {},
|
166 |
-
# 'optimized_result_analysis': {}
|
167 |
-
# }
|
168 |
-
# st.session_state['project_dct']=project_dct
|
169 |
-
|
170 |
-
# st.write(project_dct)
|
171 |
-
|
172 |
-
def dump_session_details_db(allowed_users, session_name):
|
173 |
-
|
174 |
-
created_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
175 |
-
|
176 |
-
session_id = str(uuid.uuid4())
|
177 |
-
|
178 |
-
if len(allowed_users) == 0:
|
179 |
-
c.execute(
|
180 |
-
"INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?,?)",
|
181 |
-
(
|
182 |
-
user_id,
|
183 |
-
name,
|
184 |
-
session_id,
|
185 |
-
session_name,
|
186 |
-
page_name,
|
187 |
-
created_time,
|
188 |
-
created_time,
|
189 |
-
None,
|
190 |
-
),
|
191 |
-
)
|
192 |
-
conn.commit()
|
193 |
-
else:
|
194 |
-
for allowed_user in allowed_users:
|
195 |
-
c.execute(
|
196 |
-
"INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?,?)",
|
197 |
-
(
|
198 |
-
user_id,
|
199 |
-
name,
|
200 |
-
session_id,
|
201 |
-
session_name,
|
202 |
-
"1_Home.py",
|
203 |
-
created_time,
|
204 |
-
created_time,
|
205 |
-
allowed_user,
|
206 |
-
),
|
207 |
-
)
|
208 |
-
conn.commit()
|
209 |
-
|
210 |
-
def update_project_name_box():
|
211 |
-
st.session_state["project_name_box"] = ""
|
212 |
-
|
213 |
-
# st.success('Project created')
|
214 |
-
|
215 |
-
if "session_path" not in st.session_state:
|
216 |
-
st.session_state["session_path"] = None
|
217 |
-
|
218 |
-
# creating dir for user
|
219 |
-
|
220 |
-
if not os.path.exists(user_folder_path):
|
221 |
-
os.makedirs(user_folder_path)
|
222 |
-
|
223 |
-
c.execute("SELECT DISTINCT username FROM users ")
|
224 |
-
allowed_users_db = [user[0] for user in c.fetchall() if user[0] != name]
|
225 |
-
|
226 |
-
c.execute(
|
227 |
-
"SELECT session_name from sessions WHERE allowed_users = ?", (name,)
|
228 |
-
)
|
229 |
-
available_sessions = c.fetchall() # all sessions available for user
|
230 |
-
|
231 |
-
c.execute(
|
232 |
-
"SELECT Distinct Session_name, status, updated_time as last_updated FROM sessions WHERE owner=?",
|
233 |
-
(name,),
|
234 |
-
)
|
235 |
-
|
236 |
-
session_summary = c.fetchall()
|
237 |
-
|
238 |
-
session_summary_df = pd.DataFrame(
|
239 |
-
session_summary,
|
240 |
-
columns=["Project Name", "Last Page Edited", "Modified Date"],
|
241 |
-
)
|
242 |
-
session_summary_df["Modified Date"] = session_summary_df[
|
243 |
-
"Modified Date"
|
244 |
-
].map(lambda x: pd.to_datetime(x))
|
245 |
-
|
246 |
-
session_summary_df = session_summary_df.sort_values(
|
247 |
-
by=["Modified Date"], ascending=False
|
248 |
-
)
|
249 |
-
|
250 |
-
session_summary_df["Last Page Modified"] = session_summary_df[
|
251 |
-
"Last Page Edited"
|
252 |
-
].map(lambda x: x[2:].replace("_", " ").replace(".py", ""))
|
253 |
-
|
254 |
-
st.header("Manage Projects")
|
255 |
-
|
256 |
-
st.markdown(
|
257 |
-
"""
|
258 |
-
* **Load Existing Project:** Select the project you want and click 'Load Project'.
|
259 |
-
* **Delete Project:** If you wish to delete a project, select it and click 'Delete Project'.
|
260 |
-
* **Modify User Access:** Make changes to user access permissions as needed.
|
261 |
-
|
262 |
-
"""
|
263 |
-
)
|
264 |
-
|
265 |
-
# session_col=st.columns([5,5])
|
266 |
-
# with session_col[0]:
|
267 |
-
gd = GridOptionsBuilder.from_dataframe(session_summary_df)
|
268 |
-
gd.configure_pagination(
|
269 |
-
enabled=True, paginationAutoPageSize=False, paginationPageSize=10
|
270 |
-
)
|
271 |
-
gd.configure_selection(use_checkbox=True)
|
272 |
-
|
273 |
-
gridoptions = gd.build()
|
274 |
-
|
275 |
-
column_defs = gridoptions["columnDefs"]
|
276 |
-
columns_to_hide = ["Last Page Edited"]
|
277 |
-
for col in column_defs:
|
278 |
-
if col["headerName"] in columns_to_hide:
|
279 |
-
col["hide"] = True
|
280 |
-
|
281 |
-
if session_summary_df.shape[0] < 5:
|
282 |
-
height = (session_summary_df.shape[0]) * 20 + 100
|
283 |
-
|
284 |
-
else:
|
285 |
-
height = None
|
286 |
-
|
287 |
-
table = AgGrid(
|
288 |
-
session_summary_df,
|
289 |
-
gridOptions=gridoptions,
|
290 |
-
update_mode=GridUpdateMode.SELECTION_CHANGED,
|
291 |
-
height=height,
|
292 |
-
fit_columns_on_grid_load=True,
|
293 |
-
)
|
294 |
-
|
295 |
-
if len(table.selected_rows) > 0:
|
296 |
-
|
297 |
-
selected_rows = table.selected_rows
|
298 |
-
|
299 |
-
project_name = selected_rows[0]["Project Name"]
|
300 |
-
|
301 |
-
st.session_state["project_name"] = project_name
|
302 |
-
|
303 |
-
project_col = st.columns(2)
|
304 |
-
|
305 |
-
with project_col[0]:
|
306 |
-
|
307 |
-
project_path = os.path.join(user_folder_path, project_name)
|
308 |
-
|
309 |
-
st.session_state["project_path"] = project_path # load project dct
|
310 |
-
|
311 |
-
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
312 |
-
|
313 |
-
with open(project_dct_path, "rb") as f:
|
314 |
-
st.session_state["project_dct"] = pickle.load(f)
|
315 |
-
|
316 |
-
# st.write(st.session_state['project_dct'])
|
317 |
-
|
318 |
-
with st.spinner("Redirecting to last Saved Page"):
|
319 |
-
|
320 |
-
try:
|
321 |
-
page_link = st.page_link(
|
322 |
-
f"pages/{selected_rows[0]['Last Page Edited']}",
|
323 |
-
label=f"Load Project - **{project_name}**",
|
324 |
-
)
|
325 |
-
except Exception as e:
|
326 |
-
try:
|
327 |
-
pag_link = st.page_link(
|
328 |
-
"pages/1_Data_Import.py",
|
329 |
-
label=f"Load Project - **{project_name}**",
|
330 |
-
)
|
331 |
-
except Exception as e:
|
332 |
-
st.error("Something went wrong")
|
333 |
-
|
334 |
-
with project_col[1]:
|
335 |
-
|
336 |
-
if st.button(
|
337 |
-
f"Delete Project - **{selected_rows[0]['Project Name']}**"):
|
338 |
-
|
339 |
-
project_name_to_delete = selected_rows[0]["Project Name"]
|
340 |
-
st.warning(
|
341 |
-
f"{project_name_to_delete} will be deleted permanentaly and all the information regarding the project will be lost"
|
342 |
-
)
|
343 |
-
c.execute(
|
344 |
-
"Delete FROM sessions WHERE session_name =? AND owner =?",
|
345 |
-
(
|
346 |
-
project_name_to_delete,
|
347 |
-
st.session_state["name"],
|
348 |
-
),
|
349 |
-
)
|
350 |
-
if os.path.exists(project_path):
|
351 |
-
shutil.rmtree(project_path)
|
352 |
-
|
353 |
-
conn.commit()
|
354 |
-
st.rerun()
|
355 |
-
|
356 |
-
|
357 |
-
with st.expander("Modify user access for selected project"):
|
358 |
-
|
359 |
-
c.execute(
|
360 |
-
"SELECT DISTINCT allowed_users FROM sessions WHERE session_name = ?",
|
361 |
-
(project_name,),
|
362 |
-
)
|
363 |
-
|
364 |
-
present_users = c.fetchall()
|
365 |
-
|
366 |
-
present_users = [
|
367 |
-
user[0]
|
368 |
-
for user in present_users
|
369 |
-
if user[0] != name and user[0] is not None
|
370 |
-
]
|
371 |
-
|
372 |
-
present_users = None if len(present_users) == 0 else present_users
|
373 |
-
|
374 |
-
allowed_users = st.multiselect(
|
375 |
-
"Modify other users access",
|
376 |
-
allowed_users_db,
|
377 |
-
default=present_users,
|
378 |
-
)
|
379 |
-
|
380 |
-
if st.button("Save Changes", use_container_width=True):
|
381 |
-
pass
|
382 |
-
|
383 |
-
c.execute("SELECT Session_name FROM sessions WHERE owner=?", (name,))
|
384 |
-
|
385 |
-
user_projects = [
|
386 |
-
project[0] for project in c.fetchall()
|
387 |
-
] # user owned sessions
|
388 |
-
|
389 |
-
with st.expander("Create New Project"):
|
390 |
-
st.markdown(
|
391 |
-
"To create a new project, Enter Project name below, select user who you want to give access of this project and click **Create New Project**"
|
392 |
-
)
|
393 |
-
|
394 |
-
project_col1 = st.columns(2)
|
395 |
-
with project_col1[0]:
|
396 |
-
project_name = st.text_input("Enter Project Name",key="project_name_box")
|
397 |
-
|
398 |
-
if project_name in user_projects:
|
399 |
-
st.warning("Project already exists please enter new name")
|
400 |
-
|
401 |
-
with project_col1[1]:
|
402 |
-
|
403 |
-
allowed_users = st.multiselect(
|
404 |
-
"Select Users who can access to this Project", allowed_users_db
|
405 |
-
)
|
406 |
-
allowed_users = list(allowed_users)
|
407 |
-
|
408 |
-
Create = st.button("Create New Project", use_container_width=True)
|
409 |
-
|
410 |
-
if Create:
|
411 |
-
if len(project_name) == 0:
|
412 |
-
st.error("Plase enter a valid project name")
|
413 |
-
st.stop()
|
414 |
-
allowed_users.append(name)
|
415 |
-
|
416 |
-
if project_name in user_projects:
|
417 |
-
|
418 |
-
st.warning("Project already exists please enter new name")
|
419 |
-
st.stop()
|
420 |
-
|
421 |
-
project_path = os.path.join(user_folder_path, project_name)
|
422 |
-
|
423 |
-
os.makedirs(project_path)
|
424 |
-
|
425 |
-
dump_session_details_db(allowed_users, project_name)
|
426 |
-
|
427 |
-
project_dct = {
|
428 |
-
"data_import": {
|
429 |
-
"granularity_selection": 0,
|
430 |
-
"cat_dct": {},
|
431 |
-
"merged_df": None,
|
432 |
-
"edited_df": None,
|
433 |
-
"numeric_columns": None,
|
434 |
-
"files_dict": None,
|
435 |
-
"formatted_panel1_values": None,
|
436 |
-
"formatted_panel2_values": None,
|
437 |
-
"missing_stats_df": None,
|
438 |
-
"edited_stats_df": None,
|
439 |
-
"default_df": None,
|
440 |
-
"final_df": None,
|
441 |
-
"edited_df": None,
|
442 |
-
},
|
443 |
-
"data_validation": {
|
444 |
-
"target_column": 0,
|
445 |
-
"selected_panels": None,
|
446 |
-
"selected_feature": 0,
|
447 |
-
"validated_variables": [],
|
448 |
-
"Non_media_variables": 0,
|
449 |
-
},
|
450 |
-
"transformations": {"Media": {}, "Exogenous": {}},
|
451 |
-
"model_build": {
|
452 |
-
"sel_target_col": None,
|
453 |
-
"all_iters_check": False,
|
454 |
-
"iterations": 0,
|
455 |
-
"build_button": False,
|
456 |
-
"show_results_check": False,
|
457 |
-
"session_state_saved": {},
|
458 |
-
},
|
459 |
-
"model_tuning": {
|
460 |
-
"sel_target_col": None,
|
461 |
-
"sel_model": {},
|
462 |
-
"flag_expander": False,
|
463 |
-
"start_date_default": None,
|
464 |
-
"end_date_default": None,
|
465 |
-
"repeat_default": "No",
|
466 |
-
"flags": {},
|
467 |
-
"select_all_flags_check": {},
|
468 |
-
"selected_flags": {},
|
469 |
-
"trend_check": False,
|
470 |
-
"week_num_check": False,
|
471 |
-
"sine_cosine_check": False,
|
472 |
-
"session_state_saved": {},
|
473 |
-
},
|
474 |
-
"saved_model_results": {
|
475 |
-
"selected_options": None,
|
476 |
-
"model_grid_sel": [1],
|
477 |
-
},
|
478 |
-
"model_result_overview": {},
|
479 |
-
"build_response_curves": {
|
480 |
-
"response_metrics_selectbox": 0,
|
481 |
-
"panel_selected_selectbox": 0,
|
482 |
-
"selected_channel_name_selectbox": 0,
|
483 |
-
"K_number_input": "default",
|
484 |
-
"b_number_input": "default",
|
485 |
-
"a_number_input": "default",
|
486 |
-
"x0_number_input": "default",
|
487 |
-
},
|
488 |
-
"scenario_planner": {
|
489 |
-
"panel_selected": 0,
|
490 |
-
"metrics_selected": 0,
|
491 |
-
"scenario": None,
|
492 |
-
"optimization_key_value": None,
|
493 |
-
"total_spends_change": None,
|
494 |
-
"optimze_all_channels": False,
|
495 |
-
},
|
496 |
-
"saved_scenarios": {
|
497 |
-
"selected_scenario_selectbox_key": 0,
|
498 |
-
},
|
499 |
-
"optimized_result_analysis": {
|
500 |
-
"selected_scenario_selectbox_visualize": 0,
|
501 |
-
"metric_selectbox_visualize": 0,
|
502 |
-
},
|
503 |
-
}
|
504 |
-
|
505 |
-
st.session_state["project_dct"] = project_dct
|
506 |
-
|
507 |
-
st.session_state["project_path"] = project_path
|
508 |
-
|
509 |
-
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
510 |
-
|
511 |
-
with open(project_dct_path, "wb") as f:
|
512 |
-
pickle.dump(project_dct, f)
|
513 |
-
|
514 |
-
st.success("Project Created")
|
515 |
-
st.rerun()
|
516 |
-
# st.session_state['project_name_box']=''
|
517 |
-
# st.rerun()
|
518 |
-
|
519 |
-
# st.header('Clone Project')
|
520 |
-
|
521 |
-
with st.expander("**Clone saved projects**"):
|
522 |
-
|
523 |
-
c.execute(
|
524 |
-
"SELECT DISTINCT owner FROM sessions WHERE allowed_users=?",
|
525 |
-
(name,),
|
526 |
-
) # owner
|
527 |
-
owners = c.fetchall()
|
528 |
-
|
529 |
-
owners = [owner[0] for owner in owners]
|
530 |
-
|
531 |
-
if len(owners) == 0:
|
532 |
-
|
533 |
-
st.warning("You dont have any shared project yet!")
|
534 |
-
|
535 |
-
st.stop()
|
536 |
-
|
537 |
-
cols = st.columns(2)
|
538 |
-
|
539 |
-
with cols[0]:
|
540 |
-
|
541 |
-
owner = st.selectbox("Select Owner", owners)
|
542 |
-
|
543 |
-
c.execute("SELECT email FROM users WHERE username=?", (owner,))
|
544 |
-
|
545 |
-
owner_email = c.fetchone()[0]
|
546 |
-
|
547 |
-
owner_folder_path = os.path.join(folder_path, owner_email)
|
548 |
-
|
549 |
-
with cols[1]:
|
550 |
-
|
551 |
-
c.execute(
|
552 |
-
"SELECT session_name FROM sessions WHERE owner=? AND allowed_users = ?",
|
553 |
-
(owner, name),
|
554 |
-
) # available sessions for user
|
555 |
-
project_names = c.fetchall()
|
556 |
-
|
557 |
-
project_name_owner = st.selectbox(
|
558 |
-
"Select a saved Project available for you",
|
559 |
-
[project_name[0] for project_name in project_names],
|
560 |
-
)
|
561 |
-
owner_project_path = os.path.join(owner_folder_path, project_name)
|
562 |
-
|
563 |
-
with cols[0]:
|
564 |
-
project_name_user = st.text_input(
|
565 |
-
"Enter Project Name", value=project_name_owner
|
566 |
-
)
|
567 |
-
|
568 |
-
if project_name in user_projects:
|
569 |
-
|
570 |
-
st.warning(
|
571 |
-
"This Project name already exists in your directory Please enter a different name"
|
572 |
-
)
|
573 |
-
|
574 |
-
# st.stop()
|
575 |
-
|
576 |
-
project_path = os.path.join(user_folder_path, project_name_user)
|
577 |
-
|
578 |
-
owner_project_path = os.path.join(
|
579 |
-
owner_folder_path, project_name_owner
|
580 |
-
)
|
581 |
-
|
582 |
-
with cols[1]:
|
583 |
-
|
584 |
-
allowed_users = st.multiselect(
|
585 |
-
"Select Users who can access this session", allowed_users_db
|
586 |
-
)
|
587 |
-
allowed_users = list(allowed_users)
|
588 |
-
|
589 |
-
if st.button("Load Project", use_container_width=True):
|
590 |
-
|
591 |
-
if os.path.exists(project_path):
|
592 |
-
|
593 |
-
st.warning(
|
594 |
-
"This Project name already exists in your directory Please enter a different name"
|
595 |
-
)
|
596 |
-
|
597 |
-
st.stop()
|
598 |
-
|
599 |
-
shutil.copytree(owner_project_path, project_path)
|
600 |
-
|
601 |
-
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
602 |
-
|
603 |
-
with open(project_dct_path, "rb") as f:
|
604 |
-
st.session_state["project_dct"] = pickle.load(f)
|
605 |
-
|
606 |
-
st.session_state["project_path"] = project_path
|
607 |
-
|
608 |
-
# st.write(st.session_state['project_dct'])
|
609 |
-
|
610 |
-
dump_session_details_db(allowed_users, project_name_user)
|
611 |
-
st.success("Project Cloned")
|
612 |
-
|
613 |
-
elif auth_status == False:
|
614 |
-
st.error("Username/Password is incorrect")
|
615 |
-
|
616 |
-
if auth_status != True:
|
617 |
-
try:
|
618 |
-
username_forgot_pw, email_forgot_password, random_password = (
|
619 |
-
authenticator.forgot_password("Forgot password")
|
620 |
-
)
|
621 |
-
if username_forgot_pw:
|
622 |
-
st.session_state["config"]["credentials"]["usernames"][
|
623 |
-
username_forgot_pw
|
624 |
-
]["password"] = stauth.Hasher([random_password]).generate()[0]
|
625 |
-
send_email(email_forgot_password, random_password)
|
626 |
-
st.success("New password sent securely")
|
627 |
-
# Random password to be transferred to user securely
|
628 |
-
elif username_forgot_pw == False:
|
629 |
-
st.error("Username not found")
|
630 |
-
except Exception as e:
|
631 |
-
st.error(e)
|
|
|
1 |
+
import sqlite3
|
2 |
+
import uuid
|
3 |
+
import streamlit as st
|
4 |
+
from utilities import (
|
5 |
+
load_local_css,
|
6 |
+
set_header,
|
7 |
+
load_authenticator,
|
8 |
+
send_email,
|
9 |
+
)
|
10 |
+
import streamlit_authenticator as stauth
|
11 |
+
import yaml
|
12 |
+
from yaml import SafeLoader
|
13 |
+
import os
|
14 |
+
import datetime
|
15 |
+
import shutil
|
16 |
+
import pandas as pd
|
17 |
+
from st_aggrid import AgGrid
|
18 |
+
from st_aggrid import GridOptionsBuilder, GridUpdateMode
|
19 |
+
import pickle
|
20 |
+
from pathlib import Path
|
21 |
+
|
22 |
+
st.set_page_config(layout="wide")
|
23 |
+
load_local_css("styles.css")
|
24 |
+
set_header()
|
25 |
+
|
26 |
+
# def authenticator():
|
27 |
+
for k, v in st.session_state.items():
|
28 |
+
if k not in ["logout", "login", "config"] and not k.startswith(
|
29 |
+
"FormSubmitter"
|
30 |
+
):
|
31 |
+
st.session_state[k] = v
|
32 |
+
with open("config.yaml") as file:
|
33 |
+
config = yaml.load(file, Loader=SafeLoader)
|
34 |
+
st.session_state["config"] = config
|
35 |
+
authenticator = stauth.Authenticate(
|
36 |
+
config["credentials"],
|
37 |
+
config["cookie"]["name"],
|
38 |
+
config["cookie"]["key"],
|
39 |
+
config["cookie"]["expiry_days"],
|
40 |
+
config["preauthorized"],
|
41 |
+
)
|
42 |
+
st.session_state["authenticator"] = authenticator
|
43 |
+
name, authentication_status, username = authenticator.login("Login", "main")
|
44 |
+
auth_status = st.session_state.get("authentication_status")
|
45 |
+
|
46 |
+
st.session_state["name"] = name
|
47 |
+
if auth_status == True:
|
48 |
+
authenticator.logout("Logout", "main")
|
49 |
+
is_state_initiaized = st.session_state.get("initialized", False)
|
50 |
+
|
51 |
+
if not is_state_initiaized:
|
52 |
+
|
53 |
+
if "session_name" not in st.session_state:
|
54 |
+
st.session_state["session_name"] = None
|
55 |
+
|
56 |
+
cols1 = st.columns([2, 1])
|
57 |
+
|
58 |
+
with cols1[0]:
|
59 |
+
st.markdown(f"**Welcome {name}**")
|
60 |
+
with cols1[1]:
|
61 |
+
st.markdown(
|
62 |
+
f"**Current Session: {st.session_state['session_name']}**"
|
63 |
+
)
|
64 |
+
|
65 |
+
# relative_path = Path('DB_Sample','..' ,'DB', 'User.db')
|
66 |
+
# absolute_path = Path.cwd() / relative_path
|
67 |
+
# st.write(absolute_path)
|
68 |
+
# database_file=Path(__file__).parent / relative_path
|
69 |
+
|
70 |
+
database_file = r"DB/User.db"
|
71 |
+
|
72 |
+
conn = sqlite3.connect(
|
73 |
+
database_file, check_same_thread=False
|
74 |
+
) # connection with sql db
|
75 |
+
c = conn.cursor()
|
76 |
+
|
77 |
+
# c.execute('DELETE FROM sessions')
|
78 |
+
# conn.commit()
|
79 |
+
# c.executemany("INSERT INTO users (username, email) VALUES (?, ?)",
|
80 |
+
# [("Geetha Krishna", "[email protected]"),
|
81 |
+
# ("Samkeet Sangai", "[email protected]"),
|
82 |
+
# ('Manoj P','[email protected]'),
|
83 |
+
# ('Srishti Verma','[email protected]'),
|
84 |
+
# ('Ismail mohammed',"[email protected]"),
|
85 |
+
# ('Sharon Sheng','[email protected]'),
|
86 |
+
# ('Ioannis Papadopoulos','[email protected]'),
|
87 |
+
# ('Herman Kwong',"[email protected]")
|
88 |
+
# ])
|
89 |
+
|
90 |
+
# conn.commit()
|
91 |
+
|
92 |
+
# c.execute(f"PRAGMA table_info({'sessions'})")
|
93 |
+
# conn.commit()
|
94 |
+
|
95 |
+
# st.write(c.fetchall())
|
96 |
+
|
97 |
+
# c.execute("Select * from users")
|
98 |
+
# st.write(c.fetchall())
|
99 |
+
|
100 |
+
page_name = "Home Page"
|
101 |
+
|
102 |
+
c.execute(
|
103 |
+
"SELECT email, user_id, user_type FROM users WHERE username = ?",
|
104 |
+
(name,),
|
105 |
+
)
|
106 |
+
user_data = c.fetchone()
|
107 |
+
|
108 |
+
email, user_id, user_type = user_data
|
109 |
+
|
110 |
+
# st.write(user_type)
|
111 |
+
# with st.sidebar:
|
112 |
+
# # if user_type != 'technical':
|
113 |
+
# st.page_link("home.py", label="Home123")
|
114 |
+
# st.page_link('pages/1_Data_Import.py',label='Data Import')
|
115 |
+
# st.page_link('pages/2_Data_Validation.py',label="Data Validation")
|
116 |
+
# st.page_link('pages/3_Transformations.py',label='Transformations')
|
117 |
+
# st.page_link("pages/4_Model_Build.py")
|
118 |
+
# st.page_link('pages/5_Model_Tuning_with_panel.py',label='Model Tuning')
|
119 |
+
|
120 |
+
# st.page_link('pages/5_Saved_Model_Results.py',label="Saved Model Results")
|
121 |
+
|
122 |
+
# st.write(pd.to_datetime(created_time))
|
123 |
+
c.execute("DELETE FROM sessions")
|
124 |
+
# # c.execute('select * from sessions')
|
125 |
+
conn.commit()
|
126 |
+
# output = c.fetchall()
|
127 |
+
|
128 |
+
# st.write(output)
|
129 |
+
|
130 |
+
# if emails is not None:
|
131 |
+
# email = emails[0]
|
132 |
+
|
133 |
+
folder_path = r"Users"
|
134 |
+
user_folder_path = os.path.join(folder_path, email)
|
135 |
+
|
136 |
+
# project_dct = {
|
137 |
+
# 'data_import': {
|
138 |
+
# "granularity_selection":0,
|
139 |
+
# 'cat_dct':{},
|
140 |
+
# "merged_df":None,
|
141 |
+
# 'edited_df':None,
|
142 |
+
# "numeric_columns":None,
|
143 |
+
# "files_dict":None,
|
144 |
+
# 'formatted_panel1_values':None,
|
145 |
+
# 'formatted_panel2_values':None,
|
146 |
+
# "missing_stats_df":None,
|
147 |
+
# 'edited_stats_df':None
|
148 |
+
|
149 |
+
# },
|
150 |
+
|
151 |
+
# 'data_validation': {"target_column":0,
|
152 |
+
# 'selected_panels':None,
|
153 |
+
# "selected_feature":0,
|
154 |
+
# "validated_variables":[],
|
155 |
+
# "Non_media_variables":0
|
156 |
+
|
157 |
+
# },
|
158 |
+
# 'transformations': {},
|
159 |
+
# 'model_build': {},
|
160 |
+
# 'model_tuning':{},
|
161 |
+
# 'saved_model_results': {},
|
162 |
+
# 'model_result_overview': {},
|
163 |
+
# 'build_response_curves': {},
|
164 |
+
# 'scenario_planner': {},
|
165 |
+
# 'saved_scenarios': {},
|
166 |
+
# 'optimized_result_analysis': {}
|
167 |
+
# }
|
168 |
+
# st.session_state['project_dct']=project_dct
|
169 |
+
|
170 |
+
# st.write(project_dct)
|
171 |
+
|
172 |
+
def dump_session_details_db(allowed_users, session_name):
|
173 |
+
|
174 |
+
created_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
|
175 |
+
|
176 |
+
session_id = str(uuid.uuid4())
|
177 |
+
|
178 |
+
if len(allowed_users) == 0:
|
179 |
+
c.execute(
|
180 |
+
"INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?,?)",
|
181 |
+
(
|
182 |
+
user_id,
|
183 |
+
name,
|
184 |
+
session_id,
|
185 |
+
session_name,
|
186 |
+
page_name,
|
187 |
+
created_time,
|
188 |
+
created_time,
|
189 |
+
None,
|
190 |
+
),
|
191 |
+
)
|
192 |
+
conn.commit()
|
193 |
+
else:
|
194 |
+
for allowed_user in allowed_users:
|
195 |
+
c.execute(
|
196 |
+
"INSERT INTO sessions VALUES (?, ?, ?, ?, ?, ?, ?,?)",
|
197 |
+
(
|
198 |
+
user_id,
|
199 |
+
name,
|
200 |
+
session_id,
|
201 |
+
session_name,
|
202 |
+
"1_Home.py",
|
203 |
+
created_time,
|
204 |
+
created_time,
|
205 |
+
allowed_user,
|
206 |
+
),
|
207 |
+
)
|
208 |
+
conn.commit()
|
209 |
+
|
210 |
+
def update_project_name_box():
|
211 |
+
st.session_state["project_name_box"] = ""
|
212 |
+
|
213 |
+
# st.success('Project created')
|
214 |
+
|
215 |
+
if "session_path" not in st.session_state:
|
216 |
+
st.session_state["session_path"] = None
|
217 |
+
|
218 |
+
# creating dir for user
|
219 |
+
|
220 |
+
if not os.path.exists(user_folder_path):
|
221 |
+
os.makedirs(user_folder_path)
|
222 |
+
|
223 |
+
c.execute("SELECT DISTINCT username FROM users ")
|
224 |
+
allowed_users_db = [user[0] for user in c.fetchall() if user[0] != name]
|
225 |
+
|
226 |
+
c.execute(
|
227 |
+
"SELECT session_name from sessions WHERE allowed_users = ?", (name,)
|
228 |
+
)
|
229 |
+
available_sessions = c.fetchall() # all sessions available for user
|
230 |
+
|
231 |
+
c.execute(
|
232 |
+
"SELECT Distinct Session_name, status, updated_time as last_updated FROM sessions WHERE owner=?",
|
233 |
+
(name,),
|
234 |
+
)
|
235 |
+
|
236 |
+
session_summary = c.fetchall()
|
237 |
+
|
238 |
+
session_summary_df = pd.DataFrame(
|
239 |
+
session_summary,
|
240 |
+
columns=["Project Name", "Last Page Edited", "Modified Date"],
|
241 |
+
)
|
242 |
+
session_summary_df["Modified Date"] = session_summary_df[
|
243 |
+
"Modified Date"
|
244 |
+
].map(lambda x: pd.to_datetime(x))
|
245 |
+
|
246 |
+
session_summary_df = session_summary_df.sort_values(
|
247 |
+
by=["Modified Date"], ascending=False
|
248 |
+
)
|
249 |
+
|
250 |
+
session_summary_df["Last Page Modified"] = session_summary_df[
|
251 |
+
"Last Page Edited"
|
252 |
+
].map(lambda x: x[2:].replace("_", " ").replace(".py", ""))
|
253 |
+
|
254 |
+
st.header("Manage Projects")
|
255 |
+
|
256 |
+
st.markdown(
|
257 |
+
"""
|
258 |
+
* **Load Existing Project:** Select the project you want and click 'Load Project'.
|
259 |
+
* **Delete Project:** If you wish to delete a project, select it and click 'Delete Project'.
|
260 |
+
* **Modify User Access:** Make changes to user access permissions as needed.
|
261 |
+
|
262 |
+
"""
|
263 |
+
)
|
264 |
+
|
265 |
+
# session_col=st.columns([5,5])
|
266 |
+
# with session_col[0]:
|
267 |
+
gd = GridOptionsBuilder.from_dataframe(session_summary_df)
|
268 |
+
gd.configure_pagination(
|
269 |
+
enabled=True, paginationAutoPageSize=False, paginationPageSize=10
|
270 |
+
)
|
271 |
+
gd.configure_selection(use_checkbox=True)
|
272 |
+
|
273 |
+
gridoptions = gd.build()
|
274 |
+
|
275 |
+
column_defs = gridoptions["columnDefs"]
|
276 |
+
columns_to_hide = ["Last Page Edited"]
|
277 |
+
for col in column_defs:
|
278 |
+
if col["headerName"] in columns_to_hide:
|
279 |
+
col["hide"] = True
|
280 |
+
|
281 |
+
if session_summary_df.shape[0] < 5:
|
282 |
+
height = (session_summary_df.shape[0]) * 20 + 100
|
283 |
+
|
284 |
+
else:
|
285 |
+
height = None
|
286 |
+
|
287 |
+
table = AgGrid(
|
288 |
+
session_summary_df,
|
289 |
+
gridOptions=gridoptions,
|
290 |
+
update_mode=GridUpdateMode.SELECTION_CHANGED,
|
291 |
+
height=height,
|
292 |
+
fit_columns_on_grid_load=True,
|
293 |
+
)
|
294 |
+
|
295 |
+
if len(table.selected_rows) > 0:
|
296 |
+
|
297 |
+
selected_rows = table.selected_rows
|
298 |
+
|
299 |
+
project_name = selected_rows[0]["Project Name"]
|
300 |
+
|
301 |
+
st.session_state["project_name"] = project_name
|
302 |
+
|
303 |
+
project_col = st.columns(2)
|
304 |
+
|
305 |
+
with project_col[0]:
|
306 |
+
|
307 |
+
project_path = os.path.join(user_folder_path, project_name)
|
308 |
+
|
309 |
+
st.session_state["project_path"] = project_path # load project dct
|
310 |
+
|
311 |
+
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
312 |
+
|
313 |
+
with open(project_dct_path, "rb") as f:
|
314 |
+
st.session_state["project_dct"] = pickle.load(f)
|
315 |
+
|
316 |
+
# st.write(st.session_state['project_dct'])
|
317 |
+
|
318 |
+
with st.spinner("Redirecting to last Saved Page"):
|
319 |
+
|
320 |
+
try:
|
321 |
+
page_link = st.page_link(
|
322 |
+
f"pages/{selected_rows[0]['Last Page Edited']}",
|
323 |
+
label=f"Load Project - **{project_name}**",
|
324 |
+
)
|
325 |
+
except Exception as e:
|
326 |
+
try:
|
327 |
+
pag_link = st.page_link(
|
328 |
+
"pages/1_Data_Import.py",
|
329 |
+
label=f"Load Project - **{project_name}**",
|
330 |
+
)
|
331 |
+
except Exception as e:
|
332 |
+
st.error("Something went wrong")
|
333 |
+
|
334 |
+
with project_col[1]:
|
335 |
+
|
336 |
+
if st.button(
|
337 |
+
f"Delete Project - **{selected_rows[0]['Project Name']}**"):
|
338 |
+
|
339 |
+
project_name_to_delete = selected_rows[0]["Project Name"]
|
340 |
+
st.warning(
|
341 |
+
f"{project_name_to_delete} will be deleted permanentaly and all the information regarding the project will be lost"
|
342 |
+
)
|
343 |
+
c.execute(
|
344 |
+
"Delete FROM sessions WHERE session_name =? AND owner =?",
|
345 |
+
(
|
346 |
+
project_name_to_delete,
|
347 |
+
st.session_state["name"],
|
348 |
+
),
|
349 |
+
)
|
350 |
+
if os.path.exists(project_path):
|
351 |
+
shutil.rmtree(project_path)
|
352 |
+
|
353 |
+
conn.commit()
|
354 |
+
st.rerun()
|
355 |
+
|
356 |
+
|
357 |
+
with st.expander("Modify user access for selected project"):
|
358 |
+
|
359 |
+
c.execute(
|
360 |
+
"SELECT DISTINCT allowed_users FROM sessions WHERE session_name = ?",
|
361 |
+
(project_name,),
|
362 |
+
)
|
363 |
+
|
364 |
+
present_users = c.fetchall()
|
365 |
+
|
366 |
+
present_users = [
|
367 |
+
user[0]
|
368 |
+
for user in present_users
|
369 |
+
if user[0] != name and user[0] is not None
|
370 |
+
]
|
371 |
+
|
372 |
+
present_users = None if len(present_users) == 0 else present_users
|
373 |
+
|
374 |
+
allowed_users = st.multiselect(
|
375 |
+
"Modify other users access",
|
376 |
+
allowed_users_db,
|
377 |
+
default=present_users,
|
378 |
+
)
|
379 |
+
|
380 |
+
if st.button("Save Changes", use_container_width=True):
|
381 |
+
pass
|
382 |
+
|
383 |
+
c.execute("SELECT Session_name FROM sessions WHERE owner=?", (name,))
|
384 |
+
|
385 |
+
user_projects = [
|
386 |
+
project[0] for project in c.fetchall()
|
387 |
+
] # user owned sessions
|
388 |
+
|
389 |
+
with st.expander("Create New Project"):
|
390 |
+
st.markdown(
|
391 |
+
"To create a new project, Enter Project name below, select user who you want to give access of this project and click **Create New Project**"
|
392 |
+
)
|
393 |
+
|
394 |
+
project_col1 = st.columns(2)
|
395 |
+
with project_col1[0]:
|
396 |
+
project_name = st.text_input("Enter Project Name",key="project_name_box")
|
397 |
+
|
398 |
+
if project_name in user_projects:
|
399 |
+
st.warning("Project already exists please enter new name")
|
400 |
+
|
401 |
+
with project_col1[1]:
|
402 |
+
|
403 |
+
allowed_users = st.multiselect(
|
404 |
+
"Select Users who can access to this Project", allowed_users_db
|
405 |
+
)
|
406 |
+
allowed_users = list(allowed_users)
|
407 |
+
|
408 |
+
Create = st.button("Create New Project", use_container_width=True)
|
409 |
+
|
410 |
+
if Create:
|
411 |
+
if len(project_name) == 0:
|
412 |
+
st.error("Plase enter a valid project name")
|
413 |
+
st.stop()
|
414 |
+
allowed_users.append(name)
|
415 |
+
|
416 |
+
if project_name in user_projects:
|
417 |
+
|
418 |
+
st.warning("Project already exists please enter new name")
|
419 |
+
st.stop()
|
420 |
+
|
421 |
+
project_path = os.path.join(user_folder_path, project_name)
|
422 |
+
|
423 |
+
os.makedirs(project_path)
|
424 |
+
|
425 |
+
dump_session_details_db(allowed_users, project_name)
|
426 |
+
|
427 |
+
project_dct = {
|
428 |
+
"data_import": {
|
429 |
+
"granularity_selection": 0,
|
430 |
+
"cat_dct": {},
|
431 |
+
"merged_df": None,
|
432 |
+
"edited_df": None,
|
433 |
+
"numeric_columns": None,
|
434 |
+
"files_dict": None,
|
435 |
+
"formatted_panel1_values": None,
|
436 |
+
"formatted_panel2_values": None,
|
437 |
+
"missing_stats_df": None,
|
438 |
+
"edited_stats_df": None,
|
439 |
+
"default_df": None,
|
440 |
+
"final_df": None,
|
441 |
+
"edited_df": None,
|
442 |
+
},
|
443 |
+
"data_validation": {
|
444 |
+
"target_column": 0,
|
445 |
+
"selected_panels": None,
|
446 |
+
"selected_feature": 0,
|
447 |
+
"validated_variables": [],
|
448 |
+
"Non_media_variables": 0,
|
449 |
+
},
|
450 |
+
"transformations": {"Media": {}, "Exogenous": {}},
|
451 |
+
"model_build": {
|
452 |
+
"sel_target_col": None,
|
453 |
+
"all_iters_check": False,
|
454 |
+
"iterations": 0,
|
455 |
+
"build_button": False,
|
456 |
+
"show_results_check": False,
|
457 |
+
"session_state_saved": {},
|
458 |
+
},
|
459 |
+
"model_tuning": {
|
460 |
+
"sel_target_col": None,
|
461 |
+
"sel_model": {},
|
462 |
+
"flag_expander": False,
|
463 |
+
"start_date_default": None,
|
464 |
+
"end_date_default": None,
|
465 |
+
"repeat_default": "No",
|
466 |
+
"flags": {},
|
467 |
+
"select_all_flags_check": {},
|
468 |
+
"selected_flags": {},
|
469 |
+
"trend_check": False,
|
470 |
+
"week_num_check": False,
|
471 |
+
"sine_cosine_check": False,
|
472 |
+
"session_state_saved": {},
|
473 |
+
},
|
474 |
+
"saved_model_results": {
|
475 |
+
"selected_options": None,
|
476 |
+
"model_grid_sel": [1],
|
477 |
+
},
|
478 |
+
"model_result_overview": {},
|
479 |
+
"build_response_curves": {
|
480 |
+
"response_metrics_selectbox": 0,
|
481 |
+
"panel_selected_selectbox": 0,
|
482 |
+
"selected_channel_name_selectbox": 0,
|
483 |
+
"K_number_input": "default",
|
484 |
+
"b_number_input": "default",
|
485 |
+
"a_number_input": "default",
|
486 |
+
"x0_number_input": "default",
|
487 |
+
},
|
488 |
+
"scenario_planner": {
|
489 |
+
"panel_selected": 0,
|
490 |
+
"metrics_selected": 0,
|
491 |
+
"scenario": None,
|
492 |
+
"optimization_key_value": None,
|
493 |
+
"total_spends_change": None,
|
494 |
+
"optimze_all_channels": False,
|
495 |
+
},
|
496 |
+
"saved_scenarios": {
|
497 |
+
"selected_scenario_selectbox_key": 0,
|
498 |
+
},
|
499 |
+
"optimized_result_analysis": {
|
500 |
+
"selected_scenario_selectbox_visualize": 0,
|
501 |
+
"metric_selectbox_visualize": 0,
|
502 |
+
},
|
503 |
+
}
|
504 |
+
|
505 |
+
st.session_state["project_dct"] = project_dct
|
506 |
+
|
507 |
+
st.session_state["project_path"] = project_path
|
508 |
+
|
509 |
+
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
510 |
+
|
511 |
+
with open(project_dct_path, "wb") as f:
|
512 |
+
pickle.dump(project_dct, f)
|
513 |
+
|
514 |
+
st.success("Project Created")
|
515 |
+
st.rerun()
|
516 |
+
# st.session_state['project_name_box']=''
|
517 |
+
# st.rerun()
|
518 |
+
|
519 |
+
# st.header('Clone Project')
|
520 |
+
|
521 |
+
with st.expander("**Clone saved projects**"):
|
522 |
+
|
523 |
+
c.execute(
|
524 |
+
"SELECT DISTINCT owner FROM sessions WHERE allowed_users=?",
|
525 |
+
(name,),
|
526 |
+
) # owner
|
527 |
+
owners = c.fetchall()
|
528 |
+
|
529 |
+
owners = [owner[0] for owner in owners]
|
530 |
+
|
531 |
+
if len(owners) == 0:
|
532 |
+
|
533 |
+
st.warning("You dont have any shared project yet!")
|
534 |
+
|
535 |
+
st.stop()
|
536 |
+
|
537 |
+
cols = st.columns(2)
|
538 |
+
|
539 |
+
with cols[0]:
|
540 |
+
|
541 |
+
owner = st.selectbox("Select Owner", owners)
|
542 |
+
|
543 |
+
c.execute("SELECT email FROM users WHERE username=?", (owner,))
|
544 |
+
|
545 |
+
owner_email = c.fetchone()[0]
|
546 |
+
|
547 |
+
owner_folder_path = os.path.join(folder_path, owner_email)
|
548 |
+
|
549 |
+
with cols[1]:
|
550 |
+
|
551 |
+
c.execute(
|
552 |
+
"SELECT session_name FROM sessions WHERE owner=? AND allowed_users = ?",
|
553 |
+
(owner, name),
|
554 |
+
) # available sessions for user
|
555 |
+
project_names = c.fetchall()
|
556 |
+
|
557 |
+
project_name_owner = st.selectbox(
|
558 |
+
"Select a saved Project available for you",
|
559 |
+
[project_name[0] for project_name in project_names],
|
560 |
+
)
|
561 |
+
owner_project_path = os.path.join(owner_folder_path, project_name)
|
562 |
+
|
563 |
+
with cols[0]:
|
564 |
+
project_name_user = st.text_input(
|
565 |
+
"Enter Project Name", value=project_name_owner
|
566 |
+
)
|
567 |
+
|
568 |
+
if project_name in user_projects:
|
569 |
+
|
570 |
+
st.warning(
|
571 |
+
"This Project name already exists in your directory Please enter a different name"
|
572 |
+
)
|
573 |
+
|
574 |
+
# st.stop()
|
575 |
+
|
576 |
+
project_path = os.path.join(user_folder_path, project_name_user)
|
577 |
+
|
578 |
+
owner_project_path = os.path.join(
|
579 |
+
owner_folder_path, project_name_owner
|
580 |
+
)
|
581 |
+
|
582 |
+
with cols[1]:
|
583 |
+
|
584 |
+
allowed_users = st.multiselect(
|
585 |
+
"Select Users who can access this session", allowed_users_db
|
586 |
+
)
|
587 |
+
allowed_users = list(allowed_users)
|
588 |
+
|
589 |
+
if st.button("Load Project", use_container_width=True):
|
590 |
+
|
591 |
+
if os.path.exists(project_path):
|
592 |
+
|
593 |
+
st.warning(
|
594 |
+
"This Project name already exists in your directory Please enter a different name"
|
595 |
+
)
|
596 |
+
|
597 |
+
st.stop()
|
598 |
+
|
599 |
+
shutil.copytree(owner_project_path, project_path)
|
600 |
+
|
601 |
+
project_dct_path = os.path.join(project_path, "project_dct.pkl")
|
602 |
+
|
603 |
+
with open(project_dct_path, "rb") as f:
|
604 |
+
st.session_state["project_dct"] = pickle.load(f)
|
605 |
+
|
606 |
+
st.session_state["project_path"] = project_path
|
607 |
+
|
608 |
+
# st.write(st.session_state['project_dct'])
|
609 |
+
|
610 |
+
dump_session_details_db(allowed_users, project_name_user)
|
611 |
+
st.success("Project Cloned")
|
612 |
+
|
613 |
+
elif auth_status == False:
|
614 |
+
st.error("Username/Password is incorrect")
|
615 |
+
|
616 |
+
if auth_status != True:
|
617 |
+
try:
|
618 |
+
username_forgot_pw, email_forgot_password, random_password = (
|
619 |
+
authenticator.forgot_password("Forgot password")
|
620 |
+
)
|
621 |
+
if username_forgot_pw:
|
622 |
+
st.session_state["config"]["credentials"]["usernames"][
|
623 |
+
username_forgot_pw
|
624 |
+
]["password"] = stauth.Hasher([random_password]).generate()[0]
|
625 |
+
send_email(email_forgot_password, random_password)
|
626 |
+
st.success("New password sent securely")
|
627 |
+
# Random password to be transferred to user securely
|
628 |
+
elif username_forgot_pw == False:
|
629 |
+
st.error("Username not found")
|
630 |
+
except Exception as e:
|
631 |
+
st.error(e)
|