Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,23 +6,22 @@ import plotly.graph_objects as go
|
|
6 |
# Function to plot the map
|
7 |
def plot_map(data):
|
8 |
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
10 |
fig.add_trace(go.Scattergeo(
|
11 |
-
lon=[
|
12 |
-
lat=[
|
13 |
-
text=
|
14 |
mode='text',
|
15 |
))
|
16 |
-
fig.update_layout(title="Top
|
17 |
return fig
|
18 |
|
19 |
-
|
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
|
28 |
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
|
@@ -30,23 +29,11 @@ uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=Tru
|
|
30 |
# Display map button
|
31 |
display_map_button = st.button('Display Map of CSV Data ๐บ๏ธ')
|
32 |
|
33 |
-
|
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 |
-
|
41 |
-
|
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.")
|
|
|
6 |
# Function to plot the map
|
7 |
def plot_map(data):
|
8 |
fig = px.choropleth(locations=data['State'], locationmode="USA-states", scope="usa")
|
9 |
+
grouped_data = data.groupby('State')
|
10 |
+
for state, group in grouped_data:
|
11 |
+
top_corp = group.nlargest(1, 'Revenue')
|
12 |
+
text_label = f"{top_corp['Corporation'].iloc[0]} - ${top_corp['Revenue'].iloc[0]}B"
|
13 |
+
lon = group['Longitude'].mean()
|
14 |
+
lat = group['Latitude'].mean()
|
15 |
fig.add_trace(go.Scattergeo(
|
16 |
+
lon=[lon],
|
17 |
+
lat=[lat],
|
18 |
+
text=text_label,
|
19 |
mode='text',
|
20 |
))
|
21 |
+
fig.update_layout(title="Top Corporation by State in the United States")
|
22 |
return fig
|
23 |
|
24 |
+
st.title('Top Corporation by State in the United States ๐ข')
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# Upload CSV
|
27 |
uploaded_files = st.file_uploader("Choose a CSV file", accept_multiple_files=True, type="csv")
|
|
|
29 |
# Display map button
|
30 |
display_map_button = st.button('Display Map of CSV Data ๐บ๏ธ')
|
31 |
|
32 |
+
if display_map_button:
|
|
|
|
|
|
|
33 |
if uploaded_files:
|
34 |
for uploaded_file in uploaded_files:
|
35 |
data = pd.read_csv(uploaded_file)
|
36 |
+
st.write(f"Map for {uploaded_file.name} ๐")
|
37 |
+
st.plotly_chart(plot_map(data))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
else:
|
39 |
+
st.write("Please upload a CSV file to proceed. ๐")
|