Using Your Machine Learning Model with Web/Mobile apps. (Flask REST API)

Showing off how good your Machine Learning model works using a Colab/Jupyter notebook gets old. The output comes out in a very unpresentable form. If it’s an image then Matplotlib is probably not the best way you’d want to show it to other people, specially non-coders. Having a working application helps you understand the real-life use of your model and how it performs.

Using pickle and flask, you can make a REST API with a few simple steps and use the JSON responses in any application you might want to develop.

What is a REST API?

A REST API (Representational State Transfer) is a simple architectural style for an API that communicates using HTTP requests. Basically, you can use a certain architecture, mostly JSON, to communicate between two applications (Client and Server). You can read about JSON and REST API on the hyperlinks.

Let’s get started, shall we?

First, we need to save our existing model as a pickle file so we can use it with our web app. Pickle is a python library that basically saves a Python object as a file using something called object serialization. Pickle allows you to save a python object with all its data/weights and use it anywhere else. You can read more about pickle here.

  1. Go to your existing Python Notebook where you have originally trained your model. Pickle is bundled with the default Python package, so you don’t need to install it.

2. Now we just dump your model object into a .pickle file. Later we can load this file in our Flask app and use it there. Run this code next to save your model.

import pickle
# you can name your model anything. I'm just using model for now.
with open('model.pickle', 'wb') as f:
# picke.dump takes the model and the file objects respectively
pickle.dump(model, f)

This should give you a pickle file that can now be used in the Flask application.

Now, we have to create a Flask app. We already have your machine learning model saved and ready to be used in the Webapp.

  1. You can set up a new python environment or you could use an existing one. Using a new environment is mostly better for web apps because it’s easier to keep track of all the libraries/frameworks you’re using and use specific versions of some frameworks if needed.

If you haven’t used a virtual environment before, you can read about them here.

There are several ways you can create a python virtual environment. I like to use Virtualenv. First, make a project directory and open a terminal there. To install Virtualenv and make a new Virtual Environment, run these commands in the terminal (env is the name of the environment and it could be anything):

pip install virtualenv
virtualenv env

If you have multiple Python installations, then use this:

py -3 -m pip install virtualenv
py -3 -m virtualenv env

Now, you just have to activate your virtual environment.


If this worked, you’ll see your environment’s name in paratheses in front of the terminal path. Like this:

2. Now we just to have install Flask(This is a new environment) and we’re good to go. Run this in a terminal with the environment activated(read the step above):

pip install flask

The basic setup is done now.

You would also need the library from which you imported the model object. I have used ‘sklearn’ here, so I need to install ‘sklearn’ as well.

pip install scikit-learn

You should install other libraries now if you need them like NumPy, Pandas (Most of the things you’d need are already bundled with scikit-learn).

3. Next, create a python file that would act as the main Flask file. Using an IDE from here will make this whole process easier. I prefer Microsoft VS Code, which can be found here.

You can name it anything but the standard/preferred name is “”.

4. Now we have to import ‘Flask’, ‘jsonify’ and ‘request’ (Flask is the flask module, jsonify is used to return JSON responses and ‘request’ is used to handle request data received from the client) from the flask module and pickle.

from flask import Flask, jsonify, request
import pickle

5. Next is initializing the Flask app.

app = Flask(__name__)

The idea of the first parameter is to give Flask an idea of what belongs to your application. This name is used to find resources on the filesystem, can be used by extensions to improve debugging information and a lot more.

So it’s important what you provide there. If you are using a single module, `__name__` is always the correct value. If you, however, are using a package, it’s usually recommended to hardcode the name of your package there.

There are two routes given below to give you an Idea of how flask routes work and how you can build a feature-rich backend for your Machine Learning model

6. Let’s make the home route first. Home route deals with any HTTP requests sent to the home (‘/’) route. For example, if your website is “”, then the home route would be “”.

@app.route("/", methods=["GET"])
def home():
return "<center><h1> This is the Home Route </h1></center>"

“@app.route” is an example of a decorator. A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

Here, it basically tells Flask that the function below this is a route function. It takes two arguments: Route and Methods. The route is the postfix to your backend domain and methods are HTTP request methods you might use with this route. This is a great resource to read more about HTTP requests and their uses.

In the return statement, you can return any HTML code as a string and the browser will interpret it as HTML code.

7. Next, we can add our main route that would handle POST requests from the web app, use the model to generate an output and return it as JSON.

@app.route("/predict", methods=["GET", "POST"])

You can create your own endpoints after ‘/’. I used ‘predict’ here as an example.

Now, we can create the function and load up the pickle file we made earlier.

Remember to put the ‘.pickle’ file in the same directory as your ‘’ file or copy the correct relative path. Make sure it’s in your project folder because it will make hosting the application easier.

def predict():
with open("model.pickle", "rb") as f:
model = pickle.load(f)

We use “rb” (read binary) mode while reading the file because ‘pickle.load’ function requires a binary file object.

This step gives you your model back as it was before we saved it. Now you can send post requests from your client-side and handle them here.

If you want to test your server locally, you’d have to enable CORS. Enabling CORS in flask is very easy can be done with these easy steps.

You can access form data or JSON data sent by the Client using the request context.

# this gives you the form data, if any, in the HTTP request as a
# python dictionary
# this gives you any json data in the HTTP request

Returning JSON data is also very easy. You can make a dictionary of your response and return it using jsonify.

return jsonify({"response": "success"})

If you want to handle different HTTP requests at the same endpoint/route, you can use the following code in if-else to perform different functions for different types of HTTP requests on the same route.

if request.method == 'POST':
# Statements concerning a POST request
elif request.method == 'GET':
# Statements concerning a GET request

8. Finally, we just need to start our server using the ‘’ method. Put this at the end of your file. You can also specify a port in the run function but remember to remove that from your code while deploying it online because hosting services assign their own port numbers.

if __name__ == "__main__":

9. You can now run your python file and you should be ready with your very own flask server that uses a Machine Learning Model.

Run this in a terminal with the virtual environment activated.


Now, you can easily host your server online on services like Heroku. This is a good resource to get started.

You can use extend this application to do many things and make a fully-fledged application. You can add more endpoints according to your needs and handle other types of HTTP requests.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build Groph Step by Step

Install Google chrome in Ubuntu 20.04.

Final Sprint! Refactoring and Debugging!

Go With The Flow — Building My First Music App with Django (Part 1)

SSH Tunnelling and port forwarding

Python Objects Part III: String Interning


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gautam Jain

Gautam Jain

More from Medium

About Kubernetes architecture 3; Debugging

Kruskal’s Algorithm

Remote deployment with RMI

Revision of shallow machine learning algorithms: Clustering — part 2