petertulip86 commited on
Commit
91f3d9a
·
verified ·
1 Parent(s): e0f2bce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from bs4 import BeautifulSoup
4
+ import pandas as pd
5
+ import plotly.express as px
6
+
7
+ def main():
8
+ st.set_page_config(page_title="奇美醫院病床占用率查詢", layout="wide")
9
+
10
+ st.title("奇美醫院病床占用率查詢系統")
11
+ st.write("此應用程式顯示奇美醫院的即時病床占用情況")
12
+
13
+ # 添加刷新按鈕
14
+ if st.button("刷新數據"):
15
+ st.cache_data.clear()
16
+
17
+ # 獲取病床數據
18
+ df = get_hospital_bed_data()
19
+
20
+ # 顯示數據
21
+ col1, col2 = st.columns([2, 3])
22
+
23
+ with col1:
24
+ st.subheader("病床占用數據表")
25
+ st.dataframe(df, use_container_width=True)
26
+
27
+ # 計算總數
28
+ total_beds = df['總床數'].sum()
29
+ total_occupied = df['佔床數'].sum()
30
+ total_available = df['空床數'].sum()
31
+ overall_rate = f"{(total_occupied / total_beds * 100):.2f}%" if total_beds > 0 else "0%"
32
+
33
+ st.subheader("總計")
34
+ total_data = pd.DataFrame({
35
+ '項目': ['總病床數', '總佔床數', '總空床數', '整體佔床率'],
36
+ '數值': [total_beds, total_occupied, total_available, overall_rate]
37
+ })
38
+ st.dataframe(total_data, use_container_width=True, hide_index=True)
39
+
40
+ with col2:
41
+ st.subheader("病床占用率視覺化")
42
+
43
+ # 創建占用率條形圖
44
+ fig = px.bar(
45
+ df,
46
+ x='病床類別',
47
+ y='佔床數',
48
+ text='佔床率',
49
+ color='佔床率',
50
+ color_continuous_scale='RdYlGn_r', # 紅(高)-黃-綠(低)色彩範圍
51
+ labels={'佔床數': '已占用病床數', '病床類別': '病床類型', '佔床率': '占用率'},
52
+ height=500
53
+ )
54
+ fig.update_layout(xaxis={'categoryorder': 'total descending'})
55
+ st.plotly_chart(fig, use_container_width=True)
56
+
57
+ # 創建餅圖顯示空床/占床比例
58
+ pie_data = pd.DataFrame({
59
+ '狀態': ['已占用', '可用'],
60
+ '數量': [total_occupied, total_available]
61
+ })
62
+ pie_fig = px.pie(
63
+ pie_data,
64
+ values='數量',
65
+ names='狀態',
66
+ color='狀態',
67
+ color_discrete_map={'已占用': 'red', '可用': 'green'},
68
+ hole=0.4
69
+ )
70
+ st.plotly_chart(pie_fig, use_container_width=True)
71
+
72
+ @st.cache_data(ttl=300) # 資料快取5分鐘
73
+ def get_hospital_bed_data():
74
+ """獲取奇美醫院病床占用數據"""
75
+ try:
76
+ # URL of the page to scrape
77
+ url = "https://www.chimei.org.tw/%E4%BD%94%E5%BA%8A%E7%8E%87%E6%9F%A5%E8%A9%A2/%E4%BD%94%E5%BA%8A%E7%8E%87%E6%9F%A5%E8%A9%A2.aspx?ihospital=10&ffloor="
78
+
79
+ # Send HTTP request
80
+ response = requests.get(url)
81
+ response.encoding = 'utf-8' # Set encoding to handle Chinese characters
82
+
83
+ # Create BeautifulSoup object
84
+ soup = BeautifulSoup(response.text, 'html.parser')
85
+
86
+ # Find the target table (DG1)
87
+ target_table = soup.find('table', {'id': 'DG1'})
88
+
89
+ # Extract data from the table
90
+ data = []
91
+ if target_table:
92
+ rows = target_table.find_all('tr')
93
+
94
+ # Skip the header row
95
+ for row in rows[1:]:
96
+ cols = row.find_all('td')
97
+ if len(cols) == 5: # Ensure row has 5 columns
98
+ bed_type = cols[0].text.strip()
99
+ total_beds = int(cols[1].text.strip())
100
+ occupied_beds = int(cols[2].text.strip())
101
+ available_beds = int(cols[3].text.strip())
102
+ occupancy_rate = cols[4].text.strip()
103
+
104
+ data.append({
105
+ '病床類別': bed_type,
106
+ '總床數': total_beds,
107
+ '佔床數': occupied_beds,
108
+ '空床數': available_beds,
109
+ '佔床率': occupancy_rate
110
+ })
111
+
112
+ # Create pandas DataFrame
113
+ df = pd.DataFrame(data)
114
+ return df
115
+
116
+ except Exception as e:
117
+ st.error(f"獲取數據時發生錯誤: {e}")
118
+ return pd.DataFrame({
119
+ '病床類別': [],
120
+ '總床數': [],
121
+ '佔床數': [],
122
+ '空床數': [],
123
+ '佔床率': []
124
+ })
125
+
126
+ if __name__ == "__main__":
127
+ main()