Below is a comprehensive guide to the Flask API endpoints for our Panchayat Data service. The application is designed to serve hierarchical location data (state → district → taluk → village) and the members associated with each village.
This API provides a structured way to query Indian Panchayat data from a CSV file (panchayat_data_with_members.csv). Each record contains hierarchical location info:
- State (
state_code,state) - District (
district_code,district) - Taluk (
taluk_code,taluk) - Village (
village_code,village) - Members (like name, phone, email, role, etc.)
With these endpoints, you can:
- Fetch all states and their codes.
- Drill down by state_code to get districts.
- Drill down by district_code to get taluks.
- Drill down by taluk_code to get villages.
- Fetch members by specifying a village_code (or get all members if no code is given).
- Add a new member to an existing village in the CSV.
GET /
- Description: Serves the
index.htmltemplate (the home page for the web app). - Response: Renders an HTML page if it exists (not JSON).
GET /api/states
- Description: Returns a list of all unique states in the dataset.
- Response (JSON Array):
[ { "state_code": 9, "state": "Uttar Pradesh" }, { "state_code": 8, "state": "Rajasthan" }, ... ] - Example:
curl http://localhost:<port>/api/states
GET /api/districts/<state_code>
- Description: For a given state_code, returns all districts associated with that state.
- URL Parameters:
<state_code>(integer) — Example:28for Andhra Pradesh.
- Response (JSON Array):
[ { "district_code": 576, "district": "North And Middle Andaman" }, { "district_code": 552, "district": "South Andamans" }, ... ] - Example:
curl http://localhost:<port>/api/districts/35
GET /api/taluks/<district_code>
- Description: For a given district_code, returns all taluks (sometimes referred to as blocks or tehsils).
- URL Parameters:
<district_code>(integer) — Example:576.
- Response (JSON Array):
[ { "taluk_code": 6793, "taluk": "Diglipur" }, { "taluk_code": 6796, "taluk": "Mayabunder" }, ... ] - Example:
curl http://localhost:<port>/api/taluks/576
GET /api/villages/<taluk_code>
- Description: For a given taluk_code, returns all villages under that taluk.
- URL Parameters:
<taluk_code>(integer) — Example:6793.
- Response (JSON Array):
[ { "village_code": 234465, "village": "Diglipur" }, { "village_code": 245696, "village": "Gandhi Nagar" }, ... ] - Example:
curl http://localhost:<port>/api/villages/6793
GET /api/members
- Description: Returns Panchayat members (Sarpanch, Ward Member, Pradhan, etc.) based on query parameters.
- Query Parameters:
village_code(integer, optional): If provided, filters members to only that village.
- Response (JSON Array):
[ { "name": "V Boopathi", "role": "Pradhan", "phone": "95XXXXXX41", "email": "gp**********37@gmail.com", "village": "Diglipur", "taluk": "Diglipur", "district": "North And Middle Andaman", "state": "Andaman And Nicobar Islands" }, ... ] - Usage:
- Get all members (no filters):
curl http://localhost:<port>/api/members
- Get members by village_code:
curl http://localhost:<port>/api/members?village_code=234465
- Get all members (no filters):
POST /api/members/add
- Description: Adds a new member to an existing village. Updates the in-memory DataFrame and persists the change in
panchayat_data_with_members.csv. - Request Body (JSON):
{ "village_code": 234465, "name": "John Doe", "phone": "9876543210", "email": "john@example.com", "role": "Ward Member" } - Response:
{ "success": true, "message": "Member added successfully" } - Error Handling:
- If the
village_codedoes not exist or any other error occurs, returns{"success": false, "message": "...error details..."}.
- If the
- CSV File:
panchayat_data_with_members.csv - Columns:
state_code(int)state(string)district_code(int)district(string)taluk_code(int)taluk(string)village_code(int)village(string)member_id(int)name(string) — member’s full namephone(string) — member’s phoneemail(string) — member’s emailrole(string) — e.g., “Pradhan,” “Ward Member,” etc.
Each endpoint reads from or writes to this CSV via Pandas.
If you add a member, a new row is appended to the CSV, keeping data consistent across sessions (though you should be aware of concurrency issues if multiple users add members simultaneously).
-
Local:
- Install dependencies:
pip install flask pandas
- Run the script:
python <filename>.py
- The API defaults to
PORT=5000if the environment variablePORTis not set.
- Install dependencies:
-
Render:
- Render sets an environment variable
PORTdynamically. - The script reads from
os.environ.get('PORT', 5000). - Ensure your
requirements.txtincludes Flask and pandas. - Deploy as a Web Service on Render, and it’ll handle the rest.
- Render sets an environment variable
That’s all, Comrade (Chief)! You have a fully functional, hierarchical Panchayat data API, ready for local testing or deployment on Render. Enjoy exploring the data!