Rooobert commited on
Commit
da1dc34
·
verified ·
1 Parent(s): f614d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -72
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
 
4
  import numpy as np
5
  from datetime import datetime
6
  from dataclasses import dataclass, field
@@ -21,7 +22,7 @@ class SurveyAnalyzer:
21
  """📊 問卷分析類"""
22
 
23
  def __init__(self):
24
- # 更新滿意度欄位名稱
25
  self.satisfaction_columns = [
26
  '1.示範場域提供多元的數位課程與活動',
27
  '2.示範場域的數位課程與活動對我的生活應用有幫助',
@@ -43,13 +44,16 @@ class SurveyAnalyzer:
43
 
44
  def plot_satisfaction_scores(self, df: pd.DataFrame):
45
  """📊 示範場域滿意度平均分數圖表"""
 
 
 
46
  # 計算平均分數和標準差
47
- satisfaction_means = [df[col].mean() for col in self.satisfaction_columns]
48
- satisfaction_stds = [df[col].std() for col in self.satisfaction_columns]
49
 
50
  # 創建數據框
51
  satisfaction_df = pd.DataFrame({
52
- '滿意度項目': self.satisfaction_short_names,
53
  '平均分數': satisfaction_means,
54
  '標準差': satisfaction_stds
55
  })
@@ -124,27 +128,47 @@ class SurveyAnalyzer:
124
  opacity=0.85
125
  )
126
 
127
- # 添加受訪人數標註
128
- num_respondents = len(df)
129
- fig.add_annotation(
130
- x=0.5,
131
- xref='paper',
132
- yref='paper',
133
- text=f'受訪人數: {num_respondents}人',
134
- showarrow=False,
135
- font=dict(size=16),
136
- bgcolor='rgba(255,255,255,0.8)',
137
- bordercolor='rgba(0,0,0,0.2)',
138
- borderwidth=1,
139
- borderpad=4,
140
- y=-0.2
141
- )
142
-
143
- # 計算整體平均滿意度
144
- overall_satisfaction = df[self.satisfaction_columns].mean().mean()
145
 
146
  # 返回圖表和整體滿意度
147
- return fig, overall_satisfaction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  def main():
150
  st.set_page_config(page_title="示範場域滿意度調查", layout="wide")
@@ -155,58 +179,67 @@ def main():
155
  df = read_google_sheet(sheet_id, gid)
156
 
157
  if df is not None:
158
- # 檢查必要的欄位是否存在
159
- required_columns = [
160
- '1.示範場域提供多元的數位課程與活動',
161
- '2.示範場域的數位課程與活動對我的生活應用有幫助',
162
- '3.示範場域的服務人員親切有禮貌',
163
- '4.示範場域的服務空間與數位設備友善方便',
164
- '5.在示範場域可以獲得需要的協助',
165
- '6.對於示範場域的服務感到滿意'
166
- ]
167
 
168
- # 確認所有必要欄位都存在
169
- missing_columns = [col for col in required_columns if col not in df.columns]
 
 
 
170
  if missing_columns:
