Deploying Flask Apps Locally on Windows

Danny Morris

2019/01/20

This document describes an approach to developing a Flask app and deploying locally.

Environment

Create and activate conda environment in Anaconda Prompt

Before I begin with building the Flask app, I’ll create a conda environment to house the dependencies needed to run the app. A conda environment is simply an isolated directory that contains a specific collection of conda packages needed for the job. Python and Flask are the dependencies needed to run this particular app.

Open Anaconda Prompt and enter the following commands to create and activate a new conda environment.

conda create -n flask-app
conda activate flask-app

By activating the environment named “flask-app”, we enable package installation to the environment.

Install dependencies

While in Anaconda Prompt, make sure the conda environment is activated. Enter this command to install the necessary dependencies to build and deploy the Flask app.

conda install python==3.6

Run conda list to see a list of dependencies installed in the conda environment. Here I am installing Python 3.6, though other versions can be specified as well.

Very simple Flask app

Now that the conda environment is created and activated and the dependencies are installed, I can start building the Flask app. Open a text editor (e.g. Notepad++, Spyder, etc.) and create a Python script called app.py containing the following Python code.

# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello world!\n'

if __name__ == '__main__':
    app.run()

Run the app

Open Anaconda Prompt and make sure the conda environment is activated, then change directory to the folder where app.py was saved. For example, if app.py was saved in C:\Users\Owner\Documents\flask-project, your command in Anaconda Prompt may look like this.

cd Documents/flask-project

Once the new directory has been set, I can deploy the app using the following code.

python app.py

If done correctly, the print out in Anaconda Prompt should look like this.

Test the app

To test if the app is working as expected, open a web browser and navigate to http://localhost:5000. You should a message that reads “Hello world!” on the page.

Next steps

In another article I’ll look deploying a machine learning built using scikit-learn.