Ezhil commited on
Commit
45ce216
·
1 Parent(s): 5b886dd

backend deployment

Browse files
__init__.py ADDED
File without changes
__pycache__/__init__.cpython-310.pyc ADDED
Binary file (148 Bytes). View file
 
__pycache__/logger.cpython-310.pyc ADDED
Binary file (519 Bytes). View file
 
__pycache__/main.cpython-310.pyc ADDED
Binary file (578 Bytes). View file
 
api/__pycache__/main.cpython-310.pyc DELETED
Binary file (766 Bytes)
 
api/main.py DELETED
@@ -1,20 +0,0 @@
1
- from fastapi import FastAPI
2
- app = FastAPI()
3
- import os
4
- import sys
5
- src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__),"../","api"))
6
- sys.path.append(src_directory)
7
-
8
-
9
- from modules import continents
10
-
11
- df = continents.process_data()
12
-
13
- @app.get("/")
14
- def home():
15
- return {"message": "Welcome World"}
16
-
17
- @app.get("/continents")
18
- def display_continents():
19
- continent = continents.get_continents(df)
20
- return continent.tolist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
api/modules/__pycache__/continents.cpython-310.pyc DELETED
Binary file (524 Bytes)
 
api/modules/continents.py DELETED
@@ -1,11 +0,0 @@
1
- import pandas as pd
2
-
3
- file_path = "./data/world_population.csv"
4
-
5
- def process_data():
6
- data_frame = pd.read_csv(file_path)
7
- return data_frame
8
-
9
- def get_continents(data_frame):
10
- continents = data_frame['Continent'].unique()
11
- return continents
 
 
 
 
 
 
 
 
 
 
 
 
logger.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import os
3
+
4
+ # Ensure the logs directory exists
5
+ os.makedirs("logs", exist_ok=True)
6
+
7
+ # Configure logging settings
8
+ logging.basicConfig(
9
+ format="%(asctime)s - %(levelname)s - %(message)s",
10
+ level=logging.INFO,
11
+ handlers=[
12
+ logging.FileHandler("logs/backend.log"), # Save logs to logs/ folder
13
+ logging.StreamHandler() # Print logs to the console
14
+ ]
15
+ )
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+ if __name__ == "__main__":
20
+ logger.info("Logger is set up correctly!")
logs/backend.log ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 2025-02-12 15:51:01,313 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
2
+ 2025-02-12 15:51:01,338 - INFO - Data processing completed.
3
+ 2025-02-12 15:51:01,365 - INFO - Starting FastAPI application...
4
+ 2025-02-12 15:51:01,389 - INFO - Will watch for changes in these directories: ['C:\\Users\\User\\Desktop\\Data_Insights\\backend']
5
+ 2025-02-12 15:51:01,399 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
6
+ 2025-02-12 15:51:01,401 - INFO - Started reloader process [12336] using WatchFiles
7
+ 2025-02-12 15:51:01,840 - INFO - 1 change detected
8
+ 2025-02-12 15:51:02,252 - INFO - 1 change detected
9
+ 2025-02-12 15:51:02,690 - INFO - 1 change detected
10
+ 2025-02-12 15:51:03,106 - INFO - 1 change detected
11
+ 2025-02-12 15:51:03,466 - INFO - 1 change detected
12
+ 2025-02-12 15:51:03,882 - INFO - 1 change detected
13
+ 2025-02-12 15:51:03,916 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
14
+ 2025-02-12 15:51:03,938 - INFO - Data processing completed.
15
+ 2025-02-12 15:51:03,962 - INFO - Starting FastAPI application...
16
+ 2025-02-12 15:51:03,971 - INFO - Started server process [676]
17
+ 2025-02-12 15:51:03,977 - INFO - Waiting for application startup.
18
+ 2025-02-12 15:51:03,979 - INFO - Application startup complete.
19
+ 2025-02-12 15:51:04,291 - INFO - 1 change detected
20
+ 2025-02-12 15:51:04,650 - INFO - 1 change detected
21
+ 2025-02-12 15:51:05,068 - INFO - 1 change detected
22
+ 2025-02-12 15:51:05,480 - INFO - 1 change detected
23
+ 2025-02-12 15:51:05,889 - INFO - 1 change detected
24
+ 2025-02-12 15:51:06,301 - INFO - 1 change detected
25
+ 2025-02-12 15:51:06,662 - INFO - 1 change detected
26
+ 2025-02-12 15:51:07,079 - INFO - 1 change detected
27
+ 2025-02-12 15:51:07,488 - INFO - 1 change detected
28
+ 2025-02-12 15:51:07,899 - INFO - 1 change detected
29
+ 2025-02-12 15:51:08,267 - INFO - 1 change detected
30
+ 2025-02-12 15:51:08,691 - INFO - 1 change detected
31
+ 2025-02-12 15:51:09,086 - INFO - 1 change detected
32
+ 2025-02-12 15:51:09,492 - INFO - 1 change detected
33
+ 2025-02-12 15:51:09,905 - INFO - 1 change detected
34
+ 2025-02-12 15:51:10,325 - INFO - 1 change detected
35
+ 2025-02-12 15:51:10,753 - INFO - 1 change detected
36
+ 2025-02-12 15:51:11,167 - INFO - 1 change detected
37
+ 2025-02-12 15:51:11,583 - INFO - 1 change detected
38
+ 2025-02-12 15:51:11,990 - INFO - 1 change detected
39
+ 2025-02-12 15:51:12,399 - INFO - 1 change detected
40
+ 2025-02-12 15:51:12,761 - INFO - 1 change detected
41
+ 2025-02-12 15:51:13,070 - INFO - Shutting down
42
+ 2025-02-12 15:51:13,179 - INFO - Waiting for application shutdown.
43
+ 2025-02-12 15:51:13,182 - INFO - Application shutdown complete.
44
+ 2025-02-12 15:51:13,185 - INFO - Finished server process [676]
45
+ 2025-02-12 15:51:13,322 - INFO - Stopping reloader process [12336]
46
+ 2025-02-12 15:54:54,866 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
47
+ 2025-02-12 15:54:54,874 - INFO - Data processing completed.
48
+ 2025-02-12 15:54:54,895 - INFO - Starting FastAPI application...
49
+ 2025-02-12 15:54:54,919 - INFO - Will watch for changes in these directories: ['C:\\Users\\User\\Desktop\\Data_Insights\\backend']
50
+ 2025-02-12 15:54:54,923 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
51
+ 2025-02-12 15:54:54,926 - INFO - Started reloader process [20092] using WatchFiles
52
+ 2025-02-12 15:54:55,325 - INFO - 1 change detected
53
+ 2025-02-12 15:54:55,774 - INFO - 1 change detected
54
+ 2025-02-12 15:54:56,194 - INFO - 1 change detected
55
+ 2025-02-12 15:54:56,556 - INFO - 1 change detected
56
+ 2025-02-12 15:54:56,984 - INFO - 1 change detected
57
+ 2025-02-12 15:54:57,143 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
58
+ 2025-02-12 15:54:57,150 - INFO - Data processing completed.
59
+ 2025-02-12 15:54:57,166 - INFO - Starting FastAPI application...
60
+ 2025-02-12 15:54:57,167 - INFO - Started server process [14472]
61
+ 2025-02-12 15:54:57,178 - INFO - Waiting for application startup.
62
+ 2025-02-12 15:54:57,180 - INFO - Application startup complete.
63
+ 2025-02-12 15:54:57,350 - INFO - 1 change detected
64
+ 2025-02-12 15:54:57,763 - INFO - 1 change detected
65
+ 2025-02-12 15:54:58,174 - INFO - 1 change detected
66
+ 2025-02-12 15:54:58,582 - INFO - 1 change detected
67
+ 2025-02-12 15:54:58,942 - INFO - 1 change detected
68
+ 2025-02-12 15:54:59,356 - INFO - 1 change detected
69
+ 2025-02-12 15:54:59,765 - INFO - 1 change detected
70
+ 2025-02-12 15:55:00,175 - INFO - 1 change detected
71
+ 2025-02-12 15:55:00,585 - INFO - 1 change detected
72
+ 2025-02-12 15:55:00,944 - INFO - 1 change detected
73
+ 2025-02-12 15:55:01,361 - INFO - 1 change detected
74
+ 2025-02-12 15:55:01,773 - INFO - 1 change detected
75
+ 2025-02-12 15:55:02,183 - INFO - 1 change detected
76
+ 2025-02-12 15:55:02,545 - INFO - 1 change detected
77
+ 2025-02-12 15:55:02,973 - INFO - 1 change detected
78
+ 2025-02-12 15:55:03,401 - INFO - 1 change detected
79
+ 2025-02-12 15:55:03,763 - INFO - 1 change detected
80
+ 2025-02-12 15:55:04,128 - INFO - 1 change detected
81
+ 2025-02-12 15:55:04,492 - INFO - 1 change detected
82
+ 2025-02-12 15:55:04,851 - INFO - 1 change detected
83
+ 2025-02-12 15:55:05,245 - INFO - 1 change detected
84
+ 2025-02-12 15:55:05,650 - INFO - 1 change detected
85
+ 2025-02-12 15:55:06,010 - INFO - 1 change detected
86
+ 2025-02-12 15:55:06,379 - INFO - 1 change detected
87
+ 2025-02-12 15:55:06,737 - INFO - 1 change detected
88
+ 2025-02-12 15:55:07,111 - INFO - 1 change detected
89
+ 2025-02-12 15:55:07,464 - INFO - 1 change detected
90
+ 2025-02-12 15:55:07,832 - INFO - 1 change detected
91
+ 2025-02-12 15:55:08,195 - INFO - 1 change detected
92
+ 2025-02-12 15:55:08,556 - INFO - 1 change detected
93
+ 2025-02-12 15:55:08,920 - INFO - 1 change detected
94
+ 2025-02-12 15:55:09,287 - INFO - 1 change detected
95
+ 2025-02-12 15:55:09,650 - INFO - 1 change detected
96
+ 2025-02-12 15:55:10,014 - INFO - 1 change detected
97
+ 2025-02-12 15:55:10,374 - INFO - 1 change detected
98
+ 2025-02-12 15:55:10,740 - INFO - 1 change detected
99
+ 2025-02-12 15:55:11,126 - INFO - 1 change detected
100
+ 2025-02-12 15:55:11,484 - INFO - 1 change detected
101
+ 2025-02-12 15:55:11,851 - INFO - 1 change detected
102
+ 2025-02-12 15:55:12,210 - INFO - 1 change detected
103
+ 2025-02-12 15:55:12,574 - INFO - 1 change detected
104
+ 2025-02-12 15:55:12,937 - INFO - 1 change detected
105
+ 2025-02-12 15:55:13,300 - INFO - 1 change detected
106
+ 2025-02-12 15:55:13,577 - INFO - Shutting down
107
+ 2025-02-12 15:55:13,688 - INFO - Waiting for application shutdown.
108
+ 2025-02-12 15:55:13,690 - INFO - Application shutdown complete.
109
+ 2025-02-12 15:55:13,691 - INFO - Finished server process [14472]
110
+ 2025-02-12 15:55:13,874 - INFO - Stopping reloader process [20092]
111
+ 2025-02-12 15:57:44,684 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
112
+ 2025-02-12 15:57:44,695 - INFO - Data processing completed.
113
+ 2025-02-12 15:57:44,733 - INFO - Starting FastAPI application...
114
+ 2025-02-12 15:57:57,327 - INFO - Home endpoint accessed
115
+ 2025-02-12 15:58:02,645 - INFO - Fetching all continents
116
+ 2025-02-12 15:58:02,646 - INFO - Fetching all continents.
117
+ 2025-02-12 15:58:07,736 - INFO - Fetching statistics for continent: Asia, stat: None
118
+ 2025-02-12 15:58:07,737 - INFO - Fetching data for continent: Asia
119
+ 2025-02-12 15:58:07,750 - INFO - Successfully fetched data for Asia
120
+ 2025-02-12 15:58:12,511 - INFO - Fetching statistics for continent: Asia, stat: None
121
+ 2025-02-12 15:58:12,512 - INFO - Fetching data for continent: Asia
122
+ 2025-02-12 15:58:12,515 - INFO - Successfully fetched data for Asia
123
+ 2025-02-12 16:03:55,832 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
124
+ 2025-02-12 16:03:55,846 - INFO - Data processing completed.
125
+ 2025-02-12 16:03:55,876 - INFO - Starting FastAPI application...
126
+ 2025-02-12 16:03:58,995 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
127
+ 2025-02-12 16:03:59,019 - INFO - Data processing completed.
128
+ 2025-02-12 16:03:59,065 - INFO - Starting FastAPI application...
129
+ 2025-02-12 16:04:10,394 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
130
+ 2025-02-12 16:04:10,407 - INFO - Data processing completed.
131
+ 2025-02-12 16:04:10,439 - INFO - Starting FastAPI application...
132
+ 2025-02-12 16:04:17,073 - INFO - Fetching statistics for continent: Asia, stat: None
133
+ 2025-02-12 16:04:17,074 - INFO - Fetching data for continent: Asia
134
+ 2025-02-12 16:04:17,132 - INFO - Successfully fetched data for Asia
135
+ 2025-02-12 16:04:47,506 - INFO - Fetching statistics for continent: Asia, stat: Total_Countries
136
+ 2025-02-12 16:04:47,507 - INFO - Fetching data for continent: Asia
137
+ 2025-02-12 16:04:47,508 - INFO - Returning stat Total_Countries for Asia
138
+ 2025-02-12 16:04:58,352 - INFO - Fetching statistics for continent: Asia, stat: None
139
+ 2025-02-12 16:04:58,353 - INFO - Fetching data for continent: Asia
140
+ 2025-02-12 16:04:58,361 - INFO - Successfully fetched data for Asia
141
+ 2025-02-12 16:06:13,407 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
142
+ 2025-02-12 16:06:13,416 - INFO - Data processing completed.
143
+ 2025-02-12 16:06:13,431 - INFO - Starting FastAPI application...
144
+ 2025-02-12 16:06:26,579 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
145
+ 2025-02-12 16:06:26,590 - INFO - Data processing completed.
146
+ 2025-02-12 16:06:26,605 - INFO - Starting FastAPI application...
147
+ 2025-02-12 16:06:26,626 - INFO - Will watch for changes in these directories: ['C:\\Users\\User\\Desktop\\Data_Insights\\backend']
148
+ 2025-02-12 16:06:26,629 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
149
+ 2025-02-12 16:06:26,631 - INFO - Started reloader process [5564] using WatchFiles
150
+ 2025-02-12 16:06:28,204 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
151
+ 2025-02-12 16:06:28,213 - INFO - Data processing completed.
152
+ 2025-02-12 16:06:28,229 - INFO - Starting FastAPI application...
153
+ 2025-02-12 16:06:28,230 - INFO - Started server process [8188]
154
+ 2025-02-12 16:06:28,234 - INFO - Waiting for application startup.
155
+ 2025-02-12 16:06:28,236 - INFO - Application startup complete.
156
+ 2025-02-12 16:06:42,104 - INFO - 2 changes detected
157
+ 2025-02-12 16:06:46,458 - INFO - 1 change detected
158
+ 2025-02-12 16:06:56,110 - INFO - Shutting down
159
+ 2025-02-12 16:06:56,220 - INFO - Waiting for application shutdown.
160
+ 2025-02-12 16:06:56,221 - INFO - Application shutdown complete.
161
+ 2025-02-12 16:06:56,223 - INFO - Finished server process [8188]
162
+ 2025-02-12 16:06:56,429 - INFO - Stopping reloader process [5564]
163
+ 2025-02-12 16:07:12,464 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
164
+ 2025-02-12 16:07:12,476 - INFO - Data processing completed.
165
+ 2025-02-12 16:07:12,493 - INFO - Starting FastAPI application...
166
+ 2025-02-12 16:07:12,521 - INFO - Will watch for changes in these directories: ['C:\\Users\\User\\Desktop\\Data_Insights\\backend']
167
+ 2025-02-12 16:07:12,524 - INFO - Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
168
+ 2025-02-12 16:07:12,526 - INFO - Started reloader process [5188] using WatchFiles
169
+ 2025-02-12 16:07:14,185 - INFO - CSV file loaded successfully from: C:\Users\User\Desktop\Data_Insights\backend\data\world_population.csv
170
+ 2025-02-12 16:07:14,193 - INFO - Data processing completed.
171
+ 2025-02-12 16:07:14,208 - INFO - Starting FastAPI application...
172
+ 2025-02-12 16:07:14,209 - INFO - Started server process [10856]
173
+ 2025-02-12 16:07:14,216 - INFO - Waiting for application startup.
174
+ 2025-02-12 16:07:14,217 - INFO - Application startup complete.
175
+ 2025-02-12 16:07:24,758 - INFO - Fetching statistics for continent: Asia, stat: None
176
+ 2025-02-12 16:07:24,759 - INFO - Fetching data for continent: Asia
177
+ 2025-02-12 16:07:24,763 - INFO - Successfully fetched data for Asia
178
+ 2025-02-12 16:08:25,914 - INFO - Shutting down
179
+ 2025-02-12 16:08:26,022 - INFO - Waiting for application shutdown.
180
+ 2025-02-12 16:08:26,025 - INFO - Application shutdown complete.
181
+ 2025-02-12 16:08:26,026 - INFO - Finished server process [10856]
182
+ 2025-02-12 16:08:26,190 - INFO - Stopping reloader process [5188]
main.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from routes.continents import router
3
+ from logger import logger
4
+ from 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"}
models/__pycache__/population_models.cpython-310.pyc ADDED
Binary file (2.28 kB). View file
 
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
+
routes/__init__.py ADDED
File without changes
routes/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (155 Bytes). View file
 
