Flask Rest Paginate
A Pagination Extension for Flask RESTful.
Installation
Install the extension using
pip install flask-rest-paginate
Usage
In your app, add the extension as follows
from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_rest_paginate import Pagination
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///paginate-test.db"
db = SQLAlchemy(app)
pagination = Pagination(app, db)
Use in your project as
pagination.paginate(AuthorModel, author_schema)
You can also pass the sqlalchemy query object as
pagination.paginate(AuthorModel.query.filter_by(id=author_id), author_schema)
If you want to use marshmallow schemas then set the third param True
pagination.paginate(AuthorModel, marshamllow_author_schema, True)
Customize your pagination schema
If you want create a custom pagination schema. You should pass pagination_schema_hook and return a Dict with you custom schema
res = pagination.paginate(
AuthorModel,
schema,
True,
pagination_schema_hook=lambda current_page, page_obj: {
"next": page_obj.has_next,
"prev": page_obj.has_prev,
"current": current_page,
"pages": page_obj.pages,
"per_page": page_obj.per_page,
"total": page_obj.total,
},
)
You can also customize key names where data and pagination info are placed, setting the configuration variables:
app.config['PAGINATE_PAGINATION_OBJECT_KEY'] = "page_info"
app.config['PAGINATE_DATA_OBJECT_KEY'] = "results"
...
{
"page_info": {
"currentPage": 1,
"hasNext": false,
"hasPrev": false,
"pages": 1,
"size": 20,
"totalElements": 2
}
"results": {
...
}
}
Furthermore, pagination info can be moved directly to the root of the result object:
app.config['PAGINATE_PAGINATION_OBJECT_KEY'] = None
app.config['PAGINATE_DATA_OBJECT_KEY'] = "results"
...
{
"currentPage": 1,
"hasNext": false,
"hasPrev": false,
"pages": 1,
"size": 20,
"totalElements": 2
"results": {
...
}
}
Example:
Check the example folder for an example of the extension.
Contributing
We are always grateful for any kind of contribution including but not limited to bug reports, code enhancements, bug fixes, and even functionality suggestions.
You can report any bug you find or suggest new functionality with a new issue.
If you want to add yourself some functionality to the extension:
- Open an issue
- Comment there you are working on a new functionality
- Fork the repo
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Adds my new feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- mention the issue number in the PR description as
fixes #123, #321
Metadata
Release files for flask-rest-paginate 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flask_rest_paginate-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for flask_rest_paginate-1.1.3-py3-none-any.whl |
Release files / flask_rest_paginate-1.1.3-py3-none-any.whl
| Download URL | flask_rest_paginate-1.1.3-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd8f88210e959b8a2339003f76e303ae221ba7df190bef8714b474aa9c4a3e9d
|
|
BLAKE2b-256 checksum How to use checksums |
49b88ffd1dc1447ada47f3329df37e0e8c9a5278c5f640e60d57784f41128327
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/2.7.16
|
Security for flask-rest-paginate 1.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.