Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Flask Project to Upload CSV File Data in MySQL Table Using Pandas Library in Browser

Posted on December 27, 2022

 

 

Welcome folks today in this blog post we will be uploading csv file data and importing it inside the mysql table in flask using pandas library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below libraries using the pip command as shown below

 

 

pip install flask

 

 

pip install pandas

 

 

After this you need to see the directory structure of the flask app is shown below

 

 

 

 

First of all as you can see you need to create the uploads directory where we will be storing the uploaded files inside the browser. And also we need to create the templates folder and inside it we need to create the index.html file which will have the simple html5 form where user can upload csv files.

 

Now first of all you need to create the app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from flask import Flask, render_template, request, redirect, url_for
import os
 
import pandas as pd
import mysql.connector
 
app = Flask(__name__)
 
# enable debugging mode
app.config["DEBUG"] = True
 
# Upload folder
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] =  UPLOAD_FOLDER
 
 
# Database
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="exceldb"
)
 
mycursor = mydb.cursor()
 
 
if (__name__ == "__main__"):
     app.run(port = 5000)

 

 

As you can see we are importing the flask library and also we are importing the mysql driver library for connecting to the database using the username and password and also the database name. And also we are setting the upload folder path. And lastly we are starting the flask app at the port number 5000.

 

 

Creating the Table

 

 

After you install this library you need to start the xammp control panel and start the apache server and mysql server as well

 

 

 

 

Now you need to create the table inside the phpmyadmin as shown below

 

 

 

 

 

As you can see we have the four fields inside the user table. ID is the primary key of the table and then we are storing the name,age and country of the user.

 

 

Now we will be defining the get route to load the index.html file as shown below

 

 

Python
1
2
3
4
5
# Root URL
@app.route('/')
def index():
     # Set The upload HTML template '\templates\index.html'
    return render_template('index.html')

 

 

As you can see we are using the render_template() method to load the index.html file inside the browser.

 

Now we need to create the templates folder and inside it you need to create the index.html file as shown below

 

 

templates/index.html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
<!doctype html>
<html>
  <head>
    <title>FLASK CSV File Upload</title>
  </head>
  <body>
    <h1>Upload your CSV file</h1>
    <form method="POST" action="" enctype="multipart/form-data">
      <p><input type="file" name="file"></p>
      <p><input type="submit" value="Submit"></p>
    </form>
  </body>
</html>

 

 

As you can see we have the html5 form where we have the input field where we allow the user to upload the csv file and then we have the button to submit the form. Now we need to define the post route at the / path as shown below

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
@app.route("/", methods=['POST'])
def uploadFiles():
      # get the uploaded file
      uploaded_file = request.files['file']
      if uploaded_file.filename != '':
           file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file.filename)
          # set the file path
           uploaded_file.save(file_path)
           parseCSV(file_path)
          # save the file
      return redirect(url_for('index'))

 

 

As you can see inside the post request we are getting the selected file by the user and then we are uploading that csv file to the uploads folder and then we are calling the parseCSV() method which will parse the csv file and insert to mysql table.

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
def parseCSV(filePath):
      # CVS Column Names
      col_names = ['Name','Age','Country']
      # Use Pandas to parse the CSV file
      csvData = pd.read_csv(filePath,names=col_names, header=None)
      # Loop through the Rows
      for i,row in csvData.iterrows():
             sql = "INSERT INTO users (Name,Age,Country) VALUES (%s, %s, %s)"
             value = (row['Name'],row['Age'],row['Country'])
             mycursor.execute(sql, value)
             mydb.commit()

 

 

As you can see we are defining the column names inside an array. And after that we are using the read_csv() method to extract the data from the csv file and then storing it inside the array. And then we are using the for loop to insert each row to the mysql table using the INSERT statement.

 

 

Full Source Code

 

 

Wrapping it up this is the full source code of the app.py file as shown below

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, render_template, request, redirect, url_for
import os
 
import pandas as pd
import mysql.connector
 
app = Flask(__name__)
 
# enable debugging mode
app.config["DEBUG"] = True
 
# Upload folder
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] =  UPLOAD_FOLDER
 
 
# Database
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database="exceldb"
)
 
mycursor = mydb.cursor()
 
 
# Root URL
@app.route('/')
def index():
     # Set The upload HTML template '\templates\index.html'
    return render_template('index.html')
 
 
# Get the uploaded files
@app.route("/", methods=['POST'])
def uploadFiles():
      # get the uploaded file
      uploaded_file = request.files['file']
      if uploaded_file.filename != '':
           file_path = os.path.join(app.config['UPLOAD_FOLDER'], uploaded_file.filename)
          # set the file path
           uploaded_file.save(file_path)
           parseCSV(file_path)
          # save the file
      return redirect(url_for('index'))
 
def parseCSV(filePath):
      # CVS Column Names
      col_names = ['Name','Age','Country']
      # Use Pandas to parse the CSV file
      csvData = pd.read_csv(filePath,names=col_names, header=None)
      # Loop through the Rows
      for i,row in csvData.iterrows():
             sql = "INSERT INTO users (Name,Age,Country) VALUES (%s, %s, %s)"
             value = (row['Name'],row['Age'],row['Country'])
             mycursor.execute(sql, value)
             mydb.commit()
 
if (__name__ == "__main__"):
     app.run(port = 5000)

 

 

Now you need to create the file.csv which will hold sample data records as shown below

 

 

file.csv

 

 

1
2
3
4
Gautam,34,India
John,24,New Zealand
Harris,25,USA
Harry,56,England

 

 

Now if you execute the flask app in the terminal as shown below

 

 

python app.py

 

 

It will start the app at port 5000

 

 

 

Recent Posts

  • Angular 14/15 JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • Build a JWT Login & Registration Auth System in Node.js & Express Using MongoDB in Browser
  • React-Admin Example to Create CRUD REST API Using JSON-Server Library in Browser Using Javascript
  • Javascript Papaparse Example to Parse CSV Files and Export to JSON File and Download it as Attachment
  • Javascript Select2.js Example to Display Single & Multi-Select Dropdown & Fetch Remote Data Using Ajax in Dropdown
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme