Spaces:
Sleeping
Sleeping
Commit
·
f2c6a57
1
Parent(s):
fa86f82
modified code
Browse files- Frontend/app.py +33 -64
Frontend/app.py
CHANGED
|
@@ -1,36 +1,3 @@
|
|
| 1 |
-
# import streamlit as st
|
| 2 |
-
# import requests
|
| 3 |
-
|
| 4 |
-
# BASE_URL = "http://127.0.0.1:8000" # Update with your FastAPI server URL
|
| 5 |
-
|
| 6 |
-
# st.title("World Population")
|
| 7 |
-
|
| 8 |
-
# # Fetch all continents
|
| 9 |
-
# response = requests.get(f"{BASE_URL}/continents/")
|
| 10 |
-
# if response.status_code == 200:
|
| 11 |
-
# continents = response.json()["continents"]
|
| 12 |
-
# else:
|
| 13 |
-
# st.error("Failed to fetch continents")
|
| 14 |
-
# continents = []
|
| 15 |
-
|
| 16 |
-
# selected_continent = st.selectbox("Select a continent:", continents)
|
| 17 |
-
|
| 18 |
-
# if selected_continent:
|
| 19 |
-
# stat = st.text_input("Enter a statistic to fetch (optional):")
|
| 20 |
-
|
| 21 |
-
# url = f"{BASE_URL}/continents/{selected_continent}/"
|
| 22 |
-
# if stat:
|
| 23 |
-
# url += f"?stat={stat}"
|
| 24 |
-
|
| 25 |
-
# response = requests.get(url)
|
| 26 |
-
# if response.status_code == 200:
|
| 27 |
-
# data = response.json()
|
| 28 |
-
# st.json(data)
|
| 29 |
-
# else:
|
| 30 |
-
# st.error("Failed to fetch data")
|
| 31 |
-
|
| 32 |
-
#DEEPSEEK
|
| 33 |
-
|
| 34 |
import streamlit as st
|
| 35 |
import requests
|
| 36 |
|
|
@@ -38,44 +5,46 @@ import requests
|
|
| 38 |
FASTAPI_URL = "http://127.0.0.1:8000"
|
| 39 |
|
| 40 |
# Streamlit app
|
| 41 |
-
# def main():
|
| 42 |
st.title("World Population Statistics")
|
| 43 |
|
| 44 |
-
|
| 45 |
response = requests.get(f"{FASTAPI_URL}/continents/")
|
| 46 |
if response.status_code == 200:
|
| 47 |
-
continents = response.json()
|
| 48 |
else:
|
| 49 |
st.error("Failed to fetch continents")
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
selected_continent = st.selectbox("Select a Continent", continents)
|
| 54 |
|
|
|
|
| 55 |
# Fetch statistics for the selected continent
|
| 56 |
-
response = requests.get(f"{FASTAPI_URL}/continents/{selected_continent}/")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
st.write(f"**Total Countries:** {
|
| 66 |
-
st.write(f"**Total Population:** {
|
| 67 |
-
st.write(f"**Average Population:** {
|
| 68 |
-
st.write(f"**Total Area:** {
|
| 69 |
-
st.write(f"**Max Population:** {
|
| 70 |
-
st.write(f"**Min Population:** {
|
| 71 |
-
st.write(f"**Country with Max Population:** {
|
| 72 |
-
st.write(f"**Country with Min Population:** {
|
| 73 |
-
st.write(f"**Population Density:** {
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
|
|
|
|
| 5 |
FASTAPI_URL = "http://127.0.0.1:8000"
|
| 6 |
|
| 7 |
# Streamlit app
|
|
|
|
| 8 |
st.title("World Population Statistics")
|
| 9 |
|
| 10 |
+
# Fetch all continents
|
| 11 |
response = requests.get(f"{FASTAPI_URL}/continents/")
|
| 12 |
if response.status_code == 200:
|
| 13 |
+
continents = response.json().get("continents", [])
|
| 14 |
else:
|
| 15 |
st.error("Failed to fetch continents")
|
| 16 |
+
continents = []
|
| 17 |
|
| 18 |
+
# Dropdown to select a continent
|
| 19 |
selected_continent = st.selectbox("Select a Continent", continents)
|
| 20 |
|
| 21 |
+
if selected_continent:
|
| 22 |
# Fetch statistics for the selected continent
|
| 23 |
+
response = requests.get(f"{FASTAPI_URL}/continents/{selected_continent}/")
|
| 24 |
+
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
continent_data = response.json()
|
| 27 |
+
st.subheader(f"Statistics for {selected_continent}")
|
| 28 |
+
|
| 29 |
+
# Check if 'statistics' is in response
|
| 30 |
+
statistics = continent_data.get("statistics", {})
|
| 31 |
+
if statistics:
|
| 32 |
+
st.write(f"**Total Countries:** {statistics.get('Total_Countries', 'N/A')}")
|
| 33 |
+
st.write(f"**Total Population:** {statistics.get('Total_Population', 'N/A')}")
|
| 34 |
+
st.write(f"**Average Population:** {statistics.get('Average_Population', 'N/A')}")
|
| 35 |
+
st.write(f"**Total Area:** {statistics.get('Total_Area', 'N/A')}")
|
| 36 |
+
st.write(f"**Max Population:** {statistics.get('max_population', 'N/A')}")
|
| 37 |
+
st.write(f"**Min Population:** {statistics.get('min_population', 'N/A')}")
|
| 38 |
+
st.write(f"**Country with Max Population:** {statistics.get('Country_Max_Population', 'N/A')}")
|
| 39 |
+
st.write(f"**Country with Min Population:** {statistics.get('Country_Min_Population', 'N/A')}")
|
| 40 |
+
st.write(f"**Population Density:** {statistics.get('Population_Density', 'N/A')}")
|
| 41 |
+
else:
|
| 42 |
+
st.error("No statistics found for this continent.")
|
| 43 |
+
else:
|
| 44 |
+
st.error(f"Failed to fetch statistics for {selected_continent}")
|
| 45 |
+
|
| 46 |
+
# Option to view a specific statistic
|
| 47 |
+
if statistics:
|
| 48 |
+
stat_options = list(statistics.keys())
|
| 49 |
+
selected_stat = st.selectbox("Select a specific statistic to view", stat_options)
|
| 50 |
+
st.write(f"**{selected_stat}:** {statistics.get(selected_stat, 'N/A')}")
|