File size: 3,389 Bytes
6ccf7f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87


import streamlit as st
import requests

# Set the base URL for the FastAPI app
BASE_URL = "https://molehh-data-insights-backend.hf.space"  # Update this URL to match your running backend

# Title for the Streamlit app
st.title("World Population App")

# Home Page
st.header("Welcome")
try:
    response = requests.get(f"{BASE_URL}/")
    if response.status_code == 200:
        st.subheader(response.json()["message"])
    else:
        st.error(f"Failed to connect to the backend. Status Code: {response.status_code}")
except requests.exceptions.RequestException as e:
    st.error(f"An error occurred while connecting to the backend: {e}")

# Display Continents
st.header("Continents")
if st.button("Get Continents"):
    try:
        response = requests.get(f"{BASE_URL}/continent")
        if response.status_code == 200:
            continents = response.json()["continents"]
            st.write("List of Continents:")
            st.write(continents)
        else:
            st.error(f"Failed to fetch continents. Status Code: {response.status_code}")
    except requests.exceptions.RequestException as e:
        st.error(f"An error occurred while connecting to the backend: {e}")

# Display Countries
st.header("Countries")
if st.button("Get Countries"):
    try:
        response = requests.get(f"{BASE_URL}/country")
        if response.status_code == 200:
            countries = response.json()["country"]
            st.write("List of Countries:")
            st.write(countries)
        else:
            st.error(f"Failed to fetch countries. Status Code: {response.status_code}")
    except requests.exceptions.RequestException as e:
        st.error(f"An error occurred while connecting to the backend: {e}")

# Maximum Population by Continent
st.header("Maximum Population")
continent_name_max = st.text_input("Enter a continent for max population:")
if st.button("Get Max Population"):
    if continent_name_max:
        try:
            response = requests.get(f"{BASE_URL}/max_population/{continent_name_max}")
            if response.status_code == 200:
                max_population = response.json()["max_population"]
                st.write(f"Max Population Details: {max_population}")
            else:
                st.error(f"Failed to fetch max population for {continent_name_max}. Status Code: {response.status_code}")
        except requests.exceptions.RequestException as e:
            st.error(f"An error occurred while connecting to the backend: {e}")
    else:
        st.warning("Please enter a continent name.")

# Minimum Population by Continent
st.header("Minimum Population")
continent_name_min = st.text_input("Enter a continent for min population:")
if st.button("Get Min Population"):
    if continent_name_min:
        try:
            response = requests.get(f"{BASE_URL}/min_population/{continent_name_min}")
            if response.status_code == 200:
                min_population = response.json()["min_population"]
                st.write(f"Min Population Details: {min_population}")
            else:
                st.error(f"Failed to fetch min population for {continent_name_min}. Status Code: {response.status_code}")
        except requests.exceptions.RequestException as e:
            st.error(f"An error occurred while connecting to the backend: {e}")
    else:
        st.warning("Please enter a continent name.")