DragandDropGroup commited on
Commit
582c2f7
·
verified ·
1 Parent(s): a519aec

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +176 -0
app.py ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import json
4
+ import web3
5
+
6
+ total_number_pages = 4
7
+ placeholder_buttons = None
8
+
9
+ Q1_radio_options = ["Pizza","Burgers","Pasta","Hot Dogs"]
10
+ Q3_radio_options = ["Yes","No"]
11
+ Q4_radio_options = ["N/A", "Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"]
12
+
13
+
14
+ # Function that records radio element changes
15
+ def radio_change(element, state, key):
16
+ st.session_state[state] = element.index(st.session_state[key]) # Setting previously selected option
17
+
18
+ def multi_change(element, state, key):
19
+ st.session_state[state] = []
20
+ for selected_option in st.session_state[key]:
21
+ st.session_state[state].append(selected_option)
22
+
23
+ # Function that disables the last button while data is uploaded to IPFS
24
+ def button_disable():
25
+ st.session_state['disabled'] = True
26
+
27
+ def answer_change(state, key):
28
+ st.session_state[state] = st.session_state[key]
29
+
30
+ st.set_page_config(page_title='IPFS-Based Survey',)
31
+ st.title('Survey Test')
32
+
33
+ st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True)
34
+ st.markdown("<style>.big-font {font-size:24px;}</style>", unsafe_allow_html=True)
35
+
36
+
37
+ if "current_page" not in st.session_state:
38
+ st.session_state["current_page"] = 1
39
+ st.session_state["Q1"] = None
40
+ st.session_state["Q2"] = None
41
+ st.session_state["Q3"] = None
42
+ st.session_state["Q4"] = None
43
+ st.session_state["disabled"] = False
44
+
45
+ # Page 1; Video
46
+ if st.session_state["current_page"] == 1:
47
+
48
+ st.markdown("""<p class="big-font">This is a test survey</p>""", unsafe_allow_html=True)
49
+
50
+ st.radio(label = "Which is your favorite food?",
51
+ options = Q1_radio_options,
52
+ index = None if st.session_state["Q1"] == None else st.session_state["Q1"],
53
+ key = 'Q1_radio',
54
+ on_change = radio_change,
55
+ args = (Q1_radio_options, "Q1", "Q1_radio",))
56
+
57
+ st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
58
+
59
+
60
+ st.text_area(label = "How is your day?",
61
+ value= "" if st.session_state["Q2"] == None else st.session_state["Q2"],
62
+ key = 'Q2_text',
63
+ on_change = answer_change,
64
+ args = ( "Q2", "Q2_text",))
65
+
66
+ st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
67
+
68
+
69
+ placeholder = st.empty()
70
+
71
+ if st.button('Next', key='next_button_page_1'):
72
+ all_answered = True
73
+ if st.session_state["Q1"] == None or st.session_state["Q1"] == []:
74
+ all_answered = False
75
+ if st.session_state["Q2"] == None or st.session_state["Q2"] == []:
76
+ all_answered = False
77
+ if all_answered:
78
+ st.session_state["current_page"] += 1
79
+ st.rerun()
80
+ else:
81
+ with placeholder.container():
82
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
83
+
84
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
85
+
86
+
87
+ elif st.session_state["current_page"] == 2:
88
+
89
+ st.video("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
90
+ st.markdown("""<p style='font-size:18px;'>Get rick rolled!</p>""", unsafe_allow_html=True)
91
+ st.multiselect(label = "Was that a good prank?",
92
+ default = None if st.session_state["Q3"] == None else st.session_state["Q3"],
93
+ options = Q3_radio_options,
94
+ key = 'Q3_multi',
95
+ on_change = multi_change,
96
+ args = (Q3_radio_options, "Q3", "Q3_multi",))
97
+
98
+ st.markdown("""<style> div[class*="stMulti"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
99
+
100
+
101
+ placeholder = st.empty()
102
+
103
+ col1, col2 = st.columns(2)
104
+ with col1:
105
+ if st.button('Back'):
106
+ st.session_state["current_page"] -= 1
107
+ st.rerun()
108
+ with col2:
109
+ if st.button('Next'):
110
+ all_answered = True
111
+ if st.session_state["Q3"] == None or st.session_state["Q3"] == []:
112
+ all_answered = False
113
+ if all_answered:
114
+ st.session_state["current_page"] += 1
115
+ st.rerun()
116
+ else:
117
+ with placeholder.container():
118
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
119
+
120
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
121
+
122
+
123
+ elif st.session_state["current_page"] == 3:
124
+
125
+ st.radio(label = "Miami is a great school?", # Likert
126
+ options = Q4_radio_options,
127
+ index = None if st.session_state["Q4"] == None else st.session_state["Q4"],
128
+ key = 'Q4_radio',
129
+ on_change = radio_change,
130
+ args = (Q4_radio_options, "Q4", "Q4_radio",))
131
+
132
+ st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
133
+
134
+
135
+ placeholder = st.empty()
136
+
137
+ col1, col2 = st.columns(2)
138
+ with col1:
139
+ if st.button('Back'):
140
+ st.session_state["current_page"] -= 1
141
+ st.rerun()
142
+ with col2:
143
+ if st.button('Next'):
144
+ all_answered = True
145
+ if st.session_state["Q4"] == None or st.session_state["Q4"] == []:
146
+ all_answered = False
147
+ if all_answered:
148
+ st.session_state["current_page"] += 1
149
+ st.rerun()
150
+ else:
151
+ with placeholder.container():
152
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
153
+
154
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
155
+
156
+
157
+ elif st.session_state["current_page"] == 4: # Last Page
158
+ st.markdown('<p class="big-font">Thank you for participating! <br> Click on the button below to submit your answers. </p>', unsafe_allow_html=True)
159
+ st.button('Submit Responses', disabled = st.session_state["disabled"], on_click = button_disable)
160
+ if st.session_state["disabled"]:
161
+ with st.spinner(r"$\textsf{\normalsize Storing data on IPFS and Ethereum. This operation might take a few minutes. Please wait to receive your confirmation code!}$"):
162
+ try:
163
+ response = {'file': json.dumps({
164
+ "Q1": Q1_radio_options[st.session_state["Q1"]],
165
+ "Q2": st.session_state["Q2"],
166
+ "Q3": st.session_state["Q3"],
167
+ "Q4": st.session_state["Q4"],
168
+ })}
169
+ except Exception as e:
170
+ print(e)
171
+ st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨")
172
+
173
+ if st.button('Back'):
174
+ st.session_state["current_page"] -= 1
175
+ st.rerun()
176
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")