frontend / app.py
logeswari's picture
modified app.py
b0aa141
import streamlit as st
import requests
import pandas as pd
# Redirect /docs to the main page
if st.experimental_get_query_params().get("page", [""])[0] == "docs":
st.error("Page not found. Redirecting to the main dashboard...")
st.experimental_set_query_params(page="")
st.experimental_rerun()
# Set the FastAPI base URL
API_URL = "https://logeswari-backapp.hf.space"
# Streamlit app title
st.title("⭐World Population Dashboard")
# Sidebar filter for continents
st.sidebar.header("Filter")
selected_continent = st.sidebar.selectbox(
"Select the Continent:",
['Asia', 'Africa', 'North America', 'South America', 'Europe', 'Oceania']
)
# Fetch data from the FastAPI endpoint
if st.sidebar.button("Get Data"):
# Call FastAPI to get continent data
response = requests.get(f"{API_URL}/continent/{selected_continent}")
if response.status_code == 200:
data = response.json()
st.write(data)
# Display the continent information
st.header(f"Data of {data['continent']}")
st.metric("Total Population", f"{data['total_population']:,}")
st.metric("Total Area (sq km)", f"{data['total_area']:,}")
st.metric("Population Density", f"{data['continent_population_density']:.2f}")
st.subheader("Population Highlights")
st.write(
f"Max Population :{data['max_population']['country']} "
f"({data['max_population']['population']:,})"
)
# Country with min population
st.write(
f"Min Population:{data['min_population']['country']} "
f"({data['min_population']['population']:,})"
)
# countries_data=data['countries']
# country_df = pd.DataFrame(countries_data)
# st.subheader(f"Population of Countries in {data['continent']}")
# st.bar_chart(country_df.set_index("Country"))z
else:
# Handle errors
st.error(f"Error: {response.json()['detail']}")