Kabila22 commited on
Commit
ef27508
·
1 Parent(s): fb5bbb8

First commit

Browse files
Files changed (1) hide show
  1. app.py +50 -47
app.py CHANGED
@@ -2,52 +2,55 @@ import streamlit as st
2
  import requests
3
  import pandas as pd
4
 
5
- # Function to get data from FastAPI
6
- def get_continent_population(continent_name):
7
- url = "https://kabila22-backend.hf.space"
8
- response = requests.get(url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  if response.status_code == 200:
10
- return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  else:
12
- st.error("Failed to retrieve data")
13
- return None
14
-
15
- # List of continents
16
- continents = ["Africa", "Asia", "Europe", "North America", "Oceania", "South America"]
17
-
18
- # Create Streamlit dropdown box
19
- st.title("World Population by Continent")
20
- selected_continent = st.selectbox("Select a continent:", continents)
21
-
22
- # Get data for selected continent
23
- continent_data = get_continent_population(selected_continent)
24
-
25
- if continent_data:
26
- # Display population data in a table
27
- st.subheader(f"Population Data for {selected_continent}")
28
-
29
- # Ensure all values are present and convert them to numeric if necessary
30
- population_data = {
31
- "Data": ["Total Population", "Total Area", "Population Density", "Max Population", "Min Population", "Avg Population"],
32
- "Result": [
33
- continent_data.get("total_population", 0),
34
- continent_data.get("total_area_km2", 0),
35
- continent_data.get("continent_population_density", 0),
36
- continent_data["max_population"].get("population", 0),
37
- continent_data["min_population"].get("population", 0),
38
- continent_data.get("avg_population", 0)
39
- ],
40
- "Country": [
41
- "",
42
- "",
43
- "",
44
- continent_data["max_population"]["country"],
45
- continent_data["min_population"]["country"],
46
- ""
47
- ]
48
- }
49
-
50
- df = pd.DataFrame(population_data)
51
-
52
- # Display the data in a table
53
- st.table(df)
 
2
  import requests
3
  import pandas as pd
4
 
5
+
6
+ # Redirect /docs to the main page
7
+ if st.experimental_get_query_params().get("page", [""])[0] == "docs":
8
+ st.error("Page not found. Redirecting to the main dashboard...")
9
+ st.experimental_set_query_params(page="")
10
+ st.experimental_rerun()
11
+
12
+ # Set the FastAPI base URL
13
+ API_URL = "https://kabila22-backend.hf.space"
14
+
15
+ # Streamlit app title
16
+ st.title("⭐World Population Dashboard")
17
+
18
+ # Sidebar filter for continents
19
+ st.sidebar.header("Filter")
20
+ selected_continent = st.sidebar.selectbox(
21
+ "Select the Continent:",
22
+ ['Asia', 'Africa', 'North America', 'South America', 'Europe', 'Oceania']
23
+ )
24
+
25
+ # Fetch data from the FastAPI endpoint
26
+ if st.sidebar.button("Get Data"):
27
+ # Call FastAPI to get continent data
28
+ response = requests.get(f"{API_URL}/continent/{selected_continent}")
29
+
30
  if response.status_code == 200:
31
+ data = response.json()
32
+ st.write(data)
33
+
34
+ # Display the continent information
35
+ st.header(f"Data of {data['continent']}")
36
+ st.metric("Total Population", f"{data['total_population']:,}")
37
+ st.metric("Total Area (sq km)", f"{data['total_area']:,}")
38
+ st.metric("Population Density", f"{data['continent_population_density']:.2f}")
39
+ st.subheader("Population Highlights")
40
+ st.write(
41
+ f"Max Population :{data['max_population']['country']} "
42
+ f"({data['max_population']['population']:,})"
43
+ )
44
+ # Country with min population
45
+ st.write(
46
+ f"Min Population:{data['min_population']['country']} "
47
+ f"({data['min_population']['population']:,})"
48
+ )
49
+
50
+ # countries_data=data['countries']
51
+ # country_df = pd.DataFrame(countries_data)
52
+ # st.subheader(f"Population of Countries in {data['continent']}")
53
+ # st.bar_chart(country_df.set_index("Country"))z
54
  else:
55
+ # Handle errors
56
+ st.error(f"Error: {response.json()['detail']}")