inclass_poll / app.py
jonghhhh's picture
Update app.py
85eda91 verified
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("아직 제좜된 닡변이 μ—†μŠ΅λ‹ˆλ‹€.")