Getting Started

Part 1, Chapter 5


In this chapter, we'll set up the base project structure and define the first service.


Create a new project and install Flask along with Flask-RESTful:

$ mkdir testdriven-app && cd testdriven-app
$ mkdir services && cd services
$ mkdir users && cd users
$ mkdir project
$ python3.7 -m venv env
$ source env/bin/activate
(env)$ pip install flask==1.0.2
(env)$ pip install Flask-RESTful==0.3.7

Add an __init__.py file to the "project" directory and configure the first route:

# services/users/project/__init__.py


from flask import Flask, jsonify
from flask_restful import Resource, Api


# instantiate the app
app = Flask(__name__)

api = Api(app)


class UsersPing(Resource):
    def get(self):
        return {
        'status': 'success',
        'message': 'pong!'
    }


api.add_resource(UsersPing, '/users/ping')

New to Flask-RESTful? Review the Quickstart guide.

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 that's what you're used to. Just keep in mind that it is deprecated.

First, add a manage.py file to the "users" directory:

# services/users/manage.py


from flask.cli import FlaskGroup

from project import app


cli = FlaskGroup(app)


if __name__ == '__main__':
    cli()

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

Run the server from the "users" directory:

(env)$ export FLASK_APP=project/__init__.py
(env)$ python manage.py 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 config.py to the "project" directory:

# services/users/project/config.py


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


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


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


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

Update __init__.py to pull in the development config on init:

# services/users/project/__init__.py


from flask import Flask, jsonify
from flask_restful import Resource, Api


# instantiate the app
app = Flask(__name__)

api = Api(app)

# set config
app.config.from_object('project.config.DevelopmentConfig')  # new


class UsersPing(Resource):
    def get(self):
        return {
        'status': 'success',
        'message': 'pong!'
    }


api.add_resource(UsersPing, '/users/ping')

Run the app again. This time, let's enable debug mode by setting the FLASK_ENV environment variable to development:

$ export FLASK_APP=project/__init__.py
$ export FLASK_ENV=development
$ python manage.py run

* Serving Flask app "project/__init__.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: 442-775-962

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:

Flask==1.0.2
Flask-RESTful==0.3.7

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

__pycache__
env

Init a git repo and commit your code to GitHub.




Mark as Completed