Menu

Steps for Starting a New Flask Project using Python3

April 7, 2018 - Python

Introduction

This blog post describes my current approach to starting a new Flask project. This blog post covers getting up and running with a simple “Hello World” application using the following tools:

This blog post assumes the use of Python 3.4 or greater, as these versions have the ‘venv’ module built-in for creating virtual environments and pip comes automatically installed in the virtual environment that gets created.

Structure

The easiest way to create a Flask application is to put everything into a single file. This is my preferred approach for starting a new Flask project, as it proves out the environment that is being created.

Start by creating a new directory and changing into that directory:

$ mkdir flask_simple_application
$ cd flask_simple_application/

At this point, you will likely see that you have at least two version of python available on your system:

$ python --version
Python 2.7.10
$ python3 --version
Python 3.6.3

I strongly recommend using Python3 for all new projects, as it has tons of new features and the support for Python2 (legacy Python) is ending in 2020 (countdown clock).

Virtual Environments

For even the simplest projects, creating a virtual environment is a great idea. Virtual environments create an isolated environment for each project, which means that each project can have its own dependencies (ie. version of the Python interpreter, imported python packages, etc.).

Virtual environments really became beneficial to me when I was working on two simultaneous projects:

  1. An existing project required using Python2, as a needed package only supported Python2
  2. A new project where I wanted to use Python3

Having separate virtual environments for each project allowed me to easily configure which version of Python to use.

Since this project is using Python3, there is a built-in module called venv that can be used for creating virtual environments. Note: the venv module was added in Python 3.3.

Here’s the command for creating a new virtual environment, where the directory to create the virtual environment in is specified as ‘venv’. NOTE: make sure to use ‘python3’ instead of ‘python’ to guarantee that Python3 will be used in the virtual environment.

$ python3 -m venv venv

This command created a new directory called ‘venv’ which contains the following items to keep this project isolated from the other projects on your system:

To start using the virtual environment that was created, it needs to be activated:

$ source venv/bin/activate
(venv) $

After activating the virtual environment, the virtual environment name gets displayed on your command prompt (the ‘(venv)’ at the left of the prompt).

Since the virtual environment is active, running the python interpreter will use Python3!

(venv) $ python --version
Python 3.6.3

Installing Packages with pip

After creating the virtual environment for your application, it is time to start installing the Python packages that you need using pip. If you are using Python 3.4 or greater, pip gets automatically installed in your virtual environment.

Start by installing the Flask framework, which is the ‘flask’ package:

pip install flask

This command installs the Flask package plus all of its dependencies (ie. other packages needed by Flask). Here’s a sample of all the packages that were installed:

$ pip freeze
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
Werkzeug==0.14.1

It’s a good idea to save the packages (including version numbers) that are being used in the project:

$ pip freeze > requirements.txt

This can be really beneficial if you or someone else is starting with this project and wants to quickly install all these packages with ‘pip install -r requirements.txt’.

Writing Python Code

Now it’s time to actually write some Python code! In the top-level directory that was created, create a new file called app.py with the following content:

1
2
3
4
5
6
7
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello World!"

This file creates a new instance of the Flask application and defines a single route that returns the “Hello World!” statement. This is the most basic Flask application, but it will show that everything has been configured properly.

Running the Development Server

In order to check that app.py is working correctly, the Flask development server that comes bundled with the Flask framework is a great way to test out your application.

First, you need to specify where the Flask application is defined (via the FLASK_APP environment variable). Then, you can run your Flask application with ‘flask run’:

(venv) $ export FLASK_APP=app.py
(venv) $ flask run
* Serving Flask app "app"
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

As the comment states, you can now navigation to http://127.0.0.1:5000/ (this is mapped to ‘localhost’ on most systems, so you can alternatively navigate to ‘localhost:5000’) to view your application:

Structure of the Flask Application

At this point, the Flask application should look like this:

$ tree -L 2
.
├── app.py
├── requirements.txt
└── venv
    ├── bin
    ├── ...

Conclusion

This blog post provided the steps that I follow for creating a new Flask application using Python 3. In an upcoming blog post, I’ll describe how I like to structure a Flask application.

 

Copied from http://www.patricksoftwareblog.com/steps-for-starting-a-new-flask-project-using-python3/

Leave a Reply