anastaa3 commited on
Commit
2c1db9d
·
verified ·
1 Parent(s): 52ff79f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +194 -52
app.py CHANGED
@@ -3,11 +3,14 @@ import requests
3
  import json
4
  import web3
5
 
6
- total_number_pages = 4
7
  placeholder_buttons = None
8
 
9
- Q2_radio_options = ["A","B"]
10
- Q4_radio_options = [""]
 
 
 
11
 
12
 
13
  # Function that records radio element changes
@@ -27,7 +30,7 @@ def answer_change(state, key):
27
  st.session_state[state] = st.session_state[key]
28
 
29
  st.set_page_config(page_title='IPFS-Based Survey',)
30
- st.title('Test 1')
31
 
32
  st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True)
33
  st.markdown("<style>.big-font {font-size:24px;}</style>", unsafe_allow_html=True)
@@ -39,14 +42,20 @@ if "current_page" not in st.session_state:
39
  st.session_state["Q2"] = None
40
  st.session_state["Q3"] = None
41
  st.session_state["Q4"] = None
 
 
 
 
 
42
  st.session_state["disabled"] = False
43
 
44
  # Page 1; Video
45
  if st.session_state["current_page"] == 1:
46
 
47
- st.markdown("""<p class="big-font">I am describing this survey.</p>""", unsafe_allow_html=True)
48
 
