-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstreamlit_app.py
117 lines (106 loc) · 6.05 KB
/
streamlit_app.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
import streamlit as st
import streamlit.components.v1 as components
from createdb import query, rent_cup, return_cup
def check_password():
"""Returns `True` if the user had a correct password."""
def password_entered():
"""Checks whether a password entered by the user is correct."""
userquery = f"SELECT user_name, password FROM customers_db where user_name = '{st.session_state['username']}';"
results = query(userquery)
passwords = {eachLine[0]: eachLine[1] for eachLine in results}
if (
(st.session_state["username"] in passwords.keys())
and (st.session_state["password"]
== passwords[st.session_state["username"]])
):
st.session_state["password_correct"] = True
del st.session_state["password"] # don't store username + password
# del st.session_state["username"]
else:
st.session_state["password_correct"] = False
if "password_correct" not in st.session_state:
with st.form("sign-in1"):
# First run, show inputs for username + password.
st.text_input("Username", key="username")
st.text_input("Password", type="password", key="password")
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
password_entered()
elif not st.session_state["password_correct"]:
# Password not correct, show input + error.
with st.form("sign-in2"):
# First run, show inputs for username + password.
st.text_input("Username", key="username")
st.text_input("Password", type="password", key="password")
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
if submitted:
password_entered()
else:
# Password correct.
return True
st.title("Cup Adventure")
if check_password():
if "user info" not in st.session_state:
userquery = f"SELECT customer_firstName, user_name, cup_rental FROM customers_db where user_name = '{st.session_state['username']}';"
results = query(userquery)
userInfo = {'firstName': results[0][0], 'username': results[0][1], 'status': results[0][2]}
st.session_state['user info'] = userInfo
if "user info" in st.session_state:
st.write(f"Welcome {st.session_state['user info']['firstName']}")
if st.session_state['user info']['status'] == "Available":
st.write("If you would like to rent a cup, please use the dropdown below.")
with st.form("rental"):
vendorquery = f"SELECT DISTINCT vendor_id, vendor_name FROM vendors_db;"
vendorresults = query(vendorquery)
vendors = {'id': [eachVendor[0] for eachVendor in vendorresults], 'name': [eachVendor[1] for eachVendor in vendorresults]}
vendor = st.selectbox("Please select a vendor", vendors['name'])
cupquery = f"SELECT cup_id FROM cups_db WHERE sold = 'no' AND cup_status = 'Available';"
cupresults = query(cupquery)
cups = [eachCup[0] for eachCup in cupresults]
cup = st.selectbox("Please select a cup", cups)
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
st.write(submitted)
if submitted:
rent_cup(st.session_state['user info']['username'], vendor, cup)
st.write("Thank you for renting your cup.")
st.session_state['user info']['status'] = cup
elif st.session_state['user info']['status'] == None:
st.write("Use the dropdown below to rent your first cup.")
with st.form("first_rental"):
# First run, show inputs for username + password.
vendorquery = f"SELECT DISTINCT vendor_id, vendor_name FROM vendors_db;"
vendorresults = query(vendorquery)
vendors = {'id': [eachVendor[0] for eachVendor in vendorresults], 'name': [eachVendor[1] for eachVendor in vendorresults]}
vendor = st.selectbox("Please select a vendor", vendors['name'])
cupquery = f"SELECT cup_id FROM cups_db WHERE sold = 'no' AND cup_status = 'Available';"
cupresults = query(cupquery)
cups = [eachCup[0] for eachCup in cupresults]
cup = st.selectbox("Please select a cup", cups)
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
st.write(submitted)
if submitted:
rent_cup(st.session_state['user info']['username'], vendor, cup)
st.write("Thank you for renting your cup.")
st.session_state['user info']['status'] = cup
elif st.session_state['user info']['status'] != "Available":
st.write("You currently have a cup borrowed. Please return your cup when you are finished with it.")
with st.form("return"):
# First run, show inputs for username + password.
vendorquery = f"SELECT DISTINCT vendor_name FROM vendors_db;"
vendorresults = query(vendorquery)
vendors = [eachVendor[0] for eachVendor in vendorresults]
vendor = st.selectbox("Please select a vendor", vendors)
cup = int(st.selectbox("Please select a cup", [st.session_state['user info']['status']]))
# Every form must have a submit button.
submitted = st.form_submit_button("Submit")
st.write(submitted)
if submitted:
return_cup(st.session_state['user info']['username'], vendor, cup)
st.write("Thank you for returning your cup.")
st.session_state['user info']['status'] = "Available"
else:
st.write("There has been an error tracking your last cup. Please contact us for help.")