Spaces:
Build error
Build error
Update app.py
#1
by Rozeeeee - opened
app.py
CHANGED
|
@@ -15,11 +15,25 @@ def download_and_load_csv(url):
|
|
| 15 |
df = df.fillna(0) # 避免 NaN 錯誤
|
| 16 |
return df
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
selected_columns = list(selected_columns) # 確保是列表
|
| 21 |
|
| 22 |
-
# 🛠️ **檢查錯誤**
|
| 23 |
if not selected_columns:
|
| 24 |
st.error("❌ 沒有選擇數據欄位,請至少選擇一個!")
|
| 25 |
return
|
|
@@ -35,10 +49,27 @@ def generate_plots(df, df_name, selected_columns):
|
|
| 35 |
st.error("❌ 選擇的欄位長度與 `公司名稱` 不匹配,請檢查數據!")
|
| 36 |
return
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# **下載數據**
|
| 44 |
urls = [
|
|
@@ -56,12 +87,15 @@ st.title("📊 台灣企業 ESG 數據分析")
|
|
| 56 |
st.subheader("📂 數據預覽")
|
| 57 |
st.dataframe(combined_df)
|
| 58 |
|
| 59 |
-
# **選擇
|
| 60 |
emission_columns = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
|
| 61 |
-
selected_columns = st.multiselect("選擇排放類別", emission_columns, default=emission_columns[:1])
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# **生成圖表**
|
| 64 |
if selected_columns:
|
| 65 |
-
generate_plots(combined_df, "綜合數據", selected_columns)
|
| 66 |
else:
|
| 67 |
st.write("⚠️ 請選擇至少一個排放類別來顯示圖表。")
|
|
|
|
| 15 |
df = df.fillna(0) # 避免 NaN 錯誤
|
| 16 |
return df
|
| 17 |
|
| 18 |
+
# 美化圖表
|
| 19 |
+
def beautify_chart(fig):
|
| 20 |
+
fig.update_layout(
|
| 21 |
+
font_family="Arial",
|
| 22 |
+
font_color="#444",
|
| 23 |
+
title_font_family="Arial",
|
| 24 |
+
title_font_color="#000",
|
| 25 |
+
legend_title_font_color="#000",
|
| 26 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 27 |
+
paper_bgcolor='rgba(0,0,0,0)',
|
| 28 |
+
)
|
| 29 |
+
fig.update_xaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
| 30 |
+
fig.update_yaxes(showline=True, linewidth=2, linecolor='lightgray', gridcolor='lightgray')
|
| 31 |
+
return fig
|
| 32 |
+
|
| 33 |
+
# **生成 Plotly 圖表**
|
| 34 |
+
def generate_plots(df, df_name, selected_columns, chart_type):
|
| 35 |
selected_columns = list(selected_columns) # 確保是列表
|
| 36 |
|
|
|
|
| 37 |
if not selected_columns:
|
| 38 |
st.error("❌ 沒有選擇數據欄位,請至少選擇一個!")
|
| 39 |
return
|
|
|
|
| 49 |
st.error("❌ 選擇的欄位長度與 `公司名稱` 不匹配,請檢查數據!")
|
| 50 |
return
|
| 51 |
|
| 52 |
+
with st.expander(f"📊 顯示 {df_name} 圖表"):
|
| 53 |
+
st.subheader(f"{df_name} - {chart_type} 圖")
|
| 54 |
+
|
| 55 |
+
# **根據選擇的圖表類型來繪製**
|
| 56 |
+
if chart_type == "折線圖":
|
| 57 |
+
fig = px.line(df, x="公司名稱", y=valid_columns, title=f"{df_name} - 折線圖", color_discrete_sequence=theme)
|
| 58 |
+
|
| 59 |
+
elif chart_type == "散點圖":
|
| 60 |
+
fig = px.scatter(df, x="公司名稱", y=valid_columns, title=f"{df_name} - 散點圖", color_discrete_sequence=theme)
|
| 61 |
+
|
| 62 |
+
elif chart_type == "長條圖":
|
| 63 |
+
fig = px.bar(df, x="公司名稱", y=valid_columns, title=f"{df_name} - 長條圖", color_discrete_sequence=theme)
|
| 64 |
+
|
| 65 |
+
elif chart_type == "餅圖":
|
| 66 |
+
total_emissions = df.groupby("公司名稱")[valid_columns].sum().reset_index()
|
| 67 |
+
total_emissions = total_emissions.melt(id_vars=["公司名稱"], value_vars=valid_columns, var_name="排放類型", value_name="總排放量")
|
| 68 |
+
fig = px.pie(total_emissions, values='總排放量', names='公司名稱', title=f"{df_name} - 餅圖", color_discrete_sequence=theme, hole=0.3)
|
| 69 |
+
fig.update_traces(textposition='inside', textinfo='percent+label')
|
| 70 |
+
|
| 71 |
+
fig = beautify_chart(fig)
|
| 72 |
+
st.plotly_chart(fig, use_container_width=True) # ✅ **讓圖表自適應畫面**
|
| 73 |
|
| 74 |
# **下載數據**
|
| 75 |
urls = [
|
|
|
|
| 87 |
st.subheader("📂 數據預覽")
|
| 88 |
st.dataframe(combined_df)
|
| 89 |
|
| 90 |
+
# **選擇數據欄位**
|
| 91 |
emission_columns = ["範疇一排放量(噸CO2e)", "範疇二排放量(噸CO2e)", "範疇三排放量(噸CO2e)"]
|
| 92 |
+
selected_columns = st.multiselect("選擇要顯示的排放類別", emission_columns, default=emission_columns[:1])
|
| 93 |
+
|
| 94 |
+
# **選擇圖表類型**
|
| 95 |
+
chart_type = st.selectbox("選擇圖表類型", ["折線圖", "散點圖", "長條圖", "餅圖"])
|
| 96 |
|
| 97 |
# **生成圖表**
|
| 98 |
if selected_columns:
|
| 99 |
+
generate_plots(combined_df, "綜合數據", selected_columns, chart_type)
|
| 100 |
else:
|
| 101 |
st.write("⚠️ 請選擇至少一個排放類別來顯示圖表。")
|