49
- st.text_area(label = "Do you like pie?",
 
50
  value= "" if st.session_state["Q1"] == None else st.session_state["Q1"],
51
  key = 'Q1_text',
52
  on_change = answer_change,
@@ -55,24 +64,12 @@ if st.session_state["current_page"] == 1:
55
  st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
56
 
57
 
58
- st.radio(label = "MC Question",
59
- options = Q2_radio_options,
60
- index = None if st.session_state["Q2"] == None else st.session_state["Q2"],
61
- key = 'Q2_radio',
62
- on_change = radio_change,
63
- args = (Q2_radio_options, "Q2", "Q2_radio",))
64
-
65
- st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
66
-
67
-
68
  placeholder = st.empty()
69
 
70
  if st.button('Next', key='next_button_page_1'):
71
  all_answered = True
72
  if st.session_state["Q1"] == None or st.session_state["Q1"] == []:
73
  all_answered = False
74
- if st.session_state["Q2"] == None or st.session_state["Q2"] == []:
75
- all_answered = False
76
  if all_answered:
77
  st.session_state["current_page"] += 1
78
  st.rerun()
@@ -85,20 +82,26 @@ if st.session_state["current_page"] == 1:
85
 
86
  elif st.session_state["current_page"] == 2:
87
 
88
- st.video("https://www.youtube.com/watch?v=qJxi122kxgQ")
89
- st.markdown("""<p style='font-size:18px;'>Go Luka!</p>""", unsafe_allow_html=True)
90
- st.image("https://th.bing.com/th/id/OIP.M7inwN-YD5ycRt1VWQ9dCQHaEK?rs=1&pid=ImgDetMain")
91
- st.markdown("""<p style='font-size:18px;'>Youtube</p>""", unsafe_allow_html=True)
92
- if st.session_state["Q3"] == None:
93
- st.session_state["Q3"] = 5
94
- st.slider(label="You messing with it?",min_value=0,max_value=10,
95
- value= st.session_state["Q3"],
96
- key = "Q3_slider",
97
  on_change = answer_change,
98
- args = ("Q3", "Q3_slider",))
99
  st.markdown("""<style> div[class*="stSlider"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
100
 
101
 
 
 
 
 
 
 
 
 
 
 
102
  placeholder = st.empty()
103
 
104
  col1, col2 = st.columns(2)
@@ -109,6 +112,8 @@ elif st.session_state["current_page"] == 2:
109
  with col2:
110
  if st.button('Next'):
111
  all_answered = True
 
 
112
  if st.session_state["Q3"] == None or st.session_state["Q3"] == []:
113
  all_answered = False
114
  if all_answered:
@@ -123,9 +128,36 @@ elif st.session_state["current_page"] == 2:
123
 
124
  elif st.session_state["current_page"] == 3:
125
 
126
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
- st.markdown("""<p style='font-size:18px;'>Page!</p>""", unsafe_allow_html=True)
129
  placeholder = st.empty()
130
 
131
  col1, col2 = st.columns(2)
@@ -136,6 +168,12 @@ elif st.session_state["current_page"] == 3:
136
  with col2:
137
  if st.button('Next'):
138
  all_answered = True
 
 
 
 
 
 
139
  if all_answered:
140
  st.session_state["current_page"] += 1
141
  st.rerun()
@@ -148,30 +186,24 @@ elif st.session_state["current_page"] == 3:
148
 
149
  elif st.session_state["current_page"] == 4:
150
 
151
- st.selectbox(label = "1234567890asdfghjkl No answer select question",
152
- options = Q4_radio_options,
153
- index = None if st.session_state["Q4"] == None else st.session_state["Q4"],
154
- key = 'Q4_radio',
155
- on_change = radio_change,
156
- args = (Q4_radio_options, "Q4", "Q4_radio",))
 
 
157
 
158
- st.markdown("""<style> div[class*="stSelectbox"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
159
 
 
 
 
 
 
 
 
160
 
161
- if st.button("Submit"):
162
- survey_data = {
163
- "color": color,
164
- "sleep": sleep,
165
- "gender": gender,
166
- "feedback": feedback
167
- }
168
-
169
- # upload the data to ipfs
170
- ipfs_hash = upload_json_to_ipfs(survey_data)
171
- if ipfs_hash:
172
- st.success("Thank you for your submission!")
173
- st.write(f"Data uploaded to IPFS with CID: {ipfs_hash}")
174
- st.write(f"Your data can be viewed at https://gateway.pinata.cloud/ipfs/{ipfs_hash}")
175
 
176
  placeholder = st.empty()
177
 
@@ -183,7 +215,65 @@ elif st.session_state["current_page"] == 4:
183
  with col2:
184
  if st.button('Next'):
185
  all_answered = True
186
- if st.session_state["Q4"] == None or st.session_state["Q4"] == []:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  all_answered = False
188
  if all_answered:
189
  st.session_state["current_page"] += 1
@@ -195,3 +285,55 @@ elif st.session_state["current_page"] == 4:
195
  st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
196
 
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import json
4
  import web3
5
 
6
+ total_number_pages = 7
7
  placeholder_buttons = None
8
 
9
+ Q3_radio_options = ["Option 1 multi","Option 2 multi"]
10
+ Q4_radio_options = ["Option 1 Radio","Option 2 Radio"]
11
+ Q5_radio_options = ["Green","Red","Blue","Yellow"]
12
+ Q6_radio_options = ["Yes","No","Ur dumb"]
13
+ Q9_radio_options = ["IDK","Of Course","STFU!!!!!","Inner demons"]
14
 
15
 
16
  # Function that records radio element changes
 
30
  st.session_state[state] = st.session_state[key]
31
 
32
  st.set_page_config(page_title='IPFS-Based Survey',)
33
+ st.title('Newest Test')
34
 
35
  st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True)
36
  st.markdown("<style>.big-font {font-size:24px;}</style>", unsafe_allow_html=True)
 
42
  st.session_state["Q2"] = None
43
  st.session_state["Q3"] = None
44
  st.session_state["Q4"] = None
45
+ st.session_state["Q5"] = None
46
+ st.session_state["Q6"] = None
47
+ st.session_state["Q7"] = None
48
+ st.session_state["Q8"] = None
49
+ st.session_state["Q9"] = None
50
  st.session_state["disabled"] = False
51
 
52
  # Page 1; Video
53
  if st.session_state["current_page"] == 1:
54
 
55
+ st.markdown("""<p class="big-font">This is a Test of the latest version of the survey builder Tool</p>""", unsafe_allow_html=True)
56
 
57
+ st.video("https://www.youtube.com/watch?v=aJb6Dov0jlM")
58
+ st.text_area(label = "This is a text Question and should allow any text input. This should appear on the first page.",
59
  value= "" if st.session_state["Q1"] == None else st.session_state["Q1"],
60
  key = 'Q1_text',
61
  on_change = answer_change,
 
64
  st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
65
 
66
 
 
 
 
 
 
 
 
 
 
 
67
  placeholder = st.empty()
68
 
69
  if st.button('Next', key='next_button_page_1'):
70
  all_answered = True
71
  if st.session_state["Q1"] == None or st.session_state["Q1"] == []:
72
  all_answered = False
 
 
73
  if all_answered:
74
  st.session_state["current_page"] += 1
75
  st.rerun()
 
82
 
83
  elif st.session_state["current_page"] == 2:
84
 
85
+ if st.session_state["Q2"] == None:
86
+ st.session_state["Q2"] = 5
87
+ st.slider(label="This is a slider question that allows a numeric input between two numbers. This should appear on the second page.",min_value=0,max_value=10,
88
+ value= st.session_state["Q2"],
89
+ key = "Q2_slider",
 
 
 
 
90
  on_change = answer_change,
91
+ args = ("Q2", "Q2_slider",))
92
  st.markdown("""<style> div[class*="stSlider"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
93
 
94
 
95
+ st.multiselect(label = "This is a multiselect question where you can select multiple answers. This should appear on the second page.",
96
+ default = None if st.session_state["Q3"] == None else st.session_state["Q3"],
97
+ options = Q3_radio_options,
98
+ key = 'Q3_multi',
99
+ on_change = multi_change,
100
+ args = (Q3_radio_options, "Q3", "Q3_multi",))
101
+
102
+ st.markdown("""<style> div[class*="stMulti"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
103
+
104
+
105
  placeholder = st.empty()
106
 
107
  col1, col2 = st.columns(2)
 
112
  with col2:
113
  if st.button('Next'):
114
  all_answered = True
115
+ if st.session_state["Q2"] == None or st.session_state["Q2"] == []:
116
+ all_answered = False
117
  if st.session_state["Q3"] == None or st.session_state["Q3"] == []:
118
  all_answered = False
119
  if all_answered:
 
128
 
129
  elif st.session_state["current_page"] == 3:
130
 
131
+ st.radio(label = "This is a multiple choice question where you can choose one of the following. This should appear on the third page.",
132
+ options = Q4_radio_options,
133
+ index = None if st.session_state["Q4"] == None else st.session_state["Q4"],
134
+ key = 'Q4_radio',
135
+ on_change = radio_change,
136
+ args = (Q4_radio_options, "Q4", "Q4_radio",))
137
+
138
+ st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
139
+
140
+
141
+ st.radio(label = "Fav Color?",
142
+ options = Q5_radio_options,
143
+ index = None if st.session_state["Q5"] == None else st.session_state["Q5"],
144
+ key = 'Q5_radio',
145
+ on_change = radio_change,
146
+ args = (Q5_radio_options, "Q5", "Q5_radio",))
147
+
148
+ st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
149
+
150
+
151
+ st.multiselect(label = "Party Time",
152
+ default = None if st.session_state["Q6"] == None else st.session_state["Q6"],
153
+ options = Q6_radio_options,
154
+ key = 'Q6_multi',
155
+ on_change = multi_change,
156
+ args = (Q6_radio_options, "Q6", "Q6_multi",))
157
+
158
+ st.markdown("""<style> div[class*="stMulti"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
159
+
160
 
 
161
  placeholder = st.empty()
162
 
163
  col1, col2 = st.columns(2)
 
168
  with col2:
169
  if st.button('Next'):
170
  all_answered = True
171
+ if st.session_state["Q4"] == None or st.session_state["Q4"] == []:
172
+ all_answered = False
173
+ if st.session_state["Q5"] == None or st.session_state["Q5"] == []:
174
+ all_answered = False
175
+ if st.session_state["Q6"] == None or st.session_state["Q6"] == []:
176
+ all_answered = False
177
  if all_answered:
178
  st.session_state["current_page"] += 1
179
  st.rerun()
 
186
 
187
  elif st.session_state["current_page"] == 4:
188
 
189
+ if st.session_state["Q7"] == None:
190
+ st.session_state["Q7"] = 50
191
+ st.slider(label="Happiness Scale",min_value=0,max_value=100,
192
+ value= st.session_state["Q7"],
193
+ key = "Q7_slider",
194
+ on_change = answer_change,
195
+ args = ("Q7", "Q7_slider",))
196
+ st.markdown("""<style> div[class*="stSlider"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
197
 
 
198
 
199
+ st.text_area(label = "Why are you this happy/unhappy?",
200
+ value= "" if st.session_state["Q8"] == None else st.session_state["Q8"],
201
+ key = 'Q8_text',
202
+ on_change = answer_change,
203
+ args = ( "Q8", "Q8_text",))
204
+
205
+ st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
  placeholder = st.empty()
209
 
 
215
  with col2:
216
  if st.button('Next'):
217
  all_answered = True
218
+ if st.session_state["Q7"] == None or st.session_state["Q7"] == []:
219
+ all_answered = False
220
+ if st.session_state["Q8"] == None or st.session_state["Q8"] == []:
221
+ all_answered = False
222
+ if all_answered:
223
+ st.session_state["current_page"] += 1
224
+ st.rerun()
225
+ else:
226
+ with placeholder.container():
227
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
228
+
229
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
230
+
231
+
232
+ elif st.session_state["current_page"] == 5:
233
+
234
+ placeholder = st.empty()
235
+
236
+ col1, col2 = st.columns(2)
237
+ with col1:
238
+ if st.button('Back'):
239
+ st.session_state["current_page"] -= 1
240
+ st.rerun()
241
+ with col2:
242
+ if st.button('Next'):
243
+ all_answered = True
244
+ if all_answered:
245
+ st.session_state["current_page"] += 1
246
+ st.rerun()
247
+ else:
248
+ with placeholder.container():
249
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
250
+
251
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
252
+
253
+
254
+ elif st.session_state["current_page"] == 6:
255
+
256
+ st.radio(label = "I like cheese?",
257
+ options = Q9_radio_options,
258
+ index = None if st.session_state["Q9"] == None else st.session_state["Q9"],
259
+ key = 'Q9_radio',
260
+ on_change = radio_change,
261
+ args = (Q9_radio_options, "Q9", "Q9_radio",))
262
+
263
+ st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True)
264
+
265
+
266
+ placeholder = st.empty()
267
+
268
+ col1, col2 = st.columns(2)
269
+ with col1:
270
+ if st.button('Back'):
271
+ st.session_state["current_page"] -= 1
272
+ st.rerun()
273
+ with col2:
274
+ if st.button('Next'):
275
+ all_answered = True
276
+ if st.session_state["Q9"] == None or st.session_state["Q9"] == []:
277
  all_answered = False
278
  if all_answered:
279
  st.session_state["current_page"] += 1
 
285
  st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
286
 
287
 
288
+ elif st.session_state["current_page"] == 7:
289
+
290
+ placeholder = st.empty()
291
+
292
+ col1, col2 = st.columns(2)
293
+ with col1:
294
+ if st.button('Back'):
295
+ st.session_state["current_page"] -= 1
296
+ st.rerun()
297
+ with col2:
298
+ if st.button('Next'):
299
+ all_answered = True
300
+ if all_answered:
301
+ st.session_state["current_page"] += 1
302
+ st.rerun()
303
+ else:
304
+ with placeholder.container():
305
+ st.warning("Please answer all the questions on this page.", icon="⚠️")
306
+
307
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")
308
+
309
+
310
+ 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)
311
+ st.button('Submit Responses', disabled = st.session_state["disabled"], on_click = button_disable)
312
+ if st.session_state["disabled"]:
313
+ 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!}$"):
314
+ try:
315
+ response = {'file': json.dumps({
316
+ "Q1": st.session_state["Q1"],
317
+ "Q2": st.session_state["Q2"],
318
+ "Q3": Q3_radio_options[st.session_state["Q3"]],
319
+ "Q4": Q4_radio_options[st.session_state["Q4"]],
320
+ "Q5": Q5_radio_options[st.session_state["Q5"]],
321
+ "Q6": Q6_radio_options[st.session_state["Q6"]],
322
+ "Q7": st.session_state["Q7"],
323
+ "Q8": st.session_state["Q8"],
324
+ "Q9": Q9_radio_options[st.session_state["Q9"]],
325
+ })}
326
+ except Exception as e:
327
+ print(e)
328
+ st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨")
329
+
330
+ print(st.session_state["Q1"])
331
+ print(st.session_state["Q2"])
332
+ print(Q3_radio_options[st.session_state["Q3"]])
333
+ print(Q4_radio_options[st.session_state["Q4"]])
334
+ print(Q5_radio_options[st.session_state["Q5"]])
335
+ print(Q6_radio_options[st.session_state["Q6"]])
336
+ print(st.session_state["Q7"])
337
+ print(st.session_state["Q8"])
338
+ print(Q9_radio_options[st.session_state["Q9"]])
339
+ st.progress(st.session_state["current_page"]/total_number_pages, text="Progress")