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("아직 제출된 답변이 없습니다.")