dny1010 commited on
Commit
3a2fe1d
·
verified ·
1 Parent(s): b5e2321

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +137 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,139 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import altair as alt
4
+
5
+ # ===============================
6
+ # 1️⃣ 데이터 불러오기
7
+ # ===============================
8
+
9
+ past_path = '/app/src/2018-2024.xlsx'
10
+ future_path = '/app/src/예상수요량_2025예측.xlsx'
11
+
12
+ past_df = pd.read_excel(past_path)
13
+ future_df = pd.read_excel(future_path)
14
+
15
+ past_df['월'] = past_df['월'].astype(int)
16
+ future_df['월'] = future_df['월'].astype(int)
17
+
18
+ past_df = past_df.rename(columns={'판매량(kg)': 'y'})
19
+ future_df = future_df.rename(columns={'예상수요량': 'y'})
20
+
21
+ past_df['y'] = past_df['y'].astype(int)
22
+ future_df['y'] = future_df['y'].round().astype(int)
23
+
24
+ # ===============================
25
+ # 2️⃣ 페이지 설정 & 스타일
26
+ # ===============================
27
+
28
+ st.set_page_config(page_title="과일 수요량 대시보드", layout="wide")
29
+
30
+ st.markdown("""
31
+ <style>
32
+ /* 타이틀을 더 아래로 내려서 헤더와 겹치지 않게 */
33
+ .main > div:first-child {
34
+ padding-top: 50px !important;
35
+ }
36
+ .title {
37
+ font-size: 30px;
38
+ font-weight: 700;
39
+ margin-top: 25px;
40
+ margin-bottom: 20px;
41
+ }
42
+ </style>
43
+ """, unsafe_allow_html=True)
44
+
45
+ # 페이지 타이틀
46
+ st.markdown("<div class='title'>🍎 과일 수요량 대시보드 (과거 + 2025 예측)</div>", unsafe_allow_html=True)
47
+
48
+ # ===============================
49
+ # 3️⃣ 데이터 선택
50
+ # ===============================
51
+
52
+ data_type = st.sidebar.radio("📂 사용할 데이터", ["과거 데이터", "2025년 예측 데이터"])
53
+ selected_df = past_df if data_type == "과거 데이터" else future_df
54
+
55
+ # ===============================
56
+ # 4️⃣ 년도 선택 (2025년 예측은 숨김)
57
+ # ===============================
58
+
59
+ if data_type == "과거 데이터":
60
+ available_years = sorted(selected_df['년도'].unique())
61
+
62
+ selected_years = st.multiselect(
63
+ "📅 조회할 년도",
64
+ available_years,
65
+ default=[] # 기본 선택 없음
66
+ )
67
+
68
+ df_filtered = selected_df[selected_df['년도'].isin(selected_years)]
69
+ else:
70
+ df_filtered = selected_df.copy() # 2025년만 존재 → 자동 고정
71
+
72
+ # ===============================
73
+ # 5️⃣ 과일 선택 (기본 선택 없음)
74
+ # ===============================
75
+
76
+ fruits = sorted(df_filtered['과일종류'].unique())
77
+
78
+ selected_fruits = st.multiselect(
79
+ "🍊 표시할 과일 선택",
80
+ fruits,
81
+ default=[] # 기본 선택 없음
82
+ )
83
+
84
+ df_chart = df_filtered[df_filtered['과일종류'].isin(selected_fruits)]
85
+
86
+ # ===============================
87
+ # 6️⃣ 그래프 영역
88
+ # ===============================
89
+
90
+ st.subheader("📈 월별 수요량 그래프")
91
+
92
+ if len(df_chart) > 0:
93
+
94
+ if data_type == "과거 데이터":
95
+ years = sorted(df_chart['년도'].unique())
96
+ else:
97
+ years = [2025]
98
+
99
+ for y in years:
100
+ st.markdown(f"### 📌 {y}년")
101
+
102
+ df_year = df_chart[df_chart['년도'] == y]
103
+
104
+ chart = (
105
+ alt.Chart(df_year)
106
+ .mark_line(point=True)
107
+ .encode(
108
+ x=alt.X('월:O', title='월', axis=alt.Axis(labelAngle=0)),
109
+ y=alt.Y('y:Q', title='수요량'),
110
+ color='과일종류:N',
111
+ tooltip=['년도', '과일종류', '월', 'y']
112
+ )
113
+ .properties(height=350)
114
+ )
115
+
116
+ st.altair_chart(chart, use_container_width=True)
117
+
118
+ else:
119
+ st.info("그래프에 표시할 과일을 선택하세요.")
120
+
121
+ # ===============================
122
+ # 7️⃣ 테이블 영역
123
+ # ===============================
124
+
125
+ st.subheader("📊 상세 데이터")
126
+
127
+ selected_fruits_table = st.multiselect(
128
+ "📋 테이블 표시 과일",
129
+ fruits,
130
+ default=[]
131
+ )
132
+
133
+ df_table = df_filtered[df_filtered['과일종류'].isin(selected_fruits_table)]
134
 
135
+ if len(df_table) > 0:
136
+ df_show = df_table[['년도', '월', '과일종류', 'y']].rename(columns={'y': '수요량'})
137
+ st.dataframe(df_show, use_container_width=True)
138
+ else:
139
+ st.info("테이블에 표시할 과일을 선택하세요.")