Creating a WebApp using Flask+Gunicorn on Ubuntu Server: End-to-End Series (Part — 4)

Accredian Publication
4 min readMar 12, 2021


By Hemanka Sarmah

Any Data Science project is incomplete without testing the model and deploying it to production.

What is Flask?

  • Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.
  • It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.
  • Flask depends on the Jinja template engine and the Werkzeug WSGI toolkit.

Need for Gunicorn

  • Python Web Server Gateway Interface (WSGI) HTTP server.
  • It is a pre-fork worker model, ported from Ruby’s Unicorn project.
  • Gunicorn server is broadly compatible with a number of web frameworks, simply implemented, light on server resources, and fairly fast.
  • It does not support windows but will work on a Linux machine.
  • It helps complete one of the major drawbacks of the flask web app.
  • Flask web app is only restricted to one request at a time, all other request goes into waiting or are discarded.
  • Gunicorn distributes the load into multiple threads in which multiple workers can run parallelly.
  • The optimum amount of workers to be launched depends on the core of the server.
    workers = (2*number of cores) +1

Also, Check out our Article on:

  1. Data Abstraction: End-to-End Series (Part — 1)
  2. Data Preprocessing and EDA: End-to-End Series (Part — 2)
  3. Model Building and Experimentation: End-to-End Series (Part — 3)
  4. Containerizing the WebApp using Docker: End-to-End Series (Part — 5)
  5. Scaling our Docker Container using Kubernetes: End-to-End Series (Part — 6)
  6. Automating building and deployment using Jenkins: End-to-End Series (Part — 7)

Creating a Python Script using Flask

  • Create a file
from flask import Flask, render_template, request
import pickle
import numpy as np
app = Flask(__name__)model = pickle.load(open("randomforest.pkl", "rb"))@app.route('/')
def man():
return render_template('home.html')
@app.route('/predict', methods = ['POST'])
def home():
tenure = request.form['a']
PS = request.form['b']
C = request.form['c']
PB = request.form['d']
PM = request.form['e']
MC = request.form['f']
array = np.array([[tenure, PS, C, PB, PM, MC]])
pred = model.predict(array)
return render_template("result.html", data = pred)
if __name__ == "__main__": = '',port = 8000,debug = True)

Creating an HTML File

→ Home Template

  • Create a home.html file
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Churn Prediction</title>
<body bgcolor=#F0F8FF>
<center><img src="static\LOGO.png" align="middle"></center><table style="width:100%">
<img src="static\customer_churn.jpg" align="middle" width="600" height="400">
<h1 style="font-family:verdana;font-size:250%;color:Red;"> Customer Churn Prediction</h1><br><form method="POST" action = "{{url_for('home')}}">
Tenure of the Customer: <input type="text", name="a", placeholder="enter 1"/><br><br>
Phone Service: <input type="text", name="b", placeholder="enter 2"/><br><br>
Contract: <input type="text", name="c", placeholder="enter 3"/><br><br>
Paperless Billing: <input type="text", name="d", placeholder="enter 4"/><br><br>
Payment Method: <input type="text", name="e", placeholder="enter 5"/><br><br>
Monthly Charges: <input type="text", name="f", placeholder="enter 6"/><br><br><br></b>
<button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
<marquee><i>International School of AI and Data Science</i></marquee>

→ Result Template

  • Create a result.html file
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<body bgcolor=#ADD8E6>
<h1 style="font-family:verdana;font-size:350%;"> Prediction</h1>
{%if data == 0%}
<h1 style="font-family:verdana;font-size:250%;color:Green;"> Customer will Not Churn</h1>
<img src="static\not_churn.gif">
<h1 style="font-family:verdana;font-size:250%;color:Red;"> Customer will Churn</h1>
<img src="static\churn.gif">
<a href="/">go back to home page</a>


  • After running the app go to your browser and on the address bar write localhost:8000 and press enter

Connecting to server

  • In order to connect to any server remotely in the windows machine, we will be using a bitwise SSH client.
    You download it from this link.
  • Open the bitwise SSH client and log into your server.
    I am using server IP, username, and password to log into the server.
  • Then open a terminal and update the ubuntu using the command
  • Create a directory using the command
  • Then on the bitwise SSH click on SFTP to transfer files from the local system to the server.
  • After the files have been copied to the server we need to start installing the libraries.
  • After installing the libraries we can now run the flask app on the server.
  • Once the app starts running. Go to your browser type in the server IP followed by port.
  • You can now access your web app from anywhere in the world.

Running the app using Gunicorn

→ Installing Gunicorn in the ubuntu server

sudo apt install gunicorn

→ Create a file

The file will act as a gateway for gunicorn to access your app, to create a py file on server type:

  • nano and press enter
  • Then write the following code and press Ctrl + X.
from flaskapp import appif __name__ == “__main__”:
  • Once the is created, on the Terminal type
gunicorn -w 5 --threads 3 --bind wsgi:flaskapp
  • You have now launched your web app using gunicorn on your server.

Follow us for more upcoming future articles related to Data Science, Machine Learning, and Artificial Intelligence.

Also, Do give us a Clap👏 if you find this article useful as your encouragement catalyzes inspiration for and helps to create more cool stuff like this.

Visit us on



Accredian Publication

One of India’s leading institutions providing world-class Data Science & AI programs for working professionals with a mission to groom Data leaders of tomorrow!