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/