Deploy your ML model with Flask: Setup and Demo Project

Kousthubha Krishna
Analytics Vidhya
Published in
4 min readAug 20, 2020

In this article, I am showing you what I feel is a simple, easy setup to begin a flask project and some code to start building in Flask. I am using Visual Studio Code for this project.

Initial Setup

I am creating a virtual environment to develop my project. If you don’t want virtual env, you can go ahead and use the global system space.

By using venv in python library we can create a virtual environment. The command python -m venv (name of env) creates a virtual environment with the specified name. Here it is “venv” itself. Now we have to activate the environment with venv\Script\activate command. Once activated we can see our env name in green color as shown. To deactivate we just need to use deactivate command.

Upgrade pip and install flask, flask-WTF (module for wtf-forms only if you need it), python-dotenv.

pip install flask flask-wtf python-dotenv

The module python-dotenv enables us to set flask environment variables using a separate file. Create a .flaskenv set the FLASK_ENV and FLASK_APP variables as shown. FLASK_ENV indicates the type of environment i.e whether the app is under development or production.FLASK_APP specifies the module from which the flask app starts executing. In other words, the file that is first executed when you hit the flask run command to start the server. We will use the flask run command at the end.

FLASK_ENV=development
FLASK_APP=main.py

Now we create a requirements.txt file with below command that contains all the modules required for our app to run, so when we want to run our application on a different machine we can use this file and install using below command.

pip freeze > requirements.txt
pip install -r requirements.txt

Next, create the following directory structure as per your application.

Directory Structure for the demo project.

Demo Project — Hello Potter

Now let's discuss what we did.

The application folder is the main folder that contains our whole flask app. Under that, we have a static folder that stores all the statically generated content like CSS, styles, images, etc. Then we also have a templates folder where you have to put all your HTML files.

The main.py file is the module that we specified under FLASK_APP previously. It has the following import app code that loads our flask app.

from application import app

This code starts compiling our application package/folder. Inside that, we have __init__.py that gets executed as soon as we import the package. It is similar to a constructor i.e def __init__(self) is a python class. Under __init__.py we have :

from flask import Flask
app = Flask(__name__)
from application import routes

In the first line, we import Flask class in flask package that is used to create a flask application. It is the point where the python runtime creates a server and starts running it. We pass a string containing the name of the server to be created. Here __name__ gives the name of our app i.e ‘application’. This instance of our server is stored in the app variable.

In the next line, we import routes module, that executes the below code in routes.py

from application import app                                     
from flask import render_template
@app.route("/")
@app.route("/home")
def homes():
name = "Potter"
return render_template("index.html", name=name)

Initially, we import the app instance that we created in __init__.py. We use this instance to define and assign the URLs and corresponding functions to be called when we enter the URL in the browser. In the next line, we import a function render_template from the flask package, which we will use shortly.

Using the @app.route (URL) decorator we define two URLs ‘/’ i.e root and ‘/home’ which when entered in the browser make the function homes() to be called. This function returns the output of render_template function which renders the specified .html file on to the browser. We can also pass data to that HTML file in the form of (key:value) pairs as shown above. Here the key is (‘name’: “Potter”).

We can access the passed data in the html file as shown below. This is the syntax of the Jinja(a web template engine).

<h1> Hello {{ name }} !! </h1>

Finally, to run the server, open terminal in the project folder, activate venv, and run flask run command. Open the browser and hit http://127.0.0.1:5000/ as URL.

Congratulations on building our first flask app.

--

--