Uncategorized

How to Implement Pagination Using FastAPI in Python?


Reading Time: 12 minutes

How to Implement Pagination Using FastAPI in Python?

Navigating lots of info can be tricky. Let’s find out how FastAPI makes it easy with pagination, breaking down content for a smoother ride. 

We will do a step-by-step implementation with FastAPI, a top-notch Python tool, which helps organize info better. 

Improve reading, speed, and user happiness using pagination tricks. 

Let’s get started!

In this blog, we will cover:

  • What is Pagination?
  • What is FastAPI?
  • Hands-on
  • Conclusion

What is Pagination?

Pagination is a method used to organize and present content, whether in printed documents or on digital platforms, in a more manageable and user-friendly manner. 

It involves breaking down a large body of content into smaller, discrete sections or pages, making it easier for users to navigate, read, and digest.

In printed materials like books, magazines, or newspapers, pagination refers to the allocation of content into pages with sequential numbers for reference. 

This system allows readers to locate specific information quickly, flip through pages, and maintain a sense of structure within the document.

In the digital realm, especially on websites and applications, pagination serves a similar purpose. 

Lengthy articles, search results, or lists of items (such as products on an e-commerce site) are divided into multiple pages, each containing a portion of the content. 

Users can navigate between these pages by clicking on links, typically represented as page numbers or navigation buttons like “Next” and “Previous”.

This functionality is particularly useful when dealing with extensive datasets or articles, as it prevents overwhelming users with too much information on a single page.

Why do we need Pagination?

How to Implement Pagination Using FastAPI in Python?

Pagination is essential for several reasons:

  • Improved Readability: Breaking content into smaller, discrete pages makes it more readable and less overwhelming for users. Long, continuous text can be tiresome to read, whereas shorter sections are easier to digest.
  • Faster Loading Times: In digital contexts, loading an entire webpage with extensive content can be slow, especially on slower internet connections or devices with limited resources. Pagination helps by loading only one page at a time, reducing the initial load time and providing a more responsive user experience.
  • Enhanced User Experience: Users can navigate through content more easily with pagination, whether it’s a lengthy article, search results, or product listings. They can jump to specific sections or items without scrolling endlessly.
  • Reduced Scrolling: Continuous scrolling can be tiresome and frustrating, especially on touch devices. Pagination offers a clear structure and allows users to access content without excessive scrolling.
  • Logical Organization: Pagination often reflects the logical organization of content. For instance, in search results, each page may contain a set number of relevant items. In books or magazines, chapters or articles are naturally divided into pages.
  • Accessibility: Pagination can improve accessibility for users with disabilities. Screen readers and other assistive technologies can more easily identify and navigate discrete pages.

In summary, pagination is a valuable design and organizational strategy that enhances user experience, facilitates content management, and improves the performance and accessibility of digital and printed materials.

What is FastAPI?

FastAPI is a modern, high-performance web framework for building APIs with Python based on standard type hints. 

It has the following key features:

  • Fast to run: It offers very high performance, on par with NodeJS and Go, thanks to Starlette and pydantic.
  • Fast to code: It allows for significant increases in development speed.
  • Reduced number of bugs: It reduces the possibility for human-induced errors.
  • Intuitive: It offers great editor support, with completion everywhere and less time debugging.
  • Straightforward: It’s designed to be uncomplicated to use and learn, so you can spend less time reading documentation.
  • Short: It minimizes code duplication.
  • Robust: It provides production-ready code with automatic interactive documentation.
  • Standards-based: It’s based on the open standards for APIs, OpenAPI and JSON Schema.

The framework is designed to optimize your developer experience so that you can write simple code to build production-ready APIs with best practices by default.

Hands-on

How to Implement Pagination Using FastAPI in Python?

In this hands-on, we’ll learn how to implement pagination in FastAPI using the fastapi-pagination library. 

We’ll start by creating a new project in the PyCharm IDE and installing the necessary libraries. 

