Spaces:
Sleeping
Sleeping
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) |