Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,40 +20,33 @@ urls = {
|
|
| 20 |
|
| 21 |
# 下載並加載 CSV 文件到 DataFrame 的函數
|
| 22 |
@st.cache_data
|
| 23 |
-
def load_data():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
df = df.fillna(0)
|
| 30 |
-
dataframes[name] = df
|
| 31 |
-
return dataframes
|
| 32 |
|
| 33 |
# Streamlit 應用
|
| 34 |
st.title("ESG 專題數據分析")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# 顯示進度條
|
| 39 |
progress_bar = st.progress(0)
|
| 40 |
for i in range(100):
|
| 41 |
time.sleep(0.01)
|
| 42 |
progress_bar.progress(i + 1)
|
| 43 |
|
| 44 |
-
# 加載所有數據集
|
| 45 |
-
dataframes = load_data()
|
| 46 |
-
st.success("數據加載完成!")
|
| 47 |
-
|
| 48 |
-
# 允許用戶選擇數據集
|
| 49 |
-
dataset_choice = st.selectbox("選擇要顯示的數據集", list(dataframes.keys()))
|
| 50 |
-
|
| 51 |
-
# 獲取選定的 DataFrame
|
| 52 |
-
selected_df = dataframes[dataset_choice]
|
| 53 |
-
|
| 54 |
-
# 允許用戶選擇用於繪製圓餅圖的列
|
| 55 |
-
column_choice = st.selectbox("選擇欄位來繪製圖表", selected_df.columns)
|
| 56 |
-
|
| 57 |
# 檢查該列是否為數值類型
|
| 58 |
if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
|
| 59 |
# 創建一個標籤頁布局
|
|
@@ -70,8 +63,10 @@ if st.button("加載數據"):
|
|
| 70 |
fig_bar = px.bar(selected_df, x=selected_df.index, y=column_choice,
|
| 71 |
title=f"{dataset_choice} - {column_choice} 長條圖")
|
| 72 |
st.plotly_chart(fig_bar)
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
-
st.
|
| 75 |
|
| 76 |
# 下載並設置自定義字體以顯示中文字符
|
| 77 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|
|
|
|
| 20 |
|
| 21 |
# 下載並加載 CSV 文件到 DataFrame 的函數
|
| 22 |
@st.cache_data
|
| 23 |
+
def load_data(url):
|
| 24 |
+
response = requests.get(url)
|
| 25 |
+
response.encoding = 'utf-8'
|
| 26 |
+
df = pd.read_csv(io.StringIO(response.text), encoding='utf-8')
|
| 27 |
+
df = df.fillna(0)
|
| 28 |
+
return df
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Streamlit 應用
|
| 31 |
st.title("ESG 專題數據分析")
|
| 32 |
|
| 33 |
+
# 允許用戶選擇數據集
|
| 34 |
+
dataset_choice = st.selectbox("選擇要顯示的數據集", list(urls.keys()))
|
| 35 |
+
|
| 36 |
+
# 加載選定的數據集
|
| 37 |
+
selected_df = load_data(urls[dataset_choice])
|
| 38 |
+
|
| 39 |
+
# 允許用戶選擇用於繪製圖表的列
|
| 40 |
+
column_choice = st.selectbox("選擇欄位來繪製圖表", selected_df.columns)
|
| 41 |
+
|
| 42 |
+
# 添加一個生成圖表的按鈕
|
| 43 |
+
if st.button("生成圖表"):
|
| 44 |
# 顯示進度條
|
| 45 |
progress_bar = st.progress(0)
|
| 46 |
for i in range(100):
|
| 47 |
time.sleep(0.01)
|
| 48 |
progress_bar.progress(i + 1)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
# 檢查該列是否為數值類型
|
| 51 |
if pd.api.types.is_numeric_dtype(selected_df[column_choice]):
|
| 52 |
# 創建一個標籤頁布局
|
|
|
|
| 63 |
fig_bar = px.bar(selected_df, x=selected_df.index, y=column_choice,
|
| 64 |
title=f"{dataset_choice} - {column_choice} 長條圖")
|
| 65 |
st.plotly_chart(fig_bar)
|
| 66 |
+
|
| 67 |
+
st.success("圖表生成完成!")
|
| 68 |
else:
|
| 69 |
+
st.error("選定的欄位不是數值類型,無法繪製圖表。")
|
| 70 |
|
| 71 |
# 下載並設置自定義字體以顯示中文字符
|
| 72 |
font_url = "https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_&export=download"
|