Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,61 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Streamlit app
|
23 |
st.title('Top Corporations by State in the United States')
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import csv
|
5 |
+
import base64
|
6 |
+
|
7 |
+
# Default data for top two corporations per state
|
8 |
+
corporations_data = {
|
9 |
+
'Minnesota': [('CorpA', 100), ('CorpB', 50)],
|
10 |
+
'California': [('TechCorp', 300), ('HealthCorp', 200)],
|
11 |
+
# Add more states and corporations as needed
|
12 |
+
}
|
13 |
+
|
14 |
+
# Function to create a CSV file from the corporations_data dictionary
|
15 |
+
def create_csv():
|
16 |
+
csv_file = 'corporations_data.csv'
|
17 |
+
with open(csv_file, 'w', newline='') as file:
|
18 |
+
writer = csv.writer(file)
|
19 |
+
writer.writerow(['State', 'Corporation', 'Revenue'])
|
20 |
+
for state, data in corporations_data.items():
|
21 |
+
for corp, revenue in data:
|
22 |
+
writer.writerow([state, corp, revenue])
|
23 |
+
return csv_file
|
24 |
+
|
25 |
+
# Function to download CSV
|
26 |
+
def download_csv(file_name):
|
27 |
+
with open(file_name, 'r') as file:
|
28 |
+
csv_data = file.read()
|
29 |
+
b64 = base64.b64encode(csv_data.encode()).decode()
|
30 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="{file_name}">Download CSV File 📥</a>'
|
31 |
+
return href
|
32 |
+
|
33 |
+
# Function to plot the map
|
34 |
+
def plot_map(data):
|
35 |
+
fig = px.choropleth(locations=data['State'], locationmode="USA-states", color=data['Revenue'],
|
36 |
+
scope="usa", title="Top Corporations by State in the United States",
|
37 |
+
hover_name=data['Corporation'], hover_data=['Revenue'])
|
38 |
+
return fig
|
39 |
+
|
40 |
+
# Create CSV from default data
|
41 |
+
csv_file = create_csv()
|
42 |
|
43 |
# Streamlit app
|
44 |
st.title('Top Corporations by State in the United States')
|
45 |
+
|
46 |
+
# Upload CSV
|
47 |
+
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
|
48 |
+
|
49 |
+
# If files are uploaded, use them to generate maps
|
50 |
+
if uploaded_files:
|
51 |
+
for uploaded_file in uploaded_files:
|
52 |
+
data = pd.read_csv(uploaded_file)
|
53 |
+
st.write(f"Map for {uploaded_file.name}")
|
54 |
+
st.plotly_chart(plot_map(data))
|
55 |
+
else:
|
56 |
+
# Use default data
|
57 |
+
data = pd.read_csv(csv_file)
|
58 |
+
st.plotly_chart(plot_map(data))
|
59 |
+
|
60 |
+
# Download CSV
|
61 |
+
st.markdown(download_csv(csv_file), unsafe_allow_html=True)
|