File size: 2,178 Bytes
c904db4
 
 
 
 
 
 
 
 
f2c6a57
c904db4
 
f2c6a57
c904db4
 
f2c6a57
c904db4
f2c6a57
c904db4
 
f2c6a57
c904db4
f2c6a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import streamlit as st
import requests

# FastAPI backend URL
FASTAPI_URL = "http://127.0.0.1:8000"

# Streamlit app
st.title("World Population Statistics")

# Fetch all continents
response = requests.get(f"{FASTAPI_URL}/continents/")
if response.status_code == 200:
    continents = response.json().get("continents", [])
else:
    st.error("Failed to fetch continents")
    continents = []

# Dropdown to select a continent
selected_continent = st.selectbox("Select a Continent", continents)

if selected_continent:
    # Fetch statistics for the selected continent
    response = requests.get(f"{FASTAPI_URL}/continents/{selected_continent}/")
    
    if response.status_code == 200:
        continent_data = response.json()
        st.subheader(f"Statistics for {selected_continent}")

        # Check if 'statistics' is in response
        statistics = continent_data.get("statistics", {})
        if statistics:
            st.write(f"**Total Countries:** {statistics.get('Total_Countries', 'N/A')}")
            st.write(f"**Total Population:** {statistics.get('Total_Population', 'N/A')}")
            st.write(f"**Average Population:** {statistics.get('Average_Population', 'N/A')}")
            st.write(f"**Total Area:** {statistics.get('Total_Area', 'N/A')}")
            st.write(f"**Max Population:** {statistics.get('max_population', 'N/A')}")
            st.write(f"**Min Population:** {statistics.get('min_population', 'N/A')}")
            st.write(f"**Country with Max Population:** {statistics.get('Country_Max_Population', 'N/A')}")
            st.write(f"**Country with Min Population:** {statistics.get('Country_Min_Population', 'N/A')}")
            st.write(f"**Population Density:** {statistics.get('Population_Density', 'N/A')}")
        else:
            st.error("No statistics found for this continent.")
    else:
        st.error(f"Failed to fetch statistics for {selected_continent}")

# Option to view a specific statistic
if statistics:
    stat_options = list(statistics.keys())
    selected_stat = st.selectbox("Select a specific statistic to view", stat_options)
    st.write(f"**{selected_stat}:** {statistics.get(selected_stat, 'N/A')}")