Python Tutorial 58 – Flask Paginate, Implement Paginations to List Products and Services in Search Result

Pagination to break down product and service search results by pages is a popular approach in web and mobile applications. In this piece, I’ll walk you through how to utilize flask paginate to implement paginations

flask paginate

Pagination to break down product and service search results by pages is a popular approach in web and mobile applications. In this piece, I’ll walk you through how to utilize flask paginate to implement paginations

What’s Flask Paginate

Flask paginate is a simple pagination extension for flask which is used to show results in a limited number per page from tons of data. It uses bootstrap as a CSS framework and works with it perfectly to enhance user experience.

flask paginate

Results are paginated using the paginate function of Flask SQLAlchemy with any number of arguments as desired. The result of calling the paginate() function is a Pagination Object, which has many methods that can help you achieve your desired result

Installation and Module Import

Similar to other flask extensions, installing flask paginate is super easy. We can input this command into the computer terminal.

Pip3 install flask-paginate

In the Python script, we need to import these three modules from the flask_paginate. I’ll use them with the explanation in a moment.

from flask_paginate import Pagination, get_page_args, get_page_parameter

Create the page source code parameter

When the configuration and deployment are done, we can start setting up the page sourcecode parameter.

get_page_parameter()

Different from the API endpoint I walked through before, flask paginates itself has a method, get_page_parameter(), for developers to create a page parameter. Once the page variable is created, the application can automatically detect the page number and show the result. The default page number would be 1.

Set up results per page

flaks paginate

Meanwhile, we need to create a per_page variable to define the limit number of each page. Here I set 20. Then, the offset number after page one needs to be set up as well. So here is the variable.

Offset = (page - 1 )* per_page

Manage what you like to show per page

This is the most critical component in the flask pagination because it connects with your database and shows what you want to display.

Basically, the purpose of the pagination aims to break down all your fetched data from the database and show it by pages. So we need to create a range of data to show working along with the page number.

1. Create the x and y variables standing for the starting and ending point of the data

if page != None:

x = (int(page) - 1) * per_page

y = int(page) * per_page

2. Fetch all the data

cursor.execute('''SELECT….. ‘’’)

all_data_all = cursor.fetchall()

for i in all_data_all:

       Element_info = {.....}

allCountries.append(element_info)

3. Create a User variable used for the table parameter in a moment

user = allCountries[x:y]

Pagination Variable and Render_template

For returning the paginate page data, we need to create a variable by using the pagination method given by the flask paginate module.

In this method, there are several parameters and value-added on, they are listed below

pagination = Pagination(page=page, per_page=per_page, offset=offset, total=len(allCountries), record_name='allCountries')

  • Page
  • Per_page
  • Offset
  • Total
  • Record_name

Then, each request and response from the render_template needs to add the pagination variable and also the user created above.

  • Tables
  • pagination

return render_template("demo.html", tables=user, all_data_country2=data, all_data_category2=data, user=current_user, pagination=pagination)

Add Pagination Jinja Codes in the Specific HTML Script

By referring to the variables we used in the above render_template, we can use Jinja to sync them together.

First thing first, it’s the pagination that let users select which page they like to explore.

{{ pagination.links }}

flask paginate

Then, for the fetched data range in the specific page number, we can utilize the Jinjia loop and the table variable to show the relevant result on each paginated page.

Full Python Script of Flask Pagination Code Sample

If you are interested in the full Python Tutorial 58 – Flask Paginate, Implement Paginations to List Products and Services in Search Result, please subscribe to our newsletter by adding the message “Python Tutorial 58”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Python Tutorial 58 – Flask Paginate, Implement Paginations to List Products and Services in Search Result. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support my channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)