Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Flask Bootstrap 4 Pagination Example to Paginate Array of Users Using flask-paginate Library

Posted on January 19, 2023

 

 

Welcome folks today in this blog post we will be paginating users in flask using flask-paginate library using bootstrap framework in browser. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

 

pip install flask-paginate

 

 

After that you need to create the app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
from flask import Flask, render_template
from flask_paginate import Pagination, get_page_args
 
 
app = Flask(__name__)
app.template_folder = ''
users = list(range(100))
 
if __name__ == '__main__':
    app.run(debug=True)

 

 

As you can see we are importing the flask library and then we are importing the flask pagination module and then we are using the list method to generate 100 test users using the range() method.

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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,
                           )

 

 

As you can see we have assigned a route whenever user goes to the / homepage then we will be showing the index.html template using the render_template() method and inside it we are passing the users array and inside it we are also passing the total pages and users per page value.

 

 

And now we need to make the index.html file and copy paste the below html

 

 

index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!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>

 

 

As you can see we are displaying the data inside the table and also we have the pagination links displayed based upon the total users present inside the array.

 

 

 

Recent Posts

  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Android Java Project to Download Youtube Video Thumbnail From URL & Save it inside SD Card
  • Android Java Project to Embed Google Maps & Add Markers Using Maps SDK
  • Android Java Project to Download Random Image From Unsplash Using OkHttp & Picasso Library & Display it
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme