Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,130 @@ import pandas as pd
|
|
3 |
import plotly.express as px
|
4 |
import plotly.graph_objects as go
|
5 |
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Function to create a download link
|
8 |
def create_download_link(file_path, link_title):
|
9 |
with open(file_path, 'rb') as file:
|
@@ -50,3 +173,64 @@ if display_map_button:
|
|
50 |
csv_file_path = 'top_corporation_per_state.csv'
|
51 |
download_link = create_download_link(csv_file_path, "Top Corporations by State")
|
52 |
st.markdown(download_link, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import plotly.express as px
|
4 |
import plotly.graph_objects as go
|
5 |
import base64
|
6 |
+
import folium
|
7 |
+
from folium import Choropleth, Circle, Marker, Popup, LayerControl
|
8 |
+
from folium.plugins import HeatMap
|
9 |
+
import pandas as pd
|
10 |
+
|
11 |
+
def heatmap_of_revenue_by_state(df):
|
12 |
+
m1 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
13 |
+
heat_data = [[row['Latitude'], row['Longitude'], row['Revenue']] for index, row in df.iterrows()]
|
14 |
+
HeatMap(heat_data).add_to(m1)
|
15 |
+
return m1
|
16 |
+
|
17 |
+
def top_corporations_by_state(df):
|
18 |
+
m2 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
19 |
+
for idx, row in df.iterrows():
|
20 |
+
Marker([row['Latitude'], row['Longitude']], popup=f"{row['Corporation']} - Revenue: ${row['Revenue']}M").add_to(m2)
|
21 |
+
return m2
|
22 |
+
|
23 |
+
def revenue_bands_by_state(df):
|
24 |
+
m3 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
25 |
+
def color_producer(val):
|
26 |
+
if val < 2000:
|
27 |
+
return 'green'
|
28 |
+
elif val < 5000:
|
29 |
+
return 'orange'
|
30 |
+
else:
|
31 |
+
return 'red'
|
32 |
+
for idx, row in df.iterrows():
|
33 |
+
Circle(
|
34 |
+
location=[row['Latitude'], row['Longitude']],
|
35 |
+
radius=int(row['Revenue']),
|
36 |
+
color=color_producer(row['Revenue'])).add_to(m3)
|
37 |
+
return m3
|
38 |
+
|
39 |
+
def multi_layer_map(df):
|
40 |
+
m4 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
41 |
+
heat_data = [[row['Latitude'], row['Longitude'], row['Revenue']] for index, row in df.iterrows()]
|
42 |
+
HeatMap(heat_data).add_to(folium.FeatureGroup(name='Heat Map').add_to(m4))
|
43 |
+
LayerControl().add_to(m4)
|
44 |
+
for idx, row in df.iterrows():
|
45 |
+
Marker([row['Latitude'], row['Longitude']], popup=f"{row['Corporation']} - Revenue: ${row['Revenue']}M").add_to(folium.FeatureGroup(name='Corporations').add_to(m4))
|
46 |
+
LayerControl().add_to(m4)
|
47 |
+
return m4
|
48 |
+
|
49 |
+
# 5. Revenue Change Over Time (Simulated)
|
50 |
+
def revenue_change_over_time(df):
|
51 |
+
df['Year'] = [2019 + i % 3 for i in range(len(df))]
|
52 |
+
m5 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
53 |
+
for year in [2019, 2020, 2021]:
|
54 |
+
df_year = df[df['Year'] == year]
|
55 |
+
heat_data_year = [[row['Latitude'], row['Longitude'], row['Revenue']] for index, row in df_year.iterrows()]
|
56 |
+
HeatMap(heat_data_year, name=f"Revenue in {year}").add_to(folium.FeatureGroup(name=f"Year {year}").add_to(m5))
|
57 |
+
LayerControl().add_to(m5)
|
58 |
+
return m5
|
59 |
+
|
60 |
+
# 6. Distribution of Corporations
|
61 |
+
def distribution_of_corporations(df):
|
62 |
+
m6 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
63 |
+
corporation_count = df['State'].value_counts().reset_index()
|
64 |
+
corporation_count.columns = ['State', 'Count']
|
65 |
+
for idx, row in corporation_count.iterrows():
|
66 |
+
state_row = df[df['State'] == row['State']].iloc[0]
|
67 |
+
Circle(
|
68 |
+
location=[state_row['Latitude'], state_row['Longitude']],
|
69 |
+
radius=row['Count']*1000,
|
70 |
+
color='blue',
|
71 |
+
fill=True,
|
72 |
+
fill_color='blue'
|
73 |
+
).add_to(m6)
|
74 |
+
return m6
|
75 |
+
|
76 |
+
# 7. Type of Corporations (Simulated)
|
77 |
+
def type_of_corporations(df):
|
78 |
+
df['Type'] = ['Tech', 'Health', 'Finance', 'Retail'] * (len(df) // 4)
|
79 |
+
m7 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
80 |
+
for corp_type in ['Tech', 'Health', 'Finance', 'Retail']:
|
81 |
+
df_type = df[df['Type'] == corp_type]
|
82 |
+
for idx, row in df_type.iterrows():
|
83 |
+
Marker([row['Latitude'], row['Longitude']],
|
84 |
+
popup=f"{row['Corporation']} - Type: {row['Type']}",
|
85 |
+
icon=folium.Icon(color='blue' if corp_type == 'Tech' else 'green' if corp_type == 'Health' else 'red' if corp_type == 'Finance' else 'orange')).add_to(m7)
|
86 |
+
return m7
|
87 |
|
88 |
+
# 8. Interactive Map
|
89 |
+
def interactive_map(df):
|
90 |
+
m8 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
91 |
+
for idx, row in df.iterrows():
|
92 |
+
popup_content = f"""
|
93 |
+
<strong>Corporation:</strong> {row['Corporation']}<br>
|
94 |
+
<strong>State:</strong> {row['State']}<br>
|
95 |
+
<strong>Revenue:</strong> ${row['Revenue']}M<br>
|
96 |
+
<strong>Type:</strong> {row['Type']}
|
97 |
+
"""
|
98 |
+
Popup(popup_content, max_width=300).add_to(
|
99 |
+
Marker([row['Latitude'], row['Longitude']]).add_to(m8)
|
100 |
+
)
|
101 |
+
return m8
|
102 |
+
|
103 |
+
# 9. Geographical Clusters of Revenue
|
104 |
+
from folium.plugins import MarkerCluster
|
105 |
+
|
106 |
+
def geographical_clusters_of_revenue(df):
|
107 |
+
m9 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
108 |
+
marker_cluster = MarkerCluster().add_to(m9)
|
109 |
+
for idx, row in df.iterrows():
|
110 |
+
Marker(
|
111 |
+
location=[row['Latitude'], row['Longitude']],
|
112 |
+
popup=f"{row['Corporation']} - Revenue: ${row['Revenue']}M"
|
113 |
+
).add_to(marker_cluster)
|
114 |
+
return m9
|
115 |
+
|
116 |
+
# 10. Revenue vs. Location (Simulated 3D Map)
|
117 |
+
def revenue_vs_location(df):
|
118 |
+
m10 = folium.Map(location=[37.7749, -100.4194], zoom_start=4)
|
119 |
+
for idx, row in df.iterrows():
|
120 |
+
Circle(
|
121 |
+
location=[row['Latitude'], row['Longitude']],
|
122 |
+
radius=row['Revenue'],
|
123 |
+
color='purple',
|
124 |
+
fill=True,
|
125 |
+
fill_color='purple'
|
126 |
+
).add_to(m10)
|
127 |
+
return m10
|
128 |
+
|
129 |
+
|
130 |
# Function to create a download link
|
131 |
def create_download_link(file_path, link_title):
|
132 |
with open(file_path, 'rb') as file:
|
|
|
173 |
csv_file_path = 'top_corporation_per_state.csv'
|
174 |
download_link = create_download_link(csv_file_path, "Top Corporations by State")
|
175 |
st.markdown(download_link, unsafe_allow_html=True)
|
176 |
+
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
import streamlit as st
|
182 |
+
import folium
|
183 |
+
from folium import Marker, Popup, Circle, LayerControl
|
184 |
+
from folium.plugins import HeatMap, MarkerCluster
|
185 |
+
import pandas as pd
|
186 |
+
import base64
|
187 |
+
|
188 |
+
def get_map_html_string(m):
|
189 |
+
m.save("tmp_map.html")
|
190 |
+
with open("tmp_map.html", "r") as f:
|
191 |
+
html_string = f.read()
|
192 |
+
return html_string
|
193 |
+
|
194 |
+
# Functions to generate maps
|
195 |
+
#{code_listing_5_6}
|
196 |
+
#{code_listing_7_10}
|
197 |
+
|
198 |
+
# Function to provide download link for HTML string
|
199 |
+
def provide_download_link(html_string, filename):
|
200 |
+
b64 = base64.b64encode(html_string.encode()).decode()
|
201 |
+
download_link = f'<a href="data:text/html;charset=utf-8;base64,{b64}" download="{filename}">Download {filename}</a>'
|
202 |
+
return download_link
|
203 |
+
|
204 |
+
# Streamlit App
|
205 |
+
st.title("Map Visuals")
|
206 |
+
|
207 |
+
# Load data
|
208 |
+
st.write("### Upload your CSV file containing the data:")
|
209 |
+
uploaded_file = st.file_uploader("Choose a file", type=['csv'])
|
210 |
+
|
211 |
+
if uploaded_file is not None:
|
212 |
+
df = pd.read_csv(uploaded_file)
|
213 |
+
|
214 |
+
# Generate Maps
|
215 |
+
map_list = [
|
216 |
+
("Heatmap of Revenue by State", heatmap_of_revenue_by_state),
|
217 |
+
("Top Corporations by State", top_corporations_by_state),
|
218 |
+
("Revenue Bands by State", revenue_bands_by_state),
|
219 |
+
("Multi-layer Map", multi_layer_map),
|
220 |
+
("Revenue Change Over Time", revenue_change_over_time),
|
221 |
+
("Distribution of Corporations", distribution_of_corporations),
|
222 |
+
("Type of Corporations", type_of_corporations),
|
223 |
+
("Interactive Map", interactive_map),
|
224 |
+
("Geographical Clusters of Revenue", geographical_clusters_of_revenue),
|
225 |
+
("Revenue vs. Location", revenue_vs_location)
|
226 |
+
]
|
227 |
+
|
228 |
+
for idx, (title, func) in enumerate(map_list):
|
229 |
+
st.write(f"### {title}")
|
230 |
+
m = func(df)
|
231 |
+
html_string = get_map_html_string(m)
|
232 |
+
st.components.v1.html(html_string, width=900, height=600)
|
233 |
+
st.markdown(provide_download_link(html_string, f"{title.replace(' ', '_')}.html"), unsafe_allow_html=True)
|
234 |
+
|
235 |
+
|
236 |
+
streamlit_file_path
|