Spaces:
Sleeping
Sleeping
Commit
·
676cace
1
Parent(s):
c904db4
changed folder structure
Browse files- population_streamlit.py → Frontend/app.py +0 -0
- backend/__init__.py +0 -0
- backend/__pycache__/__init__.cpython-313.pyc +0 -0
- backend/__pycache__/continents.cpython-313.pyc +0 -0
- backend/__pycache__/logger.cpython-313.pyc +0 -0
- backend/__pycache__/main.cpython-313.pyc +0 -0
- backend/__pycache__/population_pandas.cpython-313.pyc +0 -0
- backend/backend.log +145 -0
- world_population.csv → backend/data/world_population.csv +0 -0
- backend/logger.py +16 -0
- backend/main.py +16 -0
- backend/models/__pycache__/population_models.cpython-313.pyc +0 -0
- backend/models/population_models.py +28 -0
- population_pandas.py → backend/population_pandas.py +20 -26
- backend/routes/__pycache__/continents.cpython-313.pyc +0 -0
- backend/routes/continents.py +34 -0
population_streamlit.py → Frontend/app.py
RENAMED
|
File without changes
|
backend/__init__.py
ADDED
|
File without changes
|
backend/__pycache__/__init__.cpython-313.pyc
ADDED
|
Binary file (140 Bytes). View file
|
|
|
backend/__pycache__/continents.cpython-313.pyc
ADDED
|
Binary file (1.86 kB). View file
|
|
|
backend/__pycache__/logger.cpython-313.pyc
ADDED
|
Binary file (671 Bytes). View file
|
|
|
backend/__pycache__/main.cpython-313.pyc
ADDED
|
Binary file (793 Bytes). View file
|
|
|
backend/__pycache__/population_pandas.cpython-313.pyc
ADDED
|
Binary file (2.54 kB). View file
|
|
|
backend/backend.log
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
2025-02-07 18:42:30,039 - ERROR - Error loading CSV file: [Errno 2] No such file or directory: 'Backend/Data/world_population.csv'
|
| 2 |
+
2025-02-07 18:44:09,012 - ERROR - Error loading CSV file: [Errno 2] No such file or directory: 'backend/Data/world_population.csv'
|
| 3 |
+
2025-02-07 18:46:51,355 - INFO - CSV file loaded successfully.
|
| 4 |
+
2025-02-07 18:46:51,441 - INFO - Data processing completed.
|
| 5 |
+
2025-02-07 18:46:51,500 - INFO - Starting FastAPI application...
|
| 6 |
+
2025-02-07 18:46:51,618 - INFO - Will watch for changes in these directories: ['C:\\chitu\\population_api\\backend']
|
| 7 |
+
2025-02-07 18:46:51,640 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
| 8 |
+
2025-02-07 18:46:51,660 - INFO - Started reloader process [6252] using WatchFiles
|
| 9 |
+
2025-02-07 18:46:56,484 - INFO - 1 change detected
|
| 10 |
+
2025-02-07 18:46:59,287 - INFO - CSV file loaded successfully.
|
| 11 |
+
2025-02-07 18:46:59,337 - INFO - Data processing completed.
|
| 12 |
+
2025-02-07 18:46:59,385 - INFO - Starting FastAPI application...
|
| 13 |
+
2025-02-07 18:46:59,388 - INFO - Started server process [10616]
|
| 14 |
+
2025-02-07 18:46:59,412 - INFO - Waiting for application startup.
|
| 15 |
+
2025-02-07 18:46:59,419 - INFO - Application startup complete.
|
| 16 |
+
2025-02-07 18:47:28,558 - INFO - Home endpoint accessed
|
| 17 |
+
2025-02-07 18:48:41,054 - INFO - 1 change detected
|
| 18 |
+
2025-02-07 18:48:41,431 - INFO - 1 change detected
|
| 19 |
+
2025-02-07 18:48:41,821 - INFO - 1 change de
|
| 20 |
+
2025-02-07 18:59:13,483 - INFO - CSV file loaded successfully.
|
| 21 |
+
2025-02-07 18:59:13,532 - INFO - Data processing completed.
|
| 22 |
+
2025-02-07 18:59:13,582 - INFO - Starting FastAPI application...
|
| 23 |
+
2025-02-07 18:59:14,078 - INFO - Will watch for changes in these directories: ['C:\\chitu\\population_api\\backend']
|
| 24 |
+
2025-02-07 18:59:14,100 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
| 25 |
+
2025-02-07 18:59:14,111 - INFO - Started reloader process [12852] using WatchFiles
|
| 26 |
+
2025-02-07 18:59:20,971 - INFO - CSV file loaded successfully.
|
| 27 |
+
2025-02-07 18:59:21,021 - INFO - Data processing completed.
|
| 28 |
+
2025-02-07 18:59:21,067 - INFO - Starting FastAPI application...
|
| 29 |
+
2025-02-07 18:59:21,070 - INFO - Started server process [2820]
|
| 30 |
+
2025-02-07 18:59:21,090 - INFO - Waiting for application startup.
|
| 31 |
+
2025-02-07 18:59:21,100 - INFO - Application startup complete.
|
| 32 |
+
2025-02-07 18:59:38,200 - INFO - Fetching list of continents.
|
| 33 |
+
2025-02-07 18:59:38,203 - INFO - Fetching all continents.
|
| 34 |
+
2025-02-07 18:59:45,760 - INFO - 1 change detected
|
| 35 |
+
2025-02-07 18:59:46,134 - INFO - 1 change detected
|
| 36 |
+
2025-02-07 18:59:46,569 - INFO - 1 change detected
|
| 37 |
+
2025-02-07 18:59:46,987 - INFO - 1 change detected
|
| 38 |
+
2025-02-07 18:59:47,347 - INFO - 1 change detected
|
| 39 |
+
2025-02-07 18:59:47,710 - INFO - 1 change detected
|
| 40 |
+
2025-02-07 18:59:48,073 - INFO - 1 change detected
|
| 41 |
+
2025-02-07 18:59:48,432 - INFO - 1 change detected
|
| 42 |
+
2025-02-07 18:59:48,790 - INFO - 1 change detected
|
| 43 |
+
2025-02-07 18:59:49,149 - INFO - 1 change detected
|
| 44 |
+
2025-02-07 18:59:49,509 - INFO - 1 change detected
|
| 45 |
+
2025-02-07 18:59:49,867 - INFO - 1 change detected
|
| 46 |
+
2025-02-07 18:59:50,228 - INFO - 1 change detected
|
| 47 |
+
2025-02-07 18:59:50,585 - INFO - 1 change detected
|
| 48 |
+
2025-02-07 18:59:50,944 - INFO - 1 change detected
|
| 49 |
+
2025-02-07 18:59:51,304 - INFO - 1 change detected
|
| 50 |
+
2025-02-07 18:59:51,661 - INFO - 1 change detected
|
| 51 |
+
2025-02-07 18:59:52,020 - INFO - 1 change detected
|
| 52 |
+
2025-02-07 18:59:52,378 - INFO - 1 change detected
|
| 53 |
+
2025-02-07 18:59:52,737 - INFO - 1 change detected
|
| 54 |
+
2025-02-07 18:59:53,100 - INFO - 1 change detected
|
| 55 |
+
2025-02-07 18:59:53,455 - INFO - 1 change detected
|
| 56 |
+
2025-02-07 18:59:53,868 - INFO - 1 change detected
|
| 57 |
+
2025-02-07 18:59:54,226 - INFO - 1 change detected
|
| 58 |
+
2025-02-07 18:59:54,589 - INFO - 1 change detected
|
| 59 |
+
2025-02-07 18:59:54,947 - INFO - 1 change detected
|
| 60 |
+
2025-02-07 18:59:55,306 - INFO - 1 change detected
|
| 61 |
+
2025-02-07 18:59:55,665 - INFO - 1 change detected
|
| 62 |
+
2025-02-07 18:59:56,019 - INFO - 1 change detected
|
| 63 |
+
2025-02-07 18:59:56,634 - INFO - 1 change detected
|
| 64 |
+
2025-02-07 18:59:57,022 - INFO - 1 change detected
|
| 65 |
+
2025-02-07 18:59:57,430 - INFO - 1 change detected
|
| 66 |
+
2025-02-07 18:59:57,847 - INFO - 1 change detected
|
| 67 |
+
2025-02-07 18:59:58,252 - INFO - 1 change detected
|
| 68 |
+
2025-02-07 18:59:58,655 - INFO - 1 change detected
|
| 69 |
+
2025-02-07 18:59:59,074 - INFO - 1 change detected
|
| 70 |
+
2025-02-07 18:59:59,494 - INFO - 1 change detected
|
| 71 |
+
2025-02-07 18:59:59,897 - INFO - 1 change detected
|
| 72 |
+
2025-02-07 19:00:00,304 - INFO - 1 change detected
|
| 73 |
+
2025-02-07 19:00:00,769 - INFO - 1 change detected
|
| 74 |
+
2025-02-07 19:00:01,135 - INFO - 1 change detected
|
| 75 |
+
2025-02-07 19:00:01,547 - INFO - 1 change detected
|
| 76 |
+
2025-02-07 19:00:01,950 - INFO - 1 change detected
|
| 77 |
+
2025-02-07 19:00:02,366 - INFO - 1 change detected
|
| 78 |
+
2025-02-07 19:00:02,771 - INFO - 1 change detected
|
| 79 |
+
2025-02-07 19:00:03,180 - INFO - 1 change detected
|
| 80 |
+
2025-02-07 19:00:03,585 - INFO - 1 change detected
|
| 81 |
+
2025-02-07 19:00:04,094 - INFO - 1 change detected
|
| 82 |
+
2025-02-07 19:00:04,457 - INFO - 1 change detected
|
| 83 |
+
2025-02-07 19:00:04,865 - INFO - 1 change detected
|
| 84 |
+
2025-02-07 19:00:05,269 - INFO - 1 change detected
|
| 85 |
+
2025-02-07 19:00:05,675 - INFO - 1 change detected
|
| 86 |
+
2025-02-07 19:00:06,135 - INFO - 1 change detected
|
| 87 |
+
2025-02-07 19:00:06,497 - INFO - 1 change detected
|
| 88 |
+
2025-02-07 19:00:06,903 - INFO - 1 change detected
|
| 89 |
+
2025-02-07 19:00:07,315 - INFO - 1 change detected
|
| 90 |
+
2025-02-07 19:00:07,723 - INFO - 1 change detected
|
| 91 |
+
2025-02-07 19:00:08,137 - INFO - 1 change detected
|
| 92 |
+
2025-02-07 19:00:08,546 - INFO - 1 change detected
|
| 93 |
+
2025-02-07 19:00:08,949 - INFO - 1 change detected
|
| 94 |
+
2025-02-07 19:00:09,357 - INFO - 1 change detected
|
| 95 |
+
2025-02-07 19:00:09,813 - INFO - 1 change detected
|
| 96 |
+
2025-02-07 19:00:10,270 - INFO - 1 change detected
|
| 97 |
+
2025-02-07 19:00:10,675 - INFO - 1 change detected
|
| 98 |
+
2025-02-07 19:00:11,036 - INFO - 1 change detected
|
| 99 |
+
2025-02-07 19:00:11,452 - INFO - 1 change detected
|
| 100 |
+
2025-02-07 19:00:11,466 - INFO - Shutting down
|
| 101 |
+
2025-02-07 19:00:11,573 - INFO - Waiting for application shutdown.
|
| 102 |
+
2025-02-07 19:00:11,579 - INFO - Application shutdown complete.
|
| 103 |
+
2025-02-07 19:00:11,584 - INFO - Finished server process [2820]
|
| 104 |
+
2025-02-07 19:00:12,039 - INFO - Stopping reloader process [12852]
|
| 105 |
+
2025-02-07 19:09:32,030 - INFO - CSV file loaded successfully.
|
| 106 |
+
2025-02-07 19:09:32,078 - INFO - Data processing completed.
|
| 107 |
+
2025-02-07 19:10:01,801 - INFO - CSV file loaded successfully.
|
| 108 |
+
2025-02-07 19:10:01,843 - INFO - Data processing completed.
|
| 109 |
+
2025-02-07 19:10:25,356 - INFO - CSV file loaded successfully.
|
| 110 |
+
2025-02-07 19:10:25,421 - INFO - Data processing completed.
|
| 111 |
+
2025-02-07 19:10:25,514 - INFO - Starting FastAPI application...
|
| 112 |
+
2025-02-07 19:10:25,673 - INFO - Will watch for changes in these directories: ['C:\\chitu\\population_api\\backend']
|
| 113 |
+
2025-02-07 19:10:25,692 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
| 114 |
+
2025-02-07 19:10:25,708 - INFO - Started reloader process [9352] using WatchFiles
|
| 115 |
+
2025-02-07 19:10:32,520 - INFO - CSV file loaded successfully.
|
| 116 |
+
2025-02-07 19:10:32,593 - INFO - Data processing completed.
|
| 117 |
+
2025-02-07 19:10:32,665 - INFO - Starting FastAPI application...
|
| 118 |
+
2025-02-07 19:10:32,667 - INFO - Started server process [3260]
|
| 119 |
+
2025-02-07 19:10:32,683 - INFO - Waiting for application startup.
|
| 120 |
+
2025-02-07 19:10:32,690 - INFO - Application startup complete.
|
| 121 |
+
2025-02-07 19:12:15,266 - INFO - Fetching all continents
|
| 122 |
+
2025-02-07 19:12:15,268 - INFO - Fetching all continents.
|
| 123 |
+
2025-02-07 19:12:31,421 - INFO - Fetching all continents
|
| 124 |
+
2025-02-07 19:12:31,431 - INFO - Fetching all continents.
|
| 125 |
+
2025-02-07 19:14:56,183 - INFO - 1 change detected
|
| 126 |
+
2025-02-07 19:15:06,615 - INFO - 3 changes detected
|
| 127 |
+
2025-02-07 19:15:29,952 - INFO - 1 change detected
|
| 128 |
+
2025-02-07 19:15:29,965 - WARNING - WatchFiles detected changes in 'population_pandas.py'. Reloading...
|
| 129 |
+
2025-02-07 19:15:30,127 - INFO - Shutting down
|
| 130 |
+
2025-02-07 19:15:30,253 - INFO - Waiting for application shutdown.
|
| 131 |
+
2025-02-07 19:15:30,259 - INFO - Application shutdown complete.
|
| 132 |
+
2025-02-07 19:15:30,268 - INFO - Finished server process [3260]
|
| 133 |
+
2025-02-07 19:15:31,437 - INFO - 1 change detected
|
| 134 |
+
2025-02-07 19:15:37,840 - INFO - 5 changes detected
|
| 135 |
+
2025-02-07 19:15:40,666 - INFO - CSV file loaded successfully.
|
| 136 |
+
2025-02-07 19:15:40,742 - INFO - Data processing completed.
|
| 137 |
+
2025-02-07 19:15:40,810 - INFO - Starting FastAPI application...
|
| 138 |
+
2025-02-07 19:15:40,816 - INFO - Started server process [12804]
|
| 139 |
+
2025-02-07 19:15:40,848 - INFO - Waiting for application startup.
|
| 140 |
+
2025-02-07 19:15:40,856 - INFO - Application startup complete.
|
| 141 |
+
2025-02-07 19:16:31,840 - INFO - Shutting down
|
| 142 |
+
2025-02-07 19:16:31,950 - INFO - Waiting for application shutdown.
|
| 143 |
+
2025-02-07 19:16:31,955 - INFO - Application shutdown complete.
|
| 144 |
+
2025-02-07 19:16:31,960 - INFO - Finished server process [12804]
|
| 145 |
+
2025-02-07 19:16:32,433 - INFO - Stopping reloader process [9352]
|
world_population.csv → backend/data/world_population.csv
RENAMED
|
File without changes
|
backend/logger.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
|
| 3 |
+
# Configure logging settings
|
| 4 |
+
logging.basicConfig(
|
| 5 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 6 |
+
level=logging.INFO,
|
| 7 |
+
handlers=[
|
| 8 |
+
logging.FileHandler("backend.log"), # Save logs to a file
|
| 9 |
+
logging.StreamHandler() # Print logs to the console
|
| 10 |
+
]
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
if __name__ == "__main__":
|
| 16 |
+
logger.info("Logger is set up correctly!")
|
backend/main.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from backend.routes.continents import router
|
| 3 |
+
from backend.logger import logger
|
| 4 |
+
from backend.routes.continents import router
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Include API routes
|
| 9 |
+
app.include_router(router)
|
| 10 |
+
|
| 11 |
+
logger.info("Starting FastAPI application...")
|
| 12 |
+
|
| 13 |
+
@app.get("/")
|
| 14 |
+
def home():
|
| 15 |
+
logger.info("Home endpoint accessed")
|
| 16 |
+
return {"message": "Welcome to the World Population API"}
|
backend/models/__pycache__/population_models.cpython-313.pyc
ADDED
|
Binary file (2.88 kB). View file
|
|
|
backend/models/population_models.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel, Field
|
| 2 |
+
from typing import List, Union
|
| 3 |
+
|
| 4 |
+
class ContinentBase(BaseModel):
|
| 5 |
+
continent: str = Field(..., title="Continent Name", example="Asia")
|
| 6 |
+
|
| 7 |
+
class ContinentStats(BaseModel):
|
| 8 |
+
Total_Countries: int = Field(..., title="Total Number of Countries", example=48)
|
| 9 |
+
Total_Population: int = Field(..., title="Total Population", example=4600000000)
|
| 10 |
+
Average_Population: float = Field(..., title="Average Population Per Country", example=96000000)
|
| 11 |
+
Total_Area: int = Field(..., title="Total Land Area (sq km)", example=44679000)
|
| 12 |
+
max_population: int = Field(..., title="Highest Country Population", example=1400000000)
|
| 13 |
+
min_population: int = Field(..., title="Lowest Country Population", example=100000)
|
| 14 |
+
Country_Max_Population: str = Field(..., title="Country with Highest Population", example="China")
|
| 15 |
+
Country_Min_Population: str = Field(..., title="Country with Lowest Population", example="Maldives")
|
| 16 |
+
Population_Density: float = Field(..., title="Population Density (people/sq km)", example=103)
|
| 17 |
+
|
| 18 |
+
class ContinentResponse(BaseModel):
|
| 19 |
+
continent: str = Field(..., title="Continent Name", example="Asia")
|
| 20 |
+
statistics: ContinentStats
|
| 21 |
+
|
| 22 |
+
class ContinentsListResponse(BaseModel):
|
| 23 |
+
continents: List[str] = Field(..., title="List of Continents", example=["Asia", "Europe", "Africa"])
|
| 24 |
+
|
| 25 |
+
class StatResponse(BaseModel):
|
| 26 |
+
stat: str = Field(..., title="Statistic Name", example="Total Population")
|
| 27 |
+
value: Union[str, int, float] = Field(..., title="Statistic Value", example=4600000000)
|
| 28 |
+
|
population_pandas.py → backend/population_pandas.py
RENAMED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Aggregate the data
|
| 8 |
continent_stats = df.groupby("Continent").agg(
|
|
@@ -21,31 +26,20 @@ continent_stats["Population_Density"] = (
|
|
| 21 |
continent_stats["Total_Population"] / continent_stats["Total_Area"]
|
| 22 |
)
|
| 23 |
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return {"continents": continents}
|
| 33 |
-
|
| 34 |
-
@app.get("/continents/{continent}/")
|
| 35 |
-
def get_continent_stats(continent: str, stat: str = None):
|
| 36 |
-
# Filter the dataframe for the given continent
|
| 37 |
result = continent_stats[continent_stats["Continent"] == continent].squeeze()
|
| 38 |
-
print(result)
|
| 39 |
-
print(type(result))
|
| 40 |
-
|
| 41 |
-
# Convert row to a dictionary
|
| 42 |
-
result = result.to_dict()
|
| 43 |
-
print(result)
|
| 44 |
|
| 45 |
-
if
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
else:
|
| 49 |
-
raise HTTPException(status_code=400, detail="Invalid statistic requested")
|
| 50 |
|
| 51 |
-
return result
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
+
# from backend.population_pandas import get_continents, get_continent_data
|
| 3 |
+
from backend.logger import logger
|
| 4 |
|
| 5 |
+
# Load the dataset
|
| 6 |
+
try:
|
| 7 |
+
df = pd.read_csv("data/world_population.csv")
|
| 8 |
+
logger.info("CSV file loaded successfully.")
|
| 9 |
+
except Exception as e:
|
| 10 |
+
logger.error(f"Error loading CSV file: {e}")
|
| 11 |
|
| 12 |
# Aggregate the data
|
| 13 |
continent_stats = df.groupby("Continent").agg(
|
|
|
|
| 26 |
continent_stats["Total_Population"] / continent_stats["Total_Area"]
|
| 27 |
)
|
| 28 |
|
| 29 |
+
logger.info("Data processing completed.")
|
| 30 |
|
| 31 |
+
def get_continents():
|
| 32 |
+
"""Returns a list of all continents."""
|
| 33 |
+
logger.info("Fetching all continents.")
|
| 34 |
+
return continent_stats["Continent"].tolist()
|
| 35 |
|
| 36 |
+
def get_continent_data(continent):
|
| 37 |
+
"""Returns statistics for a specific continent."""
|
| 38 |
+
logger.info(f"Fetching data for continent: {continent}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
result = continent_stats[continent_stats["Continent"] == continent].squeeze()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
if result.empty:
|
| 42 |
+
logger.warning(f"No data found for continent: {continent}")
|
| 43 |
+
return {}
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
return result.to_dict()
|
backend/routes/__pycache__/continents.cpython-313.pyc
ADDED
|
Binary file (2.13 kB). View file
|
|
|
backend/routes/continents.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException
|
| 2 |
+
from backend.population_pandas import get_continents, get_continent_data
|
| 3 |
+
from backend.models.population_models import ContinentResponse, ContinentsListResponse, StatResponse
|
| 4 |
+
from backend.logger import logger # Import the logger
|
| 5 |
+
|
| 6 |
+
router = APIRouter()
|
| 7 |
+
|
| 8 |
+
@router.get("/continents/", response_model=ContinentsListResponse)
|
| 9 |
+
def get_all_continents():
|
| 10 |
+
"""Fetch the list of all continents."""
|
| 11 |
+
logger.info("Fetching all continents")
|
| 12 |
+
return {"continents": get_continents()}
|
| 13 |
+
|
| 14 |
+
@router.get("/continents/{continent}/", response_model=ContinentResponse | StatResponse)
|
| 15 |
+
def get_continent_stats(continent: str, stat: str = None):
|
| 16 |
+
"""Fetch statistics for a specific continent."""
|
| 17 |
+
logger.info(f"Fetching statistics for continent: {continent}, stat: {stat}")
|
| 18 |
+
|
| 19 |
+
continent_data = get_continent_data(continent)
|
| 20 |
+
|
| 21 |
+
if not continent_data:
|
| 22 |
+
logger.error(f"Continent not found: {continent}")
|
| 23 |
+
raise HTTPException(status_code=404, detail="Continent not found")
|
| 24 |
+
|
| 25 |
+
if stat:
|
| 26 |
+
if stat in continent_data:
|
| 27 |
+
logger.info(f"Returning stat {stat} for {continent}")
|
| 28 |
+
return {"stat": stat, "value": continent_data[stat]}
|
| 29 |
+
else:
|
| 30 |
+
logger.warning(f"Invalid stat requested: {stat}")
|
| 31 |
+
raise HTTPException(status_code=400, detail="Invalid statistic requested")
|
| 32 |
+
|
| 33 |
+
logger.info(f"Successfully fetched data for {continent}")
|
| 34 |
+
return {"continent": continent, "statistics": continent_data}
|