Python Tutorial 65 – Flask Search Bar Plus Dynamic Data Table Return Using Pandas and Python

A search box or search bar is a popular function applied on Web applications for visitors to look up things you are finding. A great UX search function can hugely shorten the information search journey and enhance the user experience. In this article, I will go through how to set up a fundamental search function using Flask and return data in the panda’s data frame.

flask search bar

A search box or search bar is a popular function applied on Web applications for visitors to look up things you are finding. A great UX search function can hugely shorten the information search journey and enhance the user experience. In this article, I will go through how to set up a fundamental search function using Flask and return data in the panda’s data frame.

Ingredients on Flask Search Bar Plus Dynamic Data Table Return Using Pandas

Python3, Flask, Jinja2, JSON, Pandas

Table of Contents

Search Bar

Adding a search bar needs a form method and search type of input method added in the clientside HTML script folder. In Flask, we need to create a new HTML script saved in the template folder. Here are the codes as follows:

<form action="symbolResult" method="POST">

     <input type="search" id="symbol" name="symbol" class="form-control" placeholder="symbol" />

        <div align="center">

            <button class="btn btn-primary" type="submit" value="search" , id="search">Search</button>

        </div>

</form>

Please be sure to check two variable values. One is to add the route name “symbolResult” in the action parameter which is the same as the Flask route name you create in the Python script file. 

The other is the input id value must be the same as the form name value on the server side. I will elaborate more in a moment.

Search Result and Easy2Digital API

After having created the search bar page or a search bar function embedded on any pages you like. Now we need to develop a search result data return after any users search. Basically, the data communication includes 2 sections – Server Side and Client Side

Server Side – Python, Pandas

First thing first, we need to create a route to return the search result to the user. The route name here must be the same as the client-side HTML form action name. It’s for connecting them together.

flask search bar

Then, we need to create a form variable to capture the keyword users’ input to look up information. And the value in the get() must be the same as the ID in the form of the client side mentioned above.

Additionally, based on the keyword, we can use it to connect our database. Here I take Easy2Digital API for example and I select the financial ratio one. If you are interested in the Easy2Digital API collection. Please check out more details on this page.

We use the request methods and add related parameters in the Easy2Digiatal API endpoint to fetch the data related to the keyword from the search bar.

Last but not least, for returning a table of data to display in the search result, we can leverage the panda’s data frame and the code sample is as follows:

  • financialRatioData is for adding the table headers
  • Row_data is for adding the value from each header

Return render_template(“abc.html”, 

financialRatioData=df.columns.values,

row_data=list(df.values.tolist())

Client Side – Html, CSS, Jinja

When the server-side code and route are ready, it returns the information related to the search keyword back to the client side. The question is how the client side in HTML, and CSS can display the data from the server side. Jinja2 and for loop, zip() methods can be used in this case. Here are the codes as follows:

   <table class="table table-responsive-sm table-striped" style="width: 100%">

            <thead class="table-dark">

                <tr>

                    {% for header6 in financialRatioData %}

                    <th>{{ header6 }}</th>

                    {% endfor %}

                </tr>

            </thead>

            <tbody>

                {% for row6 in row_data %}

                <tr>

                    {% for col6, row_6 in zip(financialRatioData, row6) %}

                    <td>{{row_6}}</td>

                    {% endfor %}

                </tr>

                {% endfor %}

            </tbody>

        </table>

Full Python Script of Web App Search Bar Using Flask and Pandas

If you are interested in the Easy2Digital API, and the full python script of Flask Search Bar Plus Dynamic Data Table Return Using Pandas and Python, please subscribe to our newsletter by adding the message “Python Tutorial 65, API Number”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Python Tutorial 65 – Flask Search Bar Plus Dynamic Data Table Return Using Pandas and Python. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

Leave a Reply

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