Spaces:
Sleeping
Sleeping
Update backup.app.py
Browse files- backup.app.py +31 -10
backup.app.py
CHANGED
@@ -1,16 +1,35 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Function to plot the map
|
6 |
def plot_map(data):
|
7 |
-
fig = px.choropleth(
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
return fig
|
11 |
|
12 |
-
|
13 |
-
st.title('Top Corporations by State in the United States')
|
14 |
|
15 |
# Upload CSV
|
16 |
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
|
@@ -18,14 +37,16 @@ uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=Tru
|
|
18 |
# Display map button
|
19 |
display_map_button = st.button('Display Map of CSV Data ๐บ๏ธ')
|
20 |
|
21 |
-
# If button is clicked, use uploaded files to generate maps or use the default CSV file
|
22 |
if display_map_button:
|
23 |
if uploaded_files:
|
24 |
for uploaded_file in uploaded_files:
|
25 |
data = pd.read_csv(uploaded_file)
|
26 |
-
st.write(f"Map for {uploaded_file.name}")
|
27 |
st.plotly_chart(plot_map(data))
|
28 |
else:
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
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:
|
10 |
+
csv_data = file.read()
|
11 |
+
b64 = base64.b64encode(csv_data).decode()
|
12 |
+
return f'<a href="data:file/csv;base64,{b64}" download="{link_title}.csv">{link_title}</a>'
|
13 |
|
14 |
# Function to plot the map
|
15 |
def plot_map(data):
|
16 |
+
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
|
17 |
+
grouped_data = data.groupby('State')
|
18 |
+
for state, group in grouped_data:
|
19 |
+
top_corp = group.nlargest(1, 'Revenue')
|
20 |
+
text_label = f"{top_corp['Corporation'].iloc[0]} - ${top_corp['Revenue'].iloc[0]}B"
|
21 |
+
lon = group['Longitude'].mean()
|
22 |
+
lat = group['Latitude'].mean()
|
23 |
+
fig.add_trace(go.Scattergeo(
|
24 |
+
lon=[lon],
|
25 |
+
lat=[lat],
|
26 |
+
text=text_label,
|
27 |
+
mode='text',
|
28 |
+
))
|
29 |
+
fig.update_layout(title="Top Corporation by State in the United States")
|
30 |
return fig
|
31 |
|
32 |
+
st.title('Top Corporation by State in the United States ๐ข')
|
|
|
33 |
|
34 |
# Upload CSV
|
35 |
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
|
|
|
37 |
# Display map button
|
38 |
display_map_button = st.button('Display Map of CSV Data ๐บ๏ธ')
|
39 |
|
|
|
40 |
if display_map_button:
|
41 |
if uploaded_files:
|
42 |
for uploaded_file in uploaded_files:
|
43 |
data = pd.read_csv(uploaded_file)
|
44 |
+
st.write(f"Map for {uploaded_file.name} ๐")
|
45 |
st.plotly_chart(plot_map(data))
|
46 |
else:
|
47 |
+
st.write("Please upload a CSV file to proceed. ๐")
|
48 |
+
|
49 |
+
# Download link for the CSV file
|
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)
|