Roberta2024 commited on
Commit
f960e54
1 Parent(s): f2b678e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -6
app.py CHANGED
@@ -2,16 +2,16 @@ import requests
2
  from bs4 import BeautifulSoup
3
  import pandas as pd
4
  import folium
5
- from folium.plugins import MarkerCluster
6
  import plotly.graph_objects as go
7
- import plotly.express as px # Add this import
8
  from geopy.geocoders import Nominatim
9
  import re
10
  import streamlit as st
11
 
12
  # Streamlit title and description
13
  st.title("米其林餐廳指南爬蟲")
14
- st.write("Extract restaurant data, visualize with a pie chart and bar chart, and display locations on a map.")
15
 
16
  # Read data from Google Sheets
17
  sheet_id = "1xUfnD1WCF5ldqECI8YXIko1gCpaDDCwTztL17kjI42U"
@@ -58,7 +58,6 @@ def fetch_data():
58
  address = None
59
  region = "Unknown"
60
 
61
- # Try to extract phone number
62
  try:
63
  phone = soup.find("a", {"data-event": "CTA_tel"}).get("href").replace("tel:", "")
64
  except AttributeError:
@@ -158,8 +157,8 @@ if st.button("爬取餐廳資料"):
158
  # Display a map using Folium
159
  st.subheader("Restaurant Locations Map")
160
 
161
- # Create map centered around Tainan
162
- m = folium.Map(location=[23.0, 120.2], zoom_start=12)
163
 
164
  # Add marker cluster to the map
165
  marker_cluster = MarkerCluster().add_to(m)
@@ -173,3 +172,37 @@ if st.button("爬取餐廳資料"):
173
 
174
  # Display the map in Streamlit
175
  st.components.v1.html(m._repr_html_(), height=600)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from bs4 import BeautifulSoup
3
  import pandas as pd
4
  import folium
5
+ from folium.plugins import MarkerCluster, HeatMap
6
  import plotly.graph_objects as go
7
+ import plotly.express as px
8
  from geopy.geocoders import Nominatim
9
  import re
10
  import streamlit as st
11
 
12
  # Streamlit title and description
13
  st.title("米其林餐廳指南爬蟲")
14
+ st.write("Extract restaurant data, visualize with charts, and display locations on maps.")
15
 
16
  # Read data from Google Sheets
17
  sheet_id = "1xUfnD1WCF5ldqECI8YXIko1gCpaDDCwTztL17kjI42U"
 
58
  address = None
59
  region = "Unknown"
60
 
 
61
  try:
62
  phone = soup.find("a", {"data-event": "CTA_tel"}).get("href").replace("tel:", "")
63
  except AttributeError:
 
157
  # Display a map using Folium
158
  st.subheader("Restaurant Locations Map")
159
 
160
+ # Create map centered around the mean latitude and longitude
161
+ m = folium.Map(location=[df['Latitude'].mean(), df['Longitude'].mean()], zoom_start=10)
162
 
163
  # Add marker cluster to the map
164
  marker_cluster = MarkerCluster().add_to(m)
 
172
 
173
  # Display the map in Streamlit
174
  st.components.v1.html(m._repr_html_(), height=600)
175
+
176
+ # New section for heatmap
177
+ st.header("餐廳分布熱力圖")
178
+
179
+ # Prepare data for heatmap
180
+ heat_data = [[row['Latitude'], row['Longitude']] for index, row in df.iterrows() if pd.notnull(row['Latitude']) and pd.notnull(row['Longitude'])]
181
+
182
+ # Create a new map for the heatmap
183
+ heatmap = folium.Map(location=[df['Latitude'].mean(), df['Longitude'].mean()], zoom_start=10)
184
+
185
+ # Add heatmap to the map
186
+ HeatMap(heat_data).add_to(heatmap)
187
+
188
+ # Display the heatmap in Streamlit
189
+ st.components.v1.html(heatmap._repr_html_(), height=600)
190
+
191
+ # Regional restaurant count analysis
192
+ st.header("各區域餐廳數量分析")
193
+
194
+ # Create bar chart for restaurant count by region using Plotly Express
195
+ fig_bar = px.bar(region_group, x='Region', y='Count',
196
+ title="各區域餐廳數量比較",
197
+ color='Count',
198
+ color_continuous_scale=px.colors.sequential.Viridis)
199
+ st.plotly_chart(fig_bar)
200
+
201
+ # Create a scatter mapbox for individual restaurant locations
202
+ fig_scatter = px.scatter_mapbox(df, lat="Latitude", lon="Longitude",
203
+ hover_name="Store Name",
204
+ hover_data=["Address", "Phone"],
205
+ zoom=10, height=600,
206
+ title="餐廳位置分布圖")
207
+ fig_scatter.update_layout(mapbox_style="open-street-map")
208
+ st.plotly_chart(fig_scatter)