Getting Started

Part 1, Lesson 4

In this lesson, we’ll set up the base project structure and define the first service…

Create a new project and install Flask:

$ mkdir testdriven-app && cd testdriven-app
$ mkdir services && cd services
$ mkdir users && cd users
$ mkdir project
$ python3.6 -m venv env
$ source env/bin/activate
(env)$ pip install flask==0.12.2

Add an file to the “project” directory and configure the first route:

# services/users/project/

from flask import Flask, jsonify

# instantiate the app
app = Flask(__name__)

@app.route('/users/ping', methods=['GET'])
def ping_pong():
    return jsonify({
        'status': 'success',
        'message': 'pong!'

Next, let’s configure the Flask CLI tool to run and manage the app from the command line.

Feel free to replace the Flask CLI tool with Flask Script if you’re used to it. Just keep in mind that it is deprecated.

First, add a file to the “users” directory:

# services/users/

from flask.cli import FlaskGroup

from project import app

cli = FlaskGroup(app)

if __name__ == '__main__':

Here, we created a new FlaskGroup instance to extend the normal CLI with commands related to the Flask app.

Run the server:

(env)$ export FLASK_APP=project/
(env)$ python run

Navigate to http://localhost:5000/users/ping in your browser. You should see:

  "message": "pong!",
  "status": "success"

Kill the server and add a new file called to the “project” directory:

# services/users/project/

class BaseConfig:
    """Base configuration"""
    TESTING = False

class DevelopmentConfig(BaseConfig):
    """Development configuration"""

class TestingConfig(BaseConfig):
    """Testing configuration"""
    TESTING = True

class ProductionConfig(BaseConfig):
    """Production configuration"""

Update to pull in the dev config on init:

# services/users/project/

from flask import Flask, jsonify

# instantiate the app
app = Flask(__name__)

# set config

@app.route('/users/ping', methods=['GET'])
def ping_pong():
    return jsonify({
        'status': 'success',
        'message': 'pong!'

Run the app again. This time, let’s enable debug mode:

$ export FLASK_DEBUG=1
$ python run
* Serving Flask app "project"
* Forcing debug mode on
* Running on (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 128-868-573

Now when you make changes to the code, the app will automatically reload. Once done, kill the server and deactivate from the virtual environment. Then, add a requirements.txt file to the “users” directory:


Finally, add a .gitignore, to the project root:


Init a git repo and commit your code to GitHub.