Skip to content
Related Articles

Related Articles

Convert CSV to HTML Table using Python Pandas and Flask Framework

View Discussion
Improve Article
Save Article
  • Last Updated : 26 May, 2022

In this article, we are going to convert a CSV file into an HTML table using Python Pandas and Flask Framework.

Sample CSV file :


Stepwise Implementation

Creating Environment

Step 1: Create an environment. Create a project folder and a venv folder within.

py -3 -m venv venv

Step 2: Activate the environment.


Step 3: Install Flask and Pandas.

pip install Flask

pip install pandas

Creating Project

Step 1: Create ‘’ folder and write the code given below.


# importing flask
from flask import Flask, render_template
# importing pandas module
import pandas as pd
app = Flask(__name__)
# reading the data in the csv file
df = pd.read_csv('sample_data.csv')
df.to_csv('sample_data.csv', index=None)
# route to html page - "table"
def table():
    # converting csv to html
    data = pd.read_csv('sample_data.csv')
    return render_template('table.html', tables=[data.to_html()], titles=[''])
if __name__ == "__main__":"localhost", port=int("5000"))

Step 2: Create the folder ‘templates’. create the file ‘table.html’ inside the ‘templates’ folder.


<!DOCTYPE html>
<html lang="en">
        <title> Table </title>              
        <div align="center">
                <!--Displaying the converted table-->
                     {% for table in tables %}
                    {{ table|safe }}
                    {% endfor %}     

Step 3: Add the ‘sample_data.csv’ file.

Step 4: The project structure will look like this.

Structure of  the project

Run the Project

Step 1: Run the server.

Step 2: Browse the URL ‘localhost:5000’.

Step 3: The output web page will be displayed.


Output : CSV to HTML Table

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!