Our coding journey begins with setting up FastAPI in the project. Next, we’ll create a test route to check our FastAPI setup. 

Afterward, we’ll plan the SQL schema for the data we want to paginate. 

Following that, we’ll establish a connection to our MySQL database using Python code. 

To insert dummy data into our MySQL table, we’ll use the faker library. 

Then, we’ll create a route to retrieve this data, and we’ll utilize the fastapi-pagination library to incorporate pagination into this endpoint. 

We’ll explore various ways to use pagination, and finally, we’ll conduct tests to ensure our code serves its intended purpose and produces the expected output.

Required installations:

To perform the demo, you require the following installations:

  • FastAPI: FastAPI is a modern, high-performance web framework for building APIs with Python.
  • UVicorn: UVicorn is a fast ASGI server for running Python web applications, designed for high performance and compatibility with asynchronous code.
  • fastapi-pagination: FastAPI-Pagination is a Python library for effortless pagination of FastAPI endpoints.
  • SQLAlchemy: SQLAlchemy is a Python library that provides a high-level, object-oriented interface for interacting with relational databases using SQL.
  • Faker: The Faker library is a Python tool for generating fake data, such as names, addresses, and text, often used for testing and populating databases.

Open PyCharm CE on your desktop and click on ‘New Project’.

How to Implement Pagination Using FastAPI in Python?

Name your project and choose the path to save your project and then click on ‘create’.

How to Implement Pagination Using FastAPI in Python?

Remove all the auto-generated code from the main.py file.

Open the PyCharm terminal and run the command as shown in the image below to install the FastAPI framework.

On successful installation you will see the screen as shown in the image below.

Run the command as shown in the image below to install the UVicorn server.

On successful installation you will see the screen as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Let’s create a demonstration API route and perform testing with FastAPI.

Import the FastAPI class as shown in the image provided.

Instantiate the FastAPI application using the FastAPI class as shown in the provided image.

Create a root route at ‘/’ that provides a placeholder message in response.

The final code will look as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Initiate the server using the command demonstrated in the provided image.

After running the command, the server will get started and you will see a screen as shown in the image below.

Click on the link shown in the terminal to see the message that we returned from our code.

If everything goes well, you will be able to see a message on your browser as shown in the image below.

Let’s start with the Database preparation. 

Create a table schema using the command shown in the image below.

Our schema will look like as shown in the image below.

Let’s connect the MySQL database to our python application.

For that, first we need to install the SqlAlchemy library. 

Run the command as shown in the image below to install the library.

On successful installation you will see the screen as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Right click on the folder structure and create a new python file.

Name the file as db_connection and save the file.

Write the below provided code in the db_connection.py file and make sure to replace the username, password and databasename placeholder to its actual values.

How to Implement Pagination Using FastAPI in Python?

Let’s populate some fake data into our MySQL table.

For that first we need to install the faker library, run the command shown in the image below to install the required library.

On successful installation you will see the screen as shown in the image below.

Right click on the folder structure and create a new python file.

Name the file as insert_fake_data and save the file.

Write the code shown in the image below in the insert_fake_data.py file.

How to Implement Pagination Using FastAPI in Python?

Write the command shown in the image below to the terminal to run this file.

You may get the below error after running the above shown command.

To resolve the error, we will have to download the pymysql library.

Run the command shown in the image below to download the same.

On successful installation you will see the screen as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Run the insert_fake_data file again using the command provided earlier.

You will get the below error. 

This error is due to the country name Cote d’Ivoire. 

As you can see this country name has a single quote so MySQL is getting confused and throwing the error.

To resolve this error, we will make use of urllib library’s quote method.

Import the method as shown in the image below.

Wrap the loc variable in the quote method to parse the string correctly and avoid the error.

The final code in the insert_fake_data.py file will be as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Now run the file again and it should run without any problems.

Check the data in the table using the below mysql query.

