awacke1 commited on
Commit
2356cc2
ยท
1 Parent(s): d83ba92

Update backup.app.py

Browse files
Files changed (1) hide show
  1. backup.app.py +31 -10
backup.app.py CHANGED
@@ -1,16 +1,35 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
 
 
 
 
 
 
 
 
 
4
 
5
  # Function to plot the map
6
  def plot_map(data):
7
- fig = px.choropleth(data, locations='State', locationmode="USA-states", color='Revenue',
8
- scope="usa", title="Top Corporations by State in the United States",
9
- hover_name='Corporation', hover_data=['Revenue'])
 
 
 
 
 
 
 
 
 
 
 
10
  return fig
11
 
12
- # Streamlit app
13
- st.title('Top Corporations by State in the United States')
14
 
15
  # Upload CSV
16
  uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
@@ -18,14 +37,16 @@ uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=Tru
18
  # Display map button
19
  display_map_button = st.button('Display Map of CSV Data ๐Ÿ—บ๏ธ')
20
 
21
- # If button is clicked, use uploaded files to generate maps or use the default CSV file
22
  if display_map_button:
23
  if uploaded_files:
24
  for uploaded_file in uploaded_files:
25
  data = pd.read_csv(uploaded_file)
26
- st.write(f"Map for {uploaded_file.name}")
27
  st.plotly_chart(plot_map(data))
28
  else:
29
- # Use the default CSV file if no files are uploaded
30
- data = pd.read_csv('corporations_data.csv')
31
- st.plotly_chart(plot_map(data))
 
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import plotly.express as px
4
+ import plotly.graph_objects as go
5
+ import base64
6
+
7
+ # Function to create a download link
8
+ def create_download_link(file_path, link_title):
9
+ with open(file_path, 'rb') as file:
10
+ csv_data = file.read()
11
+ b64 = base64.b64encode(csv_data).decode()
12
+ return f'<a href="data:file/csv;base64,{b64}" download="{link_title}.csv">{link_title}</a>'
13
 
14
  # Function to plot the map
15
  def plot_map(data):
16
+ fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
17
+ grouped_data = data.groupby('State')
18
+ for state, group in grouped_data:
19
+ top_corp = group.nlargest(1, 'Revenue')
20
+ text_label = f"{top_corp['Corporation'].iloc[0]} - ${top_corp['Revenue'].iloc[0]}B"
21
+ lon = group['Longitude'].mean()
22
+ lat = group['Latitude'].mean()
23
+ fig.add_trace(go.Scattergeo(
24
+ lon=[lon],
25
+ lat=[lat],
26
+ text=text_label,
27
+ mode='text',
28
+ ))
29
+ fig.update_layout(title="Top Corporation by State in the United States")
30
  return fig
31
 
32
+ st.title('Top Corporation by State in the United States ๐Ÿข')
 
33
 
34
  # Upload CSV
35
  uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
 
37
  # Display map button
38
  display_map_button = st.button('Display Map of CSV Data ๐Ÿ—บ๏ธ')
39
 
 
40
  if display_map_button:
41
  if uploaded_files:
42
  for uploaded_file in uploaded_files:
43
  data = pd.read_csv(uploaded_file)
44
+ st.write(f"Map for {uploaded_file.name} ๐ŸŒ")
45
  st.plotly_chart(plot_map(data))
46
  else:
47
+ st.write("Please upload a CSV file to proceed. ๐Ÿ“‘")
48
+
49
+ # Download link for the CSV file
50
+ csv_file_path = 'top_corporation_per_state.csv'
51
+ download_link = create_download_link(csv_file_path, "Top Corporations by State")
52
+ st.markdown(download_link, unsafe_allow_html=True)