Lashtw commited on
Commit
1ece60c
·
verified ·
1 Parent(s): 4b07504

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -35
app.py CHANGED
@@ -77,50 +77,55 @@ def main():
77
  df = load_data(uploaded_file)
78
 
79
  if df is not None:
80
- # 定義預期的欄位列表
81
- expected_columns = ['國文', '英文', '數學', '自科', '地理', '歷史', '公民', '總平均', '社會']
82
 
83
- # 選擇要比較的欄位
84
- numeric_columns = df.select_dtypes(include=[np.number]).columns.tolist()
 
 
 
85
 
86
- # 篩選出存在的欄位
87
- available_columns = [col for col in expected_columns if col in numeric_columns]
88
 
89
- # 如果沒有可用的預期欄位,則使用所有數值欄位
90
- if not available_columns:
91
- available_columns = numeric_columns
92
-
93
- # 使用核取方塊選擇欄位
94
  st.write("### 選擇要比較的欄位")
95
 
96
- # 建立一個網格來排列核取方塊
97
- cols = st.columns(3) # 每行3個核取方塊
98
-
99
  selected_columns = []
100
- for i, col in enumerate(numeric_columns):
101
- # 預設勾選在可用欄位中的項目
102
- checkbox_col = cols[i % 3]
103
- if checkbox_col.checkbox(col, default=col in available_columns):
104
- selected_columns.append(col)
105
 
106
  if selected_columns:
107
- # 準備雷達圖數據
108
- radar_data = prepare_radar_data(df, selected_columns)
109
-
110
- # 建立雷達圖
111
- fig = create_radar_chart(radar_data, selected_columns)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- # 顯示圖表
114
- st.plotly_chart(fig, use_container_width=True)
115
-
116
- # 下載圖表
117
- fig_bytes = fig.to_image(format="png", width=1200, height=800)
118
- st.download_button(
119
- label="下載雷達圖",
120
- data=fig_bytes,
121
- file_name="student_scores_radar_chart.png",
122
- mime="image/png"
123
- )
124
 
125
  if __name__ == "__main__":
126
  main()
 
77
  df = load_data(uploaded_file)
78
 
79
  if df is not None:
80
+ # 手動指定要分析的欄位,避免自動檢測問題
81
+ potential_columns = ['國文', '英文', '數學', '自科', '地理', '歷史', '公民', '總分', '平均', '社會']
82
 
83
+ # 找出DataFrame中存在的欄位
84
+ available_columns = []
85
+ for col in potential_columns:
86
+ matching_columns = [c for c in df.columns if col in c]
87
+ available_columns.extend(matching_columns)
88
 
89
+ # 移除重複欄位
90
+ available_columns = list(dict.fromkeys(available_columns))
91
 
 
 
 
 
 
92
  st.write("### 選擇要比較的欄位")
93
 
94
+ # 創建選擇介面
 
 
95
  selected_columns = []
96
+ for column in available_columns:
97
+ if st.checkbox(column, key=column):
98
+ selected_columns.append(column)
 
 
99
 
100
  if selected_columns:
101
+ try:
102
+ # 嘗試準備雷達圖數據
103
+ numeric_data = df[selected_columns]
104
+
105
+ # 確保選擇的是數值欄位
106
+ numeric_data = numeric_data.select_dtypes(include=[np.number])
107
+
108
+ # 建立雷達圖
109
+ fig = create_radar_chart(numeric_data, selected_columns)
110
+
111
+ # 顯示圖表
112
+ st.plotly_chart(fig, use_container_width=True)
113
+
114
+ # 下載圖表
115
+ fig_bytes = fig.to_image(format="png", width=1200, height=800)
116
+ st.download_button(
117
+ label="下載雷達圖",
118
+ data=fig_bytes,
119
+ file_name="student_scores_radar_chart.png",
120
+ mime="image/png"
121
+ )
122
 
123
+ except Exception as e:
124
+ st.error(f"生成雷達圖時發生錯誤:{e}")
125
+
126
+ # 如果沒有可選擇的欄位,顯示提示
127
+ if not available_columns:
128
+ st.warning("未找到可用於比較的欄位,請檢查上傳的CSV檔案")
 
 
 
 
 
129
 
130
  if __name__ == "__main__":
131
  main()