MindLab / src /streamlit_app.py
dny1010's picture
Update src/streamlit_app.py
3a2fe1d verified
import streamlit as st
import pandas as pd
import altair as alt
# ===============================
# 1️⃣ 데이터 뢈러였기
# ===============================
past_path = '/app/src/2018-2024.xlsx'
future_path = '/app/src/μ˜ˆμƒμˆ˜μš”λŸ‰_2025예츑.xlsx'
past_df = pd.read_excel(past_path)
future_df = pd.read_excel(future_path)
past_df['μ›”'] = past_df['μ›”'].astype(int)
future_df['μ›”'] = future_df['μ›”'].astype(int)
past_df = past_df.rename(columns={'νŒλ§€λŸ‰(kg)': 'y'})
future_df = future_df.rename(columns={'μ˜ˆμƒμˆ˜μš”λŸ‰': 'y'})
past_df['y'] = past_df['y'].astype(int)
future_df['y'] = future_df['y'].round().astype(int)
# ===============================
# 2️⃣ νŽ˜μ΄μ§€ μ„€μ • & μŠ€νƒ€μΌ
# ===============================
st.set_page_config(page_title="과일 μˆ˜μš”λŸ‰ λŒ€μ‹œλ³΄λ“œ", layout="wide")
st.markdown("""
<style>
/* 타이틀을 더 μ•„λž˜λ‘œ λ‚΄λ €μ„œ 헀더와 κ²ΉμΉ˜μ§€ μ•Šκ²Œ */
.main > div:first-child {
padding-top: 50px !important;
}
.title {
font-size: 30px;
font-weight: 700;
margin-top: 25px;
margin-bottom: 20px;
}
</style>
""", unsafe_allow_html=True)
# νŽ˜μ΄μ§€ 타이틀
st.markdown("<div class='title'>🍎 과일 μˆ˜μš”λŸ‰ λŒ€μ‹œλ³΄λ“œ (κ³Όκ±° + 2025 예츑)</div>", unsafe_allow_html=True)
# ===============================
# 3️⃣ 데이터 선택
# ===============================
data_type = st.sidebar.radio("πŸ“‚ μ‚¬μš©ν•  데이터", ["κ³Όκ±° 데이터", "2025λ…„ 예츑 데이터"])
selected_df = past_df if data_type == "κ³Όκ±° 데이터" else future_df
# ===============================
# 4️⃣ 년도 선택 (2025λ…„ μ˜ˆμΈ‘μ€ μˆ¨κΉ€)
# ===============================
if data_type == "κ³Όκ±° 데이터":
available_years = sorted(selected_df['년도'].unique())
selected_years = st.multiselect(
"πŸ“… μ‘°νšŒν•  년도",
available_years,
default=[] # κΈ°λ³Έ 선택 μ—†μŒ
)
df_filtered = selected_df[selected_df['년도'].isin(selected_years)]
else:
df_filtered = selected_df.copy() # 2025λ…„λ§Œ 쑴재 β†’ μžλ™ κ³ μ •
# ===============================
# 5️⃣ 과일 선택 (κΈ°λ³Έ 선택 μ—†μŒ)
# ===============================
fruits = sorted(df_filtered['과일쒅λ₯˜'].unique())
selected_fruits = st.multiselect(
"🍊 ν‘œμ‹œν•  과일 선택",
fruits,
default=[] # κΈ°λ³Έ 선택 μ—†μŒ
)
df_chart = df_filtered[df_filtered['과일쒅λ₯˜'].isin(selected_fruits)]
# ===============================
# 6️⃣ κ·Έλž˜ν”„ μ˜μ—­
# ===============================
st.subheader("πŸ“ˆ 월별 μˆ˜μš”λŸ‰ κ·Έλž˜ν”„")
if len(df_chart) > 0:
if data_type == "κ³Όκ±° 데이터":
years = sorted(df_chart['년도'].unique())
else:
years = [2025]
for y in years:
st.markdown(f"### πŸ“Œ {y}λ…„")
df_year = df_chart[df_chart['년도'] == y]
chart = (
alt.Chart(df_year)
.mark_line(point=True)
.encode(
x=alt.X('μ›”:O', title='μ›”', axis=alt.Axis(labelAngle=0)),
y=alt.Y('y:Q', title='μˆ˜μš”λŸ‰'),
color='과일쒅λ₯˜:N',
tooltip=['년도', '과일쒅λ₯˜', 'μ›”', 'y']
)
.properties(height=350)
)
st.altair_chart(chart, use_container_width=True)
else:
st.info("κ·Έλž˜ν”„μ— ν‘œμ‹œν•  과일을 μ„ νƒν•˜μ„Έμš”.")
# ===============================
# 7️⃣ ν…Œμ΄λΈ” μ˜μ—­
# ===============================
st.subheader("πŸ“Š 상세 데이터")
selected_fruits_table = st.multiselect(
"πŸ“‹ ν…Œμ΄λΈ” ν‘œμ‹œ 과일",
fruits,
default=[]
)
df_table = df_filtered[df_filtered['과일쒅λ₯˜'].isin(selected_fruits_table)]
if len(df_table) > 0:
df_show = df_table[['년도', 'μ›”', '과일쒅λ₯˜', 'y']].rename(columns={'y': 'μˆ˜μš”λŸ‰'})
st.dataframe(df_show, use_container_width=True)
else:
st.info("ν…Œμ΄λΈ”μ— ν‘œμ‹œν•  과일을 μ„ νƒν•˜μ„Έμš”.")