molehh's picture
frontend deploy
1a3780c
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.")