pip install -U flask-paginate
- download app.py and index.html
python app.py
- visit http://127.0.0.1:5000/
-
-
Save Deepakdubey90/c89573dc45867b3f16e0317e83e5d546 to your computer and use it in GitHub Desktop.
A simple demo for how to use flask-paginate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from flask_paginate import Pagination, get_page_args | |
app = Flask(__name__) | |
app.template_folder = '' | |
users = list(range(100)) | |
def get_users(offset=0, per_page=10): | |
return users[offset: offset + per_page] | |
@app.route('/') | |
def index(): | |
page, per_page, offset = get_page_args(page_parameter='page', | |
per_page_parameter='per_page') | |
total = len(users) | |
pagination_users = get_users(offset=offset, per_page=per_page) | |
pagination = Pagination(page=page, per_page=per_page, total=total, | |
css_framework='bootstrap4') | |
return render_template('index.html', | |
users=pagination_users, | |
page=page, | |
per_page=per_page, | |
pagination=pagination, | |
) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<title>flask-bootstrap example</title> | |
<!-- Bootstrap --> | |
<!-- Latest compiled and minified CSS --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
{{ pagination.links }} | |
<div class="table-responsive"> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th>#</th> | |
<th>Value</th> | |
</tr> | |
</thead> | |
<tbody> | |
{% for user in users %} | |
<tr> | |
<td>{{ loop.index + (page - 1) * per_page }}</td> | |
<td>{{ user }}</td> | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
</div> | |
{{ pagination.links }} | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment