Skip to content

Latest commit

 

History

History
178 lines (122 loc) · 2.93 KB

File metadata and controls

178 lines (122 loc) · 2.93 KB

Dhammapada API

A simple read-only Flask API for serving Dhammapada chapters and verses from a local SQLite database.
On first run, the application creates the database and imports data from dhammapada.json.

Overview

This project:

  • uses Flask to expose HTTP endpoints
  • uses Flask-SQLAlchemy with SQLite for storage
  • uses Flask-Marshmallow for JSON serialization
  • automatically loads Dhammapada data from dhammapada.json

The current API is read-only and focused on retrieving chapters and verses.

Project Structure

dhammapadaAPI/
|-- app.py
|-- dhammapada.json
|-- README.md
`-- instance/
    `-- dhammapada.db

Requirements

Install Python 3.10+ and these packages:

pip install flask flask-sqlalchemy flask-marshmallow marshmallow-sqlalchemy

Run Locally

From the project root:

python app.py

The app will start a local development server, typically at:

http://127.0.0.1:5000

How It Works

When the application starts:

  1. it creates the SQLite database if it does not already exist
  2. it checks whether the chapter table has data
  3. if empty, it imports chapters and verses from dhammapada.json

The database file is created at:

instance/dhammapada.db

API Endpoints

GET /

Returns all chapters with their verses.

GET /chapters

Returns all chapters with their verses.

GET /chapters/<id>

Returns a single chapter by database id.

Example:

/chapters/1

GET /verses

Returns all verses ordered by chapter and verse number.

GET /verses/<id>

Returns a single verse by database id.

Example:

/verses/1

GET /chapters/<chapter_id>/verses

Returns all verses for a specific chapter using the chapter's database id.

Example:

/chapters/1/verses

Example Response

Example chapter object:

{
  "id": 1,
  "number": 1,
  "title": "Chapter title",
  "verses": [
    {
      "id": 1,
      "verse_number": 1,
      "text": "..."
    }
  ]
}

Data Source

The source text is stored in:

dhammapada.json

Its structure is:

{
  "chapters": [
    {
      "number": 1,
      "title": "Chapter title",
      "verses": [
        {
          "verse_number": 1,
          "text": "Verse text"
        }
      ]
    }
  ]
}

Notes

  • The current routes use database id, not chapter number or verse_number.
  • Running with debug=True is suitable for development only.
  • If you change dhammapada.json after the database is already populated, the app will not re-import automatically unless the database is cleared first.

Reset the Database

If you want to rebuild the database from the JSON file, delete:

instance/dhammapada.db

Then run:

python app.py

Future Improvements

  • add search by chapter number and verse number
  • add filtering and pagination
  • add a requirements.txt
  • add tests
  • add deployment instructions