routes/__pycache__/continents.cpython-310.pyc ADDED
Binary file (1.5 kB). View file
 
routes/continents.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, HTTPException
2
+ from service.population_pandas import get_continents, get_continent_data
3
+ from models.population_models import ContinentResponse, ContinentsListResponse, StatResponse
4
+ from 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}
service/__pycache__/population_pandas.cpython-310.pyc ADDED
Binary file (1.84 kB). View file
 
service/population_pandas.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ # from backend.population_pandas import get_continents, get_continent_data
3
+ from logger import logger
4
+
5
+ import os
6
+
7
+ # file_path = os.path.join(os.path.dirname(__file__), "../../data/world_population.csv")
8
+ file_path = os.path.join(os.path.dirname(__file__), "../data/world_population.csv")
9
+ # file_path = os.path.abspath(file_path) # Convert to absolute path
10
+
11
+ file_path = os.path.abspath(file_path) # Convert to absolute path
12
+ try:
13
+ df = pd.read_csv(file_path)
14
+ logger.info(f"CSV file loaded successfully from: {file_path}")
15
+ except Exception as e:
16
+ logger.error(f"Error loading CSV file from {file_path}: {e}")
17
+ df = None # Prevent NameError if file loading fails
18
+
19
+ if df is not None:
20
+ # Perform the aggregations only if df is successfully loaded
21
+ continent_stats = df.groupby("Continent").agg(
22
+ Total_Countries=('Country', 'count'),
23
+ Total_Population=('Population', 'sum'),
24
+ Average_Population=('Population', 'mean'),
25
+ Total_Area=('Area', 'sum'),
26
+ max_population=('Population', 'max'),
27
+ min_population=('Population', 'min'),
28
+ Country_Max_Population=('Population', lambda x: df.loc[x.idxmax(), 'Country']),
29
+ Country_Min_Population=('Population', lambda x: df.loc[x.idxmin(), 'Country'])
30
+ ).reset_index()
31
+ # Compute Population Density
32
+ continent_stats["Population_Density"] = (
33
+ continent_stats["Total_Population"] / continent_stats["Total_Area"]
34
+ )
35
+
36
+ logger.info("Data processing completed.")
37
+
38
+ def get_continents():
39
+ """Returns a list of all continents."""
40
+ logger.info("Fetching all continents.")
41
+ return continent_stats["Continent"].tolist()
42
+
43
+ def get_continent_data(continent):
44
+ """Returns statistics for a specific continent."""
45
+ logger.info(f"Fetching data for continent: {continent}")
46
+ result = continent_stats[continent_stats["Continent"] == continent].squeeze()
47
+
48
+ if result.empty:
49
+ logger.warning(f"No data found for continent: {continent}")
50
+ return {}
51
+
52
+ return result.to_dict()
53
+
54
+