Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,7 @@ import plotly.graph_objects as go
|
|
5 |
|
6 |
# Function to plot the map
|
7 |
def plot_map(data):
|
8 |
-
# Create a choropleth map as a base
|
9 |
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
|
10 |
-
|
11 |
-
# Add a scatter plot on top to show the corporation labels
|
12 |
for index, row in data.iterrows():
|
13 |
fig.add_trace(go.Scattergeo(
|
14 |
lon=[row['Longitude']],
|
@@ -16,13 +13,15 @@ def plot_map(data):
|
|
16 |
text=f"{row['Corporation']} - ${row['Revenue']}B",
|
17 |
mode='text',
|
18 |
))
|
19 |
-
|
20 |
-
# Set the title
|
21 |
fig.update_layout(title="Top Corporations by State in the United States")
|
22 |
-
|
23 |
return fig
|
24 |
|
25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
26 |
st.title('Top Corporations by State in the United States')
|
27 |
|
28 |
# Upload CSV
|
@@ -31,14 +30,23 @@ uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=Tru
|
|
31 |
# Display map button
|
32 |
display_map_button = st.button('Display Map of CSV Data 🗺️')
|
33 |
|
34 |
-
#
|
35 |
-
|
|
|
|
|
36 |
if uploaded_files:
|
37 |
for uploaded_file in uploaded_files:
|
38 |
data = pd.read_csv(uploaded_file)
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
else:
|
42 |
-
|
43 |
-
data = pd.read_csv('corporations_data.csv')
|
44 |
-
st.plotly_chart(plot_map(data))
|
|
|
5 |
|
6 |
# Function to plot the map
|
7 |
def plot_map(data):
|
|
|
8 |
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
|
|
|
|
|
9 |
for index, row in data.iterrows():
|
10 |
fig.add_trace(go.Scattergeo(
|
11 |
lon=[row['Longitude']],
|
|
|
13 |
text=f"{row['Corporation']} - ${row['Revenue']}B",
|
14 |
mode='text',
|
15 |
))
|
|
|
|
|
16 |
fig.update_layout(title="Top Corporations by State in the United States")
|
|
|
17 |
return fig
|
18 |
|
19 |
+
# Function to generate the CSV file
|
20 |
+
def generate_csv(data):
|
21 |
+
top_corporations = data.nlargest(2, 'Revenue')
|
22 |
+
csv_file = top_corporations.to_csv(index=False)
|
23 |
+
return csv_file
|
24 |
+
|
25 |
st.title('Top Corporations by State in the United States')
|
26 |
|
27 |
# Upload CSV
|
|
|
30 |
# Display map button
|
31 |
display_map_button = st.button('Display Map of CSV Data 🗺️')
|
32 |
|
33 |
+
# Download CSV button
|
34 |
+
download_csv_button = st.button('Download Top Two Corporations CSV 📥')
|
35 |
+
|
36 |
+
if display_map_button or download_csv_button:
|
37 |
if uploaded_files:
|
38 |
for uploaded_file in uploaded_files:
|
39 |
data = pd.read_csv(uploaded_file)
|
40 |
+
if display_map_button:
|
41 |
+
st.write(f"Map for {uploaded_file.name}")
|
42 |
+
st.plotly_chart(plot_map(data))
|
43 |
+
if download_csv_button:
|
44 |
+
csv_file = generate_csv(data)
|
45 |
+
st.download_button(
|
46 |
+
label="Download CSV File",
|
47 |
+
data=csv_file,
|
48 |
+
file_name="top_two_corporations.csv",
|
49 |
+
mime="text/csv"
|
50 |
+
)
|
51 |
else:
|
52 |
+
st.write("Please upload a CSV file to proceed.")
|
|
|
|