awacke1 commited on
Commit
0631b4d
·
1 Parent(s): 91eabb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -39
app.py CHANGED
@@ -5,30 +5,23 @@ 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):
@@ -37,25 +30,23 @@ def plot_map(data):
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)
 
5
  import base64
6
 
7
  # Default data for top two corporations per state
8
+ states = [
9
+ "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
10
+ "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
11
+ "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
12
+ "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
13
+ "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada",
14
+ "New Hampshire", "New Jersey", "New Mexico", "New York",
15
+ "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon",
16
+ "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
17
+ "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
18
+ "West Virginia", "Wisconsin", "Wyoming"
19
+ ]
20
+
21
+ corporations_data = []
22
+ for state in states:
23
+ corporations_data.append((state, f'{state}Corp1', 100))
24
+ corporations_data.append((state, f'{state}Corp2', 50))
 
 
 
 
 
 
 
25
 
26
  # Function to plot the map
27
  def plot_map(data):
 
30
  hover_name=data['Corporation'], hover_data=['Revenue'])
31
  return fig
32
 
 
 
 
33
  # Streamlit app
34
  st.title('Top Corporations by State in the United States')
35
 
36
  # Upload CSV
37
  uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
38
 
39
+ # Display map button
40
+ display_map_button = st.button('Display Map of CSV Data 🗺️')
41
+
42
+ # If button is clicked, use uploaded files to generate maps
43
+ if display_map_button:
44
+ if uploaded_files:
45
+ for uploaded_file in uploaded_files:
46
+ data = pd.read_csv(uploaded_file)
47
+ st.write(f"Map for {uploaded_file.name}")
48
+ st.plotly_chart(plot_map(data))
49
+ else:
50
+ # Use default data if no files are uploaded
51
+ data = pd.DataFrame(corporations_data, columns=['State', 'Corporation', 'Revenue'])
52
  st.plotly_chart(plot_map(data))