Skip to main content

Welcome to villain-pagination

Installation

First of all, you need to install villain-pagination on your local to start.

pip install villain-pagination

Minimal Example

Villain Paginator uses SQLAlchemy ORM.

Let's start with a simple example below. You need to import page and paginator function from villain-pagination.

  • page : is a class which used as response_model in your route declaration.
  • paginator : is main functions that will paginate your data.

To use paginate, 5 params required.

  • db : is a SQLAlchemy session.
  • model : is a object which you want to paginate.
  • order_by : is a column of the model.
  • page : is a number of a page requested to show.
  • size : is a number of data which will shown in one page.

And Here is an example.

from typing import Iterator, Any
from faker import Faker
from fastapi import Depends, FastAPI
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker

from villain import page, paginator

engine = create_engine("sqlite:///.db", connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=True, autoflush=True, bind=engine)

Base = declarative_base(bind=engine)

fake = Faker()

class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String, nullable=False)

Base.metadata.create_all()

app = FastAPI()

@app.on_event("startup")
def on_startup() -> None:
    session = SessionLocal()
    session.add_all([User(name=fake.name()) for _ in range(100)])
    session.flush()
    session.close()

def get_db() -> Iterator[Session]:
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app.get("/users/", response_model=page.Page)
def get_users(db: Session = Depends(get_db)) -> Any:
    return paginator.paginate(db = db, model = User, order_by= User.id, page = 0, size = 10 )

Metadata

Release files for villain-pagination 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for villain-pagination 0.1.5
File Interpreter ABI Platform
villain_pagination-0.1.5-py3-none-any.whl Python 3 none any Details

Release files / villain_pagination-0.1.5-py3-none-any.whl

Download URL villain_pagination-0.1.5-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2dfe15955edd39d6275e687d688c9e79722c9205d84156ef237e044a1bd0f34
BLAKE2b-256 checksum
How to use checksums
c265750ec9063b3320429d31bb77863fd7648c89855bb926eff56d9652d8eea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.1.5 This release

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page