171
- st.error(f"缺少以下必要欄位: {missing_columns}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  else:
173
- # 創建分析器
174
- analyzer = SurveyAnalyzer()
175
-
176
- # 顯示標題
177
- st.title("📊 示範場域滿意度調查分析")
178
-
179
- # 繪製滿意度圖表
180
- satisfaction_fig, overall_satisfaction = analyzer.plot_satisfaction_scores(df)
181
-
182
- # 顯示圖表
183
- st.plotly_chart(satisfaction_fig, use_container_width=True)
184
-
185
- # 顯示整體滿意度
186
- st.markdown(f"""
187
- ### 📈 整體滿意度分析
188
- - **整體平均滿意度**: {overall_satisfaction:.2f} 分
189
-
190
- #### 🔍 滿意度解讀
191
- - 0-1分: 非常不滿意
192
- - 1-2分: 不滿意
193
- - 2-3分: 普通
194
- - 3-4分: 滿意
195
- - 4-5分: 非常滿意
196
-
197
- 根據調查結果,整體滿意度為 {overall_satisfaction:.2f} 分,
198
- """, unsafe_allow_html=True)
199
-
200
- # 根據整體滿意度提供文字解讀
201
- if overall_satisfaction < 2:
202
- st.warning("⚠️ 整體滿意度較低,建議深入檢討服務品質")
203
- elif overall_satisfaction < 3:
204
- st.info("ℹ️ 整體滿意度處於普通水平,可以進一步改善服務")
205
- elif overall_satisfaction < 4:
206
- st.success("✅ 整體滿意度良好,但仍有提升空間")
207
- else:
208
- st.balloons()
209
- st.success("🎉 整體滿意度非常高,表現優異!")
210
 
211
  if __name__ == "__main__":
212
  main()
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
+ import plotly.graph_objs as go
5
  import numpy as np
6
  from datetime import datetime
7
  from dataclasses import dataclass, field
 
22
  """📊 問卷分析類"""
23
 
24
  def __init__(self):
25
+ # 滿意度欄位名稱
26
  self.satisfaction_columns = [
27
  '1.示範場域提供多元的數位課程與活動',
28
  '2.示範場域的數位課程與活動對我的生活應用有幫助',
 
44
 
45
  def plot_satisfaction_scores(self, df: pd.DataFrame):
46
  """📊 示範場域滿意度平均分數圖表"""
47
+ # 確保所有滿意度欄位都存在
48
+ existing_columns = [col for col in self.satisfaction_columns if col in df.columns]
49
+
50
  # 計算平均分數和標準差
51
+ satisfaction_means = [df[col].mean() for col in existing_columns]
52
+ satisfaction_stds = [df[col].std() for col in existing_columns]
53
 
54
  # 創建數據框
55
  satisfaction_df = pd.DataFrame({
56
+ '滿意度項目': [self.satisfaction_short_names[self.satisfaction_columns.index(col)] for col in existing_columns],
57
  '平均分數': satisfaction_means,
58
  '標準差': satisfaction_stds
59
  })
 
128
  opacity=0.85
129
  )
130
 
131
+ # 計算整體平均滿意度(只計算存在的欄位)
132
+ overall_satisfaction = df[existing_columns].mean().mean()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
 
134
  # 返回圖表和整體滿意度
135
+ return fig, overall_satisfaction, len(df)
136
+
137
+ def analyze_demographic_data(self, df: pd.DataFrame):
138
+ """分析性別和教育程度"""
139
+ # 性別分佈
140
+ if '性別' in df.columns:
141
+ gender_counts = df['性別'].value_counts()
142
+ gender_pie = go.Figure(data=[go.Pie(
143
+ labels=gender_counts.index,
144
+ values=gender_counts.values,
145
+ hole=.3,
146
+ title='性別分佈'
147
+ )])
148
+ gender_pie.update_layout(title='📊 性別分佈')
149
+ else:
150
+ gender_pie = None
151
+ st.warning("資料中缺少性別欄位")
152
+
153
+ # 教育程度分佈
154
+ if '教育程度' in df.columns:
155
+ education_counts = df['教育程度'].value_counts()
156
+ education_bar = go.Figure(data=[go.Bar(
157
+ x=education_counts.index,
158
+ y=education_counts.values,
159
+ text=education_counts.values,
160
+ textposition='auto'
161
+ )])
162
+ education_bar.update_layout(
163
+ title='📊 教育程度分佈',
164
+ xaxis_title='教育程度',
165
+ yaxis_title='人數'
166
+ )
167
+ else:
168
+ education_bar = None
169
+ st.warning("資料中缺少教育程度欄位")
170
+
171
+ return gender_pie, education_bar
172
 
173
  def main():
174
  st.set_page_config(page_title="示範場域滿意度調查", layout="wide")
 
179
  df = read_google_sheet(sheet_id, gid)
180
 
181
  if df is not None:
182
+ # 創建分析器
183
+ analyzer = SurveyAnalyzer()
 
 
 
 
 
 
 
184
 
185
+ # 顯示標題
186
+ st.title("📊 示範場域滿意度調查分析")
187
+
188
+ # 提示缺少的滿意度欄位
189
+ missing_columns = [col for col in analyzer.satisfaction_columns if col not in df.columns]
190
  if missing_columns:
191
+ st.warning(f"⚠️ 缺少以下滿意度欄位: {missing_columns}")
192
+
193
+ # 繪製滿意度圖表
194
+ satisfaction_fig, overall_satisfaction, num_respondents = analyzer.plot_satisfaction_scores(df)
195
+
196
+ # 顯示滿意度圖表
197
+ st.plotly_chart(satisfaction_fig, use_container_width=True)
198
+
199
+ # 顯示整體滿意度
200
+ st.markdown(f"""
201
+ ### 📈 整體滿意度分析
202
+ - **受訪人數**: {num_respondents} 人
203
+ - **整體平均滿意度**: {overall_satisfaction:.2f} 分
204
+
205
+ #### 🔍 滿意度解讀
206
+ - 0-1分: 非常不滿意
207
+ - 1-2分: 不滿意
208
+ - 2-3分: 普通
209
+ - 3-4分: 滿意
210
+ - 4-5分: 非常滿意
211
+
212
+ 根據調查結果,整體滿意度為 {overall_satisfaction:.2f} 分,
213
+ """, unsafe_allow_html=True)
214
+
215
+ # 根據整體滿意度提供文字解讀
216
+ if overall_satisfaction < 2:
217
+ st.warning("⚠️ 整體滿意度較低,建議深入檢討服務品質")
218
+ elif overall_satisfaction < 3:
219
+ st.info("ℹ️ 整體滿意度處於普通水平,可以進一步改善服務")
220
+ elif overall_satisfaction < 4:
221
+ st.success("✅ 整體滿意度良好,但仍有提升空間")
222
  else:
223
+ st.balloons()
224
+ st.success("🎉 整體滿意度非常高,表現優異!")
225
+
226
+ # 人口統計分析
227
+ st.header("👥 人口統計分析")
228
+
229
+ # 創建兩列顯示
230
+ col1, col2 = st.columns(2)
231
+
232
+ # 性別分佈
233
+ with col1:
234
+ gender_pie, _ = analyzer.analyze_demographic_data(df)
235
+ if gender_pie:
236
+ st.plotly_chart(gender_pie, use_container_width=True)
237
+
238
+ # 教育程度分佈
239
+ with col2:
240
+ _, education_bar = analyzer.analyze_demographic_data(df)
241
+ if education_bar:
242
+ st.plotly_chart(education_bar, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
  if __name__ == "__main__":
245
  main()