After executing the query, we will get the output as shown in the image below.

Let’s write a complete end-to-end FastAPI route to get the rows from our employee table as a HTTP response.

Right click on the folder structure and create a new python file.

Name the file as employee_model and save the file.

How to Implement Pagination Using FastAPI in Python?

Write the provided code into the employee_model.py file.

This code defines the structure of the employee table in SQLAlchemy, which we’ll use to interact with a MySQL database using FastAPI.

Open main.py file and modify its code to match the code shown in the image below.

Open your browser and hit the endpoint to see all the employee’s data.

http://localhost:8000/.

How to Implement Pagination Using FastAPI in Python?

Now, let’s add pagination to the endpoint.

To implement pagination, we need to install fastapi-pagination library.

Run the command as shown in the image below to install the library.

On successful installation you will see the screen as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

We’ve successfully installed the pagination library, and now we need to create a response model called “EmployeeOut,” which will mirror the Employee model, but without any sensitive information (though we don’t have any), and we’ll use it to send HTTP responses.

Add the below lines of code in the main.py file.

Modify the main.py file code to match the code shown in the image below.

We use the Page and paginate methods. 

The default settings are a minimum of 50 items per page and at least 1 page, but you can change the page number and size by passing parameters to adjust the response.

Let’s open the browser and hit the endpoint again to see the output as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

By default, our endpoint returns a list of 50 records from a database that contains a total of 1000 records.

If you want to receive only 10 records instead, you can specify this by using query parameters.

Pagination with custom page size parameters:

Hit the endpoint using the url shown in the image below to get only 10 records per page.

After hitting the above endpoint, we get output as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Pagination with Limit and Offset:

If you want to control how many items you see at once or split a large list into smaller parts, you can use Limit/Offset Pagination. 

You can do this by importing LimitOffsetPage and using it to organize your data instead of the regular Page.

Here’s what it does:

Limit: It helps you set a maximum number of items to show at a time. For example, you can choose to see only 10 items at once.

Offset: This lets you skip some items at the beginning of your list. For instance, if you skip the first 5 items, you’ll start viewing the list from the 6th item onwards.

By combining limit and offset, you can both skip certain items and limit how many you see in one go.

This is handy when you’re dealing with a lot of data and want to break it into manageable chunks.

Modify the main.py file code to match the code shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Let’s assume we want to query rows from 10 to 15. We could use the limit and offset to do this.

Hit the endpoint using the url shown in the image below to get the records from 10 to 15.

After hitting the above endpoint, we get output as shown in the image below.

How to Implement Pagination Using FastAPI in Python?

Conclusion

In this hands-on tutorial, we have successfully implemented pagination in FastAPI using the fastapi-pagination library. 

We began by creating a new project in the PyCharm IDE and installing the necessary libraries. 

Our journey started with setting up FastAPI in the project and creating a test route to verify our FastAPI setup.

We then meticulously planned the SQL schema for the data we intended to paginate. 

Following that, we established a connection to our MySQL database using Python code. 

To populate our MySQL table with dummy data, we leveraged the faker library. 

Afterward, we created a route to retrieve this data, and we seamlessly incorporated pagination into this endpoint using the fastapi-pagination library.

Throughout the process, we explored different methods of using pagination to suit different use cases. 

Finally, we conducted tests to ensure that our code functions as intended and consistently produces the expected output. 

We will come up with more such use cases in our upcoming blogs.

Meanwhile…

If you are an aspiring Python developer and want to explore more about the above topics, here are a few of our blogs for your reference:

Stay tuned to get all the updates about our upcoming blogs on the cloud and the latest technologies.

Keep Exploring -> Keep Learning -> Keep Mastering 

At Workfall, we strive to provide the best tech and pay opportunities to kickass coders around the world. If you’re looking to work with global clients, build cutting-edge products, and make big bucks doing so, give it a shot at workfall.com/partner today!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *