Spaces:
Sleeping
Sleeping
File size: 1,881 Bytes
8a7220f 2356cc2 8a7220f 2356cc2 8a7220f 2356cc2 8a7220f 2356cc2 8a7220f 2356cc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import base64
# Function to create a download link
def create_download_link(file_path, link_title):
with open(file_path, 'rb') as file:
csv_data = file.read()
b64 = base64.b64encode(csv_data).decode()
return f'<a href="data:file/csv;base64,{b64}" download="{link_title}.csv">{link_title}</a>'
# Function to plot the map
def plot_map(data):
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
grouped_data = data.groupby('State')
for state, group in grouped_data:
top_corp = group.nlargest(1, 'Revenue')
text_label = f"{top_corp['Corporation'].iloc[0]} - ${top_corp['Revenue'].iloc[0]}B"
lon = group['Longitude'].mean()
lat = group['Latitude'].mean()
fig.add_trace(go.Scattergeo(
lon=[lon],
lat=[lat],
text=text_label,
mode='text',
))
fig.update_layout(title="Top Corporation by State in the United States")
return fig
st.title('Top Corporation by State in the United States ๐ข')
# Upload CSV
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
# Display map button
display_map_button = st.button('Display Map of CSV Data ๐บ๏ธ')
if display_map_button:
if uploaded_files:
for uploaded_file in uploaded_files:
data = pd.read_csv(uploaded_file)
st.write(f"Map for {uploaded_file.name} ๐")
st.plotly_chart(plot_map(data))
else:
st.write("Please upload a CSV file to proceed. ๐")
# Download link for the CSV file
csv_file_path = 'top_corporation_per_state.csv'
download_link = create_download_link(csv_file_path, "Top Corporations by State")
st.markdown(download_link, unsafe_allow_html=True) |