How to use sqlalchemy core for pagination in Flask?
<p>I am using flask framework with <strong>SQLAlchemy core</strong> only. My result set returned from the 'select' statement contains few thousands of records. I would like to use pagination to avoid memory error. How can I get a JSON output using cursor for api GET method, for any page dynamically? I have tried the accepted solution here <a href="https://stackoverflow.com/questions/4840731/how-to-use-cursor-for-pagination">How to use cursor() for pagination?</a>, but could not quite get it right. Please guide. Using PostgreSQL 9.4
Options I have tried:</p>

<p>1.</p>

<pre><code>@app.route('/test/api/v1.0/docs_page/&lt;int:page&gt;', methods=['POST'])
def search_docs_page(page):
    if 'id' in session:
        doc_list = []
        no_of_pgs = 0
        doc_list = common_search_code()
        # doc_list is a resultset after querying the table for a set of query condition  
        header_dict = dict(request.headers)
        for i in header_dict.items():
            if i == 'Max-Per-Page':
                max_per_pg = int(header_dict[i])
        no_of_pgs = len(doc_list) / max_per_pg
        print 'number of doc: ' + str(len(doc_list))
        print 'number of pages: ' +  str(no_of_pgs)
        print 'max per page:' + str(max_per_pg)
        print 'page number: '+ str(page)
        page = int(request.args.get(page, type=int, default=1))
        pagination = Pagination(page=page,
                                total=len(doc_list),
                                search=True, record_name='documents')

        return jsonify({'pagination': list(pagination.__dict__),
                        'doc_list': doc_list}), 200

    return jsonify({'message': "Unauthorized"}), 401
</code></pre>

<p>I want to control the number of records to be printed in each page by passing the parameter in the request header as in this curl:</p>

<p>curl -b cookies.txt  -X POST <a href="http://localhost:8081/test/api/v1.0/docs_page/1" rel="nofollow noreferrer">http://localhost:8081/test/api/v1.0/docs_page/1</a> -H 'max_per_page:4' -H 'Content-type:application/json'</p>

<pre><code>@app.route('/test/api/v1.0/docs_page2', methods=['POST'])
 def search_docs_page2():
     if 'id' in session:
         docs = []
         no_of_pgs = 0
         doc_list = common_search_code()
         header_dict = dict(request.headers)
         for i in header_dict.items():
             if i == 'Max-Per-Page':
                 max_per_pg = int(header_dict[i])
         no_of_pgs = len(doc_list) / max_per_pg
         print 'number of doc: ' + str(len(doc_list))
         print 'number of pages: ' +  str(no_of_pgs)
         print 'max per page:' + str(max_per_pg)
         page = int(request.form.get('page', type=int, default=1))
         cursor = request.form.get('cursor')
         if cursor:
             print 'hey'
             doc_list.with_cursor(cursor)
             docs = doc_list.fetchmany(4)
             for r in docs:
                 print r, 'rrrr'

         return jsonify({'docs': docs}), 200

     return jsonify({'message': "Unauthorized"}), 401







curl:curl -b cookies.txt  -X POST http://localhost:8081/test/api/v1.0/docs_page2 -H 'max_per_page:4' -H 'Content-type:application/json' -F 'cursor=1'
</code></pre>