Spaces:
Running
Running
| import streamlit as st | |
| import pandas as pd | |
| import os | |
| # μ¬μ΄λλ°μμ νμ΄μ§ μ ν | |
| user_type = st.sidebar.selectbox("μ¬μ©μ μ νμ μ ννμΈμ:", ["νμ μλ΅", "μ€λ¬Έ μ€μ ", "κ²°κ³Ό νμΈ"]) | |
| # μ€λ¬Έ λ°μ΄ν° νμΌ κ²½λ‘ | |
| survey_file_path = "survey_data.csv" | |
| responses_file_path = "responses_data.csv" | |
| # μ€λ¬Έ μ€μ νμ΄μ§ (κ°μ¬μ©) | |
| if user_type == "μ€λ¬Έ μ€μ ": | |
| st.title("μ€λ¬Έ μ€μ ") | |
| question = st.text_input("μ€λ¬Έ μ§λ¬Έμ μ λ ₯νμΈμ:") | |
| use_options = st.checkbox("κ°κ΄μ μ΅μ μ¬μ©") | |
| options = [] | |
| if use_options: | |
| options = st.text_area("μ νν λ΅λ³ νλͺ©μ μ λ ₯νμΈμ (κ° νλͺ©μ μ μ€μ μ λ ₯):").split('\n') | |
| options = [option.strip() for option in options if option.strip()] # λΉ μ΅μ μ κ±° | |
| if st.button("μ€λ¬Έ μμ"): | |
| if question: | |
| data = { | |
| 'question': [question], | |
| 'options': [options], | |
| 'use_options': [use_options] | |
| } | |
| df = pd.DataFrame(data) | |
| df.to_csv(survey_file_path, index=False) | |
| st.success("μ€λ¬Έμ΄ μ€μ λμμ΅λλ€.") | |
| else: | |
| st.error("μ§λ¬Έμ μ λ ₯νμΈμ.") | |
| if st.button("μ€μ μ΄κΈ°ν"): | |
| if os.path.exists(survey_file_path): | |
| os.remove(survey_file_path) | |
| if os.path.exists(responses_file_path): | |
| os.remove(responses_file_path) | |
| st.success("λͺ¨λ μ€μ κ³Ό μλ΅μ΄ μ΄κΈ°νλμμ΅λλ€.") | |
| # νμ μλ΅ νμ΄μ§ | |
| elif user_type == "νμ μλ΅": | |
| st.title("μ€λ¬Έ μλ΅") | |
| if os.path.exists(survey_file_path): | |
| df = pd.read_csv(survey_file_path) | |
| question = df['question'][0] | |
| use_options = df['use_options'][0] | |
| options = eval(df['options'][0]) if use_options else [] | |
| st.write(f"### {question}") | |
| if use_options and options: | |
| options.append("κΈ°ν (μ§μ μ λ ₯)") | |
| selected_option = st.radio("λ΅λ³μ μ ννμΈμ:", options) | |
| if selected_option == "κΈ°ν (μ§μ μ λ ₯)": | |
| answer = st.text_area("μ£Όκ΄μ λ΅λ³μ μ λ ₯νμΈμ:") | |
| else: | |
| answer = selected_option | |
| else: | |
| answer = st.text_area("μ£Όκ΄μ λ΅λ³μ μ λ ₯νμΈμ:") | |
| if st.button("λ΅λ³ μ μΆ"): | |
| # μλ΅ λ°μ΄ν° μ μ₯ | |
| response_data = { | |
| 'μλ΅': [answer], | |
| 'μ ν': ['κ°κ΄μ' if use_options and answer in options else 'μ£Όκ΄μ'] | |
| } | |
| response_df = pd.DataFrame(response_data) | |
| if os.path.exists(responses_file_path): | |
| response_df.to_csv(responses_file_path, mode='a', header=False, index=False) | |
| else: | |
| response_df.to_csv(responses_file_path, index=False) | |
| st.success("λ΅λ³μ΄ μ μΆλμμ΅λλ€!") | |
| st.write("### λ΅λ³ μ μΆ μλ£") | |
| st.write(f"κ·νμ λ΅λ³: {answer}") | |
| else: | |
| st.warning("μ€λ¬Έμ΄ μμ§ μ€μ λμ§ μμμ΅λλ€. κ°μ¬μκ² λ¬ΈμνμΈμ.") | |
| # κ²°κ³Ό νμΈ νμ΄μ§ (κ°μ¬μ©) | |
| elif user_type == "κ²°κ³Ό νμΈ": | |
| st.title("μ€λ¬Έ κ²°κ³Ό νμΈ") | |
| if os.path.exists(responses_file_path): | |
| df = pd.read_csv(responses_file_path) | |
| st.write("### μλ΅ κ²°κ³Ό") | |
| if os.path.exists(survey_file_path): | |
| survey_df = pd.read_csv(survey_file_path) | |
| use_options = survey_df['use_options'][0] | |
| if use_options: | |
| options = eval(survey_df['options'][0]) | |
| # κ°κ΄μ μλ΅λ§ νν°λ§νμ¬ λ§λ κ·Έλν μμ± | |
| objective_responses = df[df['μ ν'] == 'κ°κ΄μ'] | |
| response_counts = objective_responses['μλ΅'].value_counts().reindex(options, fill_value=0) | |
| st.bar_chart(response_counts) | |
| # μ£Όκ΄μ μλ΅ νμ | |
| subjective_responses = df[df['μ ν'] == 'μ£Όκ΄μ'] | |
| if not subjective_responses.empty: | |
| st.write("### μ£Όκ΄μ μλ΅") | |
| st.table(subjective_responses['μλ΅']) | |
| else: | |
| st.info("μμ§ μ£Όκ΄μ μλ΅μ΄ μμ΅λλ€.") | |
| # λͺ¨λ μλ΅ νμ | |
| st.write("### λͺ¨λ μλ΅") | |
| st.table(df) | |
| else: | |
| st.warning("μμ§ μ μΆλ λ΅λ³μ΄ μμ΅λλ€.") | |