Avatar of condor888
condor888
 asked on

How to implement server side pagination with Flask and DataTables

My application is written with the Flask framework. I also employed jQuery DataTables to draw tables. However, how can I do a server side pagination with Flask and jQuery DataTables? I found a cgi solution but doesn't seem to fit my application well. I'd like DataTables to use Flask HTTP APIs via Ajax.
jQueryAJAXPythonWeb Development

Avatar of undefined
Last Comment
Scott Fell

8/22/2022 - Mon
Scott Fell

I do not know python, but for datatables you want to use ajax https://datatables.net/examples/ajax/index.html
condor888

ASKER
Yes, I understand, but I'd like to do server side pagination with Ajax, any idea? Thanks!
Scott Fell

The way it works is if your server side outputs 500 records, then that 500 gets sent to the client and the pagination is based on the 500 records. You can use the datatables api to create the increments of how many records to show at a time (10, 20, 50, 100, all etc)

If you have a very large amounts of data, then you will still use ajax, but set serverside processing to true.  http://datatables.net/release-datatables/examples/data_sources/server_side.html
With server-side processing enabled, all paging, searching, ordering actions that DataTables performs are handed off to a server where an SQL engine (or similar) can perform these actions on the large data set (after all, that's what the database engine is designed for!). As such, each draw of the table will result in a new Ajax request being made to get the required data.

Another option is to have a query that determines total amount of records (or cache that).  let's say you have 100,000 records and you only want to push 500 out at a time.  Then create server side generated pagination for the 200 pages.

I think the built in server side function for datatables is the way to go.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
condor888

ASKER
Hi Scott, I went to the doc page that you pointed to. But it seems that the PHP code is missing... Is there any code samples to demo how it works?

In my case, I have a huge dataset, so only part of the data should be returned each time. Thanks!
ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
condor888

ASKER
Hi Scott, sorry about the late reply. I checked the code that you sent. It's rather a server-side processing than a server-side pagination. So I am afraid this is not a proper example for the pagination...
Scott Fell

Actually, it is both.

Datatables will take in a specified number of records when you use server side processing/ajax. (See "Draw" in the api https://www.datatables.net/manual/server-side) and as needed will grab more data.

If you have 500 records and you tell datatables to takin all 500, then the pagination is built into datatables and his handled by the client. You can even select the number of rows to show at a time (10, 20, 50, all etc) and that creates your pages.

If you have 10,000 records and set 100 to come in at a time, datatables will automatically cache 100 at a time.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.