Spaces:
Sleeping
Sleeping
code changed
Browse files
app.py
CHANGED
|
@@ -2,55 +2,55 @@ import streamlit as st
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
|
| 5 |
-
# Set the FastAPI base URL
|
| 6 |
-
API_URL="https://tharu22-world-population.hf.space"
|
| 7 |
-
# Streamlit app title
|
| 8 |
-
st.title("World Population ")
|
| 9 |
-
|
| 10 |
-
# Load the data
|
| 11 |
-
file_path ="world_population.csv"
|
| 12 |
-
df = pd.read_csv(file_path)
|
| 13 |
-
|
| 14 |
-
# Streamlit app
|
| 15 |
-
def main():
|
| 16 |
-
st.title("World Population Explorer 🌍")
|
| 17 |
-
|
| 18 |
-
# Sidebar for continent selection
|
| 19 |
-
st.sidebar.header("Select a Continent")
|
| 20 |
-
continent_list = df['Continent'].unique().tolist()
|
| 21 |
-
selected_continent = st.sidebar.selectbox("Choose a continent", continent_list)
|
| 22 |
-
|
| 23 |
-
if selected_continent:
|
| 24 |
-
# Filter data for the selected continent
|
| 25 |
-
continent_data = df[df['Continent'] == selected_continent]
|
| 26 |
-
|
| 27 |
-
# Calculate statistics
|
| 28 |
-
max_population = continent_data['Population'].max()
|
| 29 |
-
min_population = continent_data['Population'].min()
|
| 30 |
-
max_country = continent_data.loc[continent_data['Population'].idxmax()]['Country']
|
| 31 |
-
min_country = continent_data.loc[continent_data['Population'].idxmin()]['Country']
|
| 32 |
-
average_population = continent_data['Population'].mean()
|
| 33 |
-
total_area = continent_data['Area'].sum()
|
| 34 |
-
total_population = continent_data['Population'].sum()
|
| 35 |
-
continent_density = total_population / total_area
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.subheader("Average Population")
|
| 47 |
-
st.write(f"The average population in {selected_continent} is **{average_population:.2f}**.")
|
| 48 |
-
|
| 49 |
-
st.subheader("Total Area")
|
| 50 |
-
st.write(f"The total area of {selected_continent} is **{total_area}** square kilometers.")
|
| 51 |
-
|
| 52 |
-
st.subheader("Total Population")
|
| 53 |
-
st.write(f"The total population of {selected_continent} is **{total_population}**.")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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://tharu22-world-population.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']}")
|