tharu22 commited on
Commit
1ae8791
Β·
1 Parent(s): a37e5a2

changed con

Browse files
Files changed (1) hide show
  1. app.py +40 -46
app.py CHANGED
@@ -1,50 +1,44 @@
1
  import streamlit as st
2
- import requests
3
  import pandas as pd
4
 
5
- # Redirect /docs to the main page
6
- # if st.query_params.get("page", "") == "docs":
7
- # st.error("Page not found. Redirecting to the main dashboard...")
8
- # st.query_params["page"] = ""
9
- # st.rerun()
10
-
11
- # Set the FastAPI base URL
12
- API_URL = "https://tharu22-world-population.hf.space"
13
-
14
- # Streamlit app title
15
- st.title("World Population Dashboard")
16
-
17
- # Sidebar filter for continents
18
- st.sidebar.header("Filter")
19
- selected_continent = st.sidebar.selectbox(
20
- "Select the Continent:",
21
- ['Asia', 'Africa', 'North America', 'South America', 'Europe', 'Oceania']
22
- )
23
-
24
- # Fetch data from the FastAPI endpoint
25
- if st.sidebar.button("Get Data"):
26
- # Call FastAPI to get continent data
27
- response = requests.get(f"{API_URL}/continent/{selected_continent}")
 
 
 
 
 
 
 
 
 
28
 
29
- if response.status_code == 200:
30
- data = response.json()
31
- st.write(data)
32
-
33
- # Display the continent information
34
- st.header(f"Data of {data['continent']}")
35
- st.metric("Total Population", f"{data['total_population']:,}")
36
- st.metric("Total Area (sq km)", f"{data['total_area']:,}")
37
- st.metric("Population Density", f"{data['continent_population_density']:.2f}")
38
- st.subheader("Population Highlights")
39
- st.write(
40
- f"Max Population :{data['max_population']['country']} "
41
- f"({data['max_population']['population']:,})"
42
- )
43
- # Country with min population
44
- st.write(
45
- f"Min Population:{data['min_population']['country']} "
46
- f"({data['min_population']['population']:,})"
47
- )
48
- else:
49
- # Handle errors
50
- st.error(f"Error: {response.json()['detail']}")
 
1
  import streamlit as st
 
2
  import pandas as pd
3
 
4
+ # File path (modify as needed)
5
+ file_path = "world_population.csv"
6
+
7
+ # Load dataset
8
+ df = pd.read_csv(file_path)
9
+
10
+ # Streamlit UI
11
+ st.title("🌍 World Population Dashboard")
12
+
13
+ # Sidebar filter
14
+ st.sidebar.header("Select a Continent")
15
+ continents = df["Continent"].unique()
16
+ selected_continent = st.sidebar.selectbox("Choose Continent:", continents)
17
+
18
+ # Processing Data
19
+ max_population = df.groupby("Continent")["Population"].max()
20
+ min_population = df.groupby("Continent")["Population"].min()
21
+ max_country = df.loc[df.groupby("Continent")["Population"].idxmax(), ["Continent", "Country"]]
22
+ min_country = df.loc[df.groupby("Continent")["Population"].idxmin(), ["Continent", "Country"]]
23
+ average_population = df.groupby("Continent")["Population"].mean()
24
+ total_area = df.groupby("Continent")["Area"].sum()
25
+ total_population = df.groupby("Continent")["Population"].sum()
26
+ population_density = total_population / total_area
27
+
28
+ # Display results for selected continent
29
+ if selected_continent:
30
+ st.header(f"🌎 {selected_continent} Statistics")
31
+
32
+ # Population Metrics
33
+ st.metric("πŸ“ˆ Maximum Population", f"{max_population[selected_continent]:,} ({max_country[max_country['Continent'] == selected_continent]['Country'].values[0]})")
34
+ st.metric("πŸ“‰ Minimum Population", f"{min_population[selected_continent]:,} ({min_country[min_country['Continent'] == selected_continent]['Country'].values[0]})")
35
+ st.metric("πŸ“Š Average Population", f"{average_population[selected_continent]:,.0f}")
36
 
37
+ # Other Metrics
38
+ st.metric("πŸ“ Total Area", f"{total_area[selected_continent]:,} sq km")
39
+ st.metric("πŸ‘₯ Total Population", f"{total_population[selected_continent]:,}")
40
+ st.metric("🏠 Population Density", f"{population_density[selected_continent]:.2f} people/sq km")
41
+
42
+ # Footer
43
+ st.markdown("πŸš€ Built with Streamlit")
44
+