1

I'm currently trying to build a small flask app in python which takes in a csv via pandas and paginates the results. I've already finished the process of importing the dataset with pandas, converting it to a list of dictionaries and paginating the results into a table using flask_paginate.

Now, I'd like to extend this code to allow querying of terms in the dataset for example (show and paginate all results where 'Customer Name' contains 'Barry'). I'm aware ill need to use an input box and process a post request but I am very unsure of how to wire it all together in the index route.

Any help would be greatly appreciated thanks.

Code for app.py

from flask import Flask, render_template
from flask_paginate import Pagination, get_page_args
import pandas

app = Flask(__name__)
app.debug = True

df = pandas.read_csv('static/superstore.csv')
data = df.to_dict(orient = 'records')

def get_users(offset=0, per_page=20):

    return data[offset: offset + per_page]

@app.route('/')
def index():

    page, per_page, offset = get_page_args(page_parameter='page', per_page_parameter='per_page')

    total = len(data)

    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',
                           data=pagination_users,
                           page=page,
                           per_page=per_page,
                           pagination=pagination)

Code for index.html

<!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">

    <link rel="stylesheet" type="text/css" href="static/main.css">
  </head>
  <body>
    <div class = "bg-primary">
    <div class = "container jumbotron jumbotron-fluid bg-transparent text-white">
      <h1 class = "display-4">Pagination Example</h1>

    </div>
  </div>
    <div class="container">
      <div class="table-responsive">
        <table class="table table-hover">
          <thead>
            <tr>
              <th>Order ID</th>
              <th>Order Date</th>
              <th>Profit</th>
              <th>Unit Price</th>
              <th>Order Quantity</th>
              <th>Customer Name</th>
              <th>Region</th>
            </tr>
          </thead>
          <tbody>
            {% for item in data %}
            <tr>
              <td>{{ item['Order ID'] }}</td>
              <td>{{ item['Order Date'] }}</td>
              {%if item['Profit'] > 0%}
                <td><span class ="badge badge-success">{{ item['Profit'] }}</span></td>
              {%else%}
                <td><span class ="badge badge-danger">{{ item['Profit'] }}</span></td>
              {%endif%}

              <td>{{ item['Unit Price'] }}</td>
              <td>{{ item['Order Quantity'] }}</td>
              <td>{{ item['Customer Name'] }}</td>
              <td>{{ item['Region'] }}</td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      {{ pagination.links }}
    </div>
  </body>
</html>

Screenshot: Screenshot of localhost:5000/

Danielc92
  • 11
  • 1
  • 3
  • i think something like a search box on top of paginated results would be suffice..https://stackoverflow.com/questions/12433304/live-search-through-table-rows this might help.. – iamklaus Sep 25 '18 at 04:56
  • I don't know if you are using it, but `flask-sqlalchemy` offers a way to return paginated data with `paginate()` function. You can easily query your data with it, and still use `flask-paginate` for pretty printing. – SivolcC Sep 25 '18 at 05:42
  • Thanks, i am not using flask-sqlalchemy at the moment. I wanted to get a basic working example minimally using a list of python dictionaries, is it possible to handle a list of dictionaries or pandas dataframe using flask-sqlalchemy or must you have a database in place? Thanks – Danielc92 Sep 25 '18 at 05:47
  • My bad, my comment was totally irrelevant. I have never worked with pandas dataframes and sqlalchemy is probably not compatible at all with it. Have a read at this, https://stackoverflow.com/questions/29525808/sqlalchemy-orm-conversion-to-pandas-dataframe but I no idea if sqlalchemy would be a wise choice anyway. – SivolcC Sep 25 '18 at 05:57
  • I was more looking for an example flask app that combines pagination and querying. I found many examples out there, but they do these two things in separation and I need to do them simultaneously. flask-sqlalchemy may be helpful but im trying to keep this app minimal as possible using only flask-paginate and basic list of dictionaries. – Danielc92 Sep 25 '18 at 06:04

0 Answers0