Roberta2024 commited on
Commit
8567ba1
·
verified ·
1 Parent(s): 9abd52a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
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"
@@ -113,25 +113,22 @@ if st.button("爬取餐廳資料"):
113
  # Group the data by region and sum the number of restaurants
114
  region_group = df.groupby("Region").size().reset_index(name='Count')
115
 
116
- # Plot enlarged pie chart with custom colors and labels
117
- pie_chart = go.Figure(go.Pie(
118
- labels=region_group["Region"],
119
- values=region_group["Count"],
120
- textinfo="label+percent",
121
- hoverinfo="label+value",
122
- textfont=dict(size=18),
123
- marker=dict(colors=px.colors.qualitative.Set3, line=dict(color='#000000', width=2))
124
- ))
125
-
126
- pie_chart.update_layout(
127
  title="Restaurant Distribution by Region",
 
 
 
128
  title_x=0.5,
129
  title_font=dict(size=24, family="Arial"),
130
  height=600,
131
  margin=dict(t=50, b=50, l=50, r=50)
132
  )
133
- st.subheader("Restaurant Distribution by Region (Enlarged Pie Chart)")
134
- st.plotly_chart(pie_chart)
135
 
136
  # Plot bar chart with custom colors and labels
137
  bar_chart = go.Figure(go.Bar(
@@ -156,13 +153,17 @@ if st.button("爬取餐廳資料"):
156
  st.plotly_chart(bar_chart)
157
 
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)
 
 
 
 
166
  for index, row in df.iterrows():
167
  if pd.notnull(row["Latitude"]) and pd.notnull(row["Longitude"]):
168
  folium.Marker(
@@ -170,6 +171,10 @@ if st.button("爬取餐廳資料"):
170
  popup=f"{row['Store Name']} ({row['Phone']})",
171
  tooltip=row["Address"]
172
  ).add_to(marker_cluster)
 
 
 
 
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 a Sunburst chart and bar chart, and display locations on a map with heatmap.")
15
 
16
  # Read data from Google Sheets
17
  sheet_id = "1xUfnD1WCF5ldqECI8YXIko1gCpaDDCwTztL17kjI42U"
 
113
  # Group the data by region and sum the number of restaurants
114
  region_group = df.groupby("Region").size().reset_index(name='Count')
115
 
116
+ # Plot Sunburst chart
117
+ sunburst = px.sunburst(
118
+ region_group,
119
+ path=['Region'],
120
+ values='Count',
 
 
 
 
 
 
121
  title="Restaurant Distribution by Region",
122
+ )
123
+
124
+ sunburst.update_layout(
125
  title_x=0.5,
126
  title_font=dict(size=24, family="Arial"),
127
  height=600,
128
  margin=dict(t=50, b=50, l=50, r=50)
129
  )
130
+ st.subheader("Restaurant Distribution by Region (Sunburst Chart)")
131
+ st.plotly_chart(sunburst)
132
 
133
  # Plot bar chart with custom colors and labels
134
  bar_chart = go.Figure(go.Bar(
 
153
  st.plotly_chart(bar_chart)
154
 
155
  # Display a map using Folium
156
+ st.subheader("Restaurant Locations Map with Heatmap")
157
 
158
  # Create map centered around Tainan
159
  m = folium.Map(location=[23.0, 120.2], zoom_start=12)
160
 
161
  # Add marker cluster to the map
162
  marker_cluster = MarkerCluster().add_to(m)
163
+
164
+ # Prepare data for heatmap
165
+ heat_data = []
166
+
167
  for index, row in df.iterrows():
168
  if pd.notnull(row["Latitude"]) and pd.notnull(row["Longitude"]):
169
  folium.Marker(
 
171
  popup=f"{row['Store Name']} ({row['Phone']})",
172
  tooltip=row["Address"]
173
  ).add_to(marker_cluster)
174
+ heat_data.append([row["Latitude"], row["Longitude"]])
175
+
176
+ # Add heatmap layer
177
+ HeatMap(heat_data).add_to(m)
178
 
179
  # Display the map in Streamlit
180
+ st.components.v1.html(m._repr_html_(), height=600)