Setting Up a Flask Project

Part 1, Chapter 3


In this chapter, we'll cover how to set up a Flask project and get it running with a simple "Hello World" application.

Key software tools that we'll be using in this chapter:

Source Code

All of the code for this course is stored on GitLab in the following repository:

flask-stock-portfolio-code on GitLab

Part I (Flask Fundamentals) of this course has a corresponding branch (part1_flask_fundamentals) in the repository.

Structure

The easiest way to create a Flask app is to start with a single file. This is my preferred approach for starting a new Flask project as it will provide quick feedback that tells us whether or not the basic environment is configured correctly.

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

$ mkdir flask-stock-portfolio-code
$ cd flask-stock-portfolio-code

At this point, you'll probably have at least two versions of python available on your system:

$ python --version
Python 2.7.10
$ python3 --version
Python 3.8.0

I strongly recommend using Python 3.8.x for all new Python projects. At the very least, you must use a version greater than 3.5 since support for Python 2 ended in January 2020.

Virtual Environments

For even the simplest of projects, you should start with a virtual environment. Virtual environments create isolated environments for each of your Python projects, so that each project can have its own unique dependencies (imported Python packages) and utilize a specific version of the Python interpreter.

Since this project is using Python 3, there's a built-in module called venv that can be used for creating virtual environments.

Run the following command to create a new virtual environment:

$ python3 -m venv venv

Make sure to use python3 instead of python to guarantee that Python 3 will be used in the virtual environment.

This command creates a new directory called "venv", which contains the following items:

  • Python interpreter (Python 3.8.x in this case)
  • Scripts for activating and deactivating the virtual environment

It will also store the Python packages that we install for this project throughout this course.

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

$ source venv/bin/activate
(venv) $

After activating, the virtual environment will be displayed on your command prompt, to the left of the prompt.

Since the virtual environment is active, running the python command will run the Python 3 version of the interpreter (which is stored within your virtual environment itself):

(venv) $ python --version
Python 3.8.0

Installing Packages with pip

With the virtual environment created and activated, it's time to start installing the Python packages that you need using pip.

Start by installing Flask:

(venv) $ pip install flask

This command installs the Flask package plus all of its dependencies (e.g., the other packages needed by Flask):

(venv)$ pip freeze

Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

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

(venv)$ pip freeze > requirements.txt

This can be really beneficial if you (future self) or someone else starts 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. Then, in your text editor of choice, add the following content:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return "Hello World!"

This file creates a new instance of the Flask app and defines a single route that returns the "Hello World!" statement. This is a very basic app, but that's the point: It quickly tells us whether everything has been configured properly or not.

There are a number of great text editors and full IDEs (Integrated Development Environments) available for Python development, but I recommend using PyCharm or VS Code.

Running the Development Server

We can use the Flask development server that comes bundled with Flask in order to check that the code in the app.py file is working correctly.

First, you need to specify where the Flask app is defined (via the FLASK_APP environment variable). Second, you should configure the Flask Development Server to run in 'development' mode (via the FLASK_ENV environment variable), as this makes debugging easier.

(venv) $ export FLASK_APP=app.py
(venv) $ export FLASK_ENV=development

If working on Windows, use set instead of export.

Now run your Flask app with flask run:

(venv) $ flask run

 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: ***-***-***

As the comment states, you can now view your app by navigating to http://127.0.0.1:5000/ in your browser of choice.

127.0.0.1 is mapped to localhost on most systems, so you can alternatively navigate to localhost:5000.

Hello World App

Turn back to you terminal after you view your site in the browser. You should see the following log message:

127.0.0.1 - - [01/Apr/2020 22:17:57] "GET / HTTP/1.1" 200 -

This indicates that a GET request was made to the '/' route and the development server returned a 200 (OK) status code.

What does that mean exactly?

  1. The browser made a request to the Flask development server (running as 'localhost:5000')
  2. The server received the request and responded with a status of 200 (OK) along with the HTML to display "Hello World!" in the web browser:

Flask Development Server - Response

That's it! We created a full web app with a single file. We also confirmed that our environment is configured correctly for development.

Structure

At this point, your project should look like this:

(venv)$ tree -L 2

.
├── app.py
├── requirements.txt
└── venv
    ├── bin
    ├── ...



Mark as Completed