awacke1 commited on
Commit
017ce74
·
1 Parent(s): 3966abc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -21
app.py CHANGED
@@ -1,25 +1,61 @@
1
  import streamlit as st
2
- import geopandas as gpd
3
- import matplotlib.pyplot as plt
4
-
5
- # Function to plot the map with labels
6
- def plot_map():
7
- us_map = gpd.read_file('path_to_shapefile.shp')
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
- us_map.plot()
14
- for state, data in corporations_data.items():
15
- geometry = us_map[us_map['STATE_NAME'] == state].geometry.iloc[0]
16
- centroid = geometry.centroid
17
- for i, (corp, revenue) in enumerate(data):
18
- plt.text(centroid.x, centroid.y - i * 0.1, f'{corp}: ${revenue}B', fontsize=10)
19
- plt.axis('off')
20
- plt.show()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Streamlit app
23
  st.title('Top Corporations by State in the United States')
24
- st.write('This map shows the top 2 corporations in each state with their revenue in billions of dollars for 2021.')
25
- plot_map()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)