import streamlit as st import pandas as pd import plotly.express as px import csv import base64 # Default data for top two corporations per state corporations_data = { 'Minnesota': [('CorpA', 100), ('CorpB', 50)], 'California': [('TechCorp', 300), ('HealthCorp', 200)], # Add more states and corporations as needed } # Function to create a CSV file from the corporations_data dictionary def create_csv(): csv_file = 'corporations_data.csv' with open(csv_file, 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['State', 'Corporation', 'Revenue']) for state, data in corporations_data.items(): for corp, revenue in data: writer.writerow([state, corp, revenue]) return csv_file # Function to download CSV def download_csv(file_name): with open(file_name, 'r') as file: csv_data = file.read() b64 = base64.b64encode(csv_data.encode()).decode() href = f'Download CSV File 📥' return href # Function to plot the map def plot_map(data): fig = px.choropleth(locations=data['State'], locationmode="USA-states", color=data['Revenue'], scope="usa", title="Top Corporations by State in the United States", hover_name=data['Corporation'], hover_data=['Revenue']) return fig # Create CSV from default data csv_file = create_csv() # Streamlit app st.title('Top Corporations by State in the United States') # Upload CSV uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv") # If files are uploaded, use them to generate maps 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: # Use default data data = pd.read_csv(csv_file) st.plotly_chart(plot_map(data)) # Download CSV st.markdown(download_csv(csv_file), unsafe_allow_html=True)