File size: 2,065 Bytes
f542e7e
017ce74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f542e7e
 
 
017ce74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
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'<a href="data:file/csv;base64,{b64}" download="{file_name}">Download CSV File 📥</a>'
    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)