Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import json | |
| import web3 | |
| total_number_pages = 4 | |
| placeholder_buttons = None | |
| Q1_radio_options = ["radio option 1","radio option 2","radio option 3","Option 4?"] | |
| Q3_radio_options = ["Wowzers","This is super crazy","Not very crazy","midly crazy"] | |
| Q4_radio_options = ["Def not crazy","Crazy","Pizza","What am I doing?"] | |
| Q6_radio_options = ["N/A", "Strongly Disagree", "Disagree", "Neutral", "Agree", "Strongly Agree"] | |
| # Function that records radio element changes | |
| def radio_change(element, state, key): | |
| st.session_state[state] = element.index(st.session_state[key]) # Setting previously selected option | |
| def multi_change(element, state, key): | |
| st.session_state[state] = [] | |
| for selected_option in st.session_state[key]: | |
| st.session_state[state].append(selected_option) | |
| # Function that disables the last button while data is uploaded to IPFS | |
| def button_disable(): | |
| st.session_state['disabled'] = True | |
| def answer_change(state, key): | |
| st.session_state[state] = st.session_state[key] | |
| st.set_page_config(page_title='IPFS-Based Survey',) | |
| st.title('Test Survey') | |
| st.markdown("<style>.row-widget.stButton {text-align: center;}</style>", unsafe_allow_html=True) | |
| st.markdown("<style>.big-font {font-size:24px;}</style>", unsafe_allow_html=True) | |
| if "current_page" not in st.session_state: | |
| st.session_state["current_page"] = 1 | |
| st.session_state["Q1"] = None | |
| st.session_state["Q2"] = None | |
| st.session_state["Q3"] = None | |
| st.session_state["Q4"] = None | |
| st.session_state["Q5"] = None | |
| st.session_state["Q6"] = None | |
| st.session_state["disabled"] = False | |
| # Page 1; Video | |
| if st.session_state["current_page"] == 1: | |
| st.markdown("""<p class="big-font">This is a test survey</p>""", unsafe_allow_html=True) | |
| st.radio(label = "This is the radio question", | |
| options = Q1_radio_options, | |
| index = None if st.session_state["Q1"] == None else st.session_state["Q1"], | |
| key = 'Q1_radio', | |
| on_change = radio_change, | |
| args = (Q1_radio_options, "Q1", "Q1_radio",)) | |
| st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| st.video("https://www.youtube.com/watch?v=dQw4w9WgXcQ") | |
| st.markdown("""<p style='font-size:18px;'>Rick rolled</p>""", unsafe_allow_html=True) | |
| st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSU_fgVbH_LfgbKK6qvziW5SgG7vxdoNMI-atQ3T7fYpX14W3u9ib3G1aMFB-9Se-INjq9RRPjpADux5kP09LaWcA") | |
| st.markdown("""<p style='font-size:18px;'>Elephant</p>""", unsafe_allow_html=True) | |
| placeholder = st.empty() | |
| if st.button('Next', key='next_button_page_1'): | |
| all_answered = True | |
| if st.session_state["Q1"] == None or st.session_state["Q1"] == []: | |
| all_answered = False | |
| if all_answered: | |
| st.session_state["current_page"] += 1 | |
| st.rerun() | |
| else: | |
| with placeholder.container(): | |
| st.warning("Please answer all the questions on this page.", icon="⚠️") | |
| st.progress(st.session_state["current_page"]/total_number_pages, text="Progress") | |
| elif st.session_state["current_page"] == 2: | |
| st.text_area(label = "This is a text input question!", | |
| value= "" if st.session_state["Q2"] == None else st.session_state["Q2"], | |
| key = 'Q2_text', | |
| on_change = answer_change, | |
| args = ( "Q2", "Q2_text",)) | |
| st.markdown("""<style> div[class*="stText"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| st.multiselect(label = "Multiple Selection question!!!!????!?!?!", | |
| default = None if st.session_state["Q3"] == None else st.session_state["Q3"], | |
| options = Q3_radio_options, | |
| key = 'Q3_multi', | |
| on_change = multi_change, | |
| args = (Q3_radio_options, "Q3", "Q3_multi",)) | |
| st.markdown("""<style> div[class*="stMulti"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| st.selectbox(label = "Single selection question. ", | |
| options = Q4_radio_options, | |
| index = None if st.session_state["Q4"] == None else st.session_state["Q4"], | |
| key = 'Q4_radio', | |
| on_change = radio_change, | |
| args = (Q4_radio_options, "Q4", "Q4_radio",)) | |
| st.markdown("""<style> div[class*="stSelectbox"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| if st.session_state["Q5"] == None: | |
| st.session_state["Q5"] = 525 | |
| st.slider(label="Slider question to choose a number between 50 and 1000",min_value=50,max_value=1000, | |
| value= st.session_state["Q5"], | |
| key = "Q5_slider", | |
| on_change = answer_change, | |
| args = ("Q5", "Q5_slider",)) | |
| st.markdown("""<style> div[class*="stSlider"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| st.radio(label = "Likert. This was a good survey", # Likert | |
| options = Q6_radio_options, | |
| index = None if st.session_state["Q6"] == None else st.session_state["Q6"], | |
| key = 'Q6_radio', | |
| on_change = radio_change, | |
| args = (Q6_radio_options, "Q6", "Q6_radio",)) | |
| st.markdown("""<style> div[class*="stRadio"] > label > div[data-testid="stMarkdownContainer"] > p {font-size: 18px;}</style> <br><br>""", unsafe_allow_html=True) | |
| placeholder = st.empty() | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button('Back'): | |
| st.session_state["current_page"] -= 1 | |
| st.rerun() | |
| with col2: | |
| if st.button('Next'): | |
| all_answered = True | |
| if st.session_state["Q2"] == None or st.session_state["Q2"] == []: | |
| all_answered = False | |
| if st.session_state["Q3"] == None or st.session_state["Q3"] == []: | |
| all_answered = False | |
| if st.session_state["Q4"] == None or st.session_state["Q4"] == []: | |
| all_answered = False | |
| if st.session_state["Q5"] == None or st.session_state["Q5"] == []: | |
| all_answered = False | |
| if st.session_state["Q6"] == None or st.session_state["Q6"] == []: | |
| all_answered = False | |
| if all_answered: | |
| st.session_state["current_page"] += 1 | |
| st.rerun() | |
| else: | |
| with placeholder.container(): | |
| st.warning("Please answer all the questions on this page.", icon="⚠️") | |
| st.progress(st.session_state["current_page"]/total_number_pages, text="Progress") | |
| elif st.session_state["current_page"] == 3: | |
| st.markdown("""<p style='font-size:18px;'>Yay a nice blank page</p>""", unsafe_allow_html=True) | |
| placeholder = st.empty() | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| if st.button('Back'): | |
| st.session_state["current_page"] -= 1 | |
| st.rerun() | |
| with col2: | |
| if st.button('Next'): | |
| all_answered = True | |
| if all_answered: | |
| st.session_state["current_page"] += 1 | |
| st.rerun() | |
| else: | |
| with placeholder.container(): | |
| st.warning("Please answer all the questions on this page.", icon="⚠️") | |
| st.progress(st.session_state["current_page"]/total_number_pages, text="Progress") | |
| elif st.session_state["current_page"] == 4: # Last Page | |
| 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) | |
| st.button('Submit Responses', disabled = st.session_state["disabled"], on_click = button_disable) | |
| if st.session_state["disabled"]: | |
| 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!}$"): | |
| try: | |
| response = {'file': json.dumps({ | |
| "Q1": Q1_radio_options[st.session_state["Q1"]], | |
| "Q2": st.session_state["Q2"], | |
| "Q3": st.session_state["Q3"], | |
| "Q4": st.session_state["Q4"], | |
| "Q5": st.session_state["Q5"], | |
| "Q6": st.session_state["Q6"], | |
| })} | |
| except Exception as e: | |
| print(e) | |
| st.error(f'An error ocurred. Here is the error message: {e}', icon="🚨") | |
| if st.button('Back'): | |
| st.session_state["current_page"] -= 1 | |
| st.rerun() | |
| st.progress(st.session_state["current_page"]/total_number_pages, text="Progress") | |