Getting Started
Part 1, Chapter 3
In this chapter, we'll set up the base project structure.
Create a new project and install Flask along with Flask-RESTX:
$ mkdir flask-tdd-docker && cd flask-tdd-docker
$ mkdir src
$ python3.12 -m venv env
$ source env/bin/activate
(env)$ pip install flask==2.3.3
(env)$ pip install flask-restx==1.2.0
Feel free to swap out virtualenv and Pip for Poetry or Pipenv. For more, review Modern Python Environments.
Add an __init__.py file to the "src" directory and configure the first endpoint:
# src/__init__.py
from flask import Flask, jsonify
from flask_restx import Resource, Api
# instantiate the app
app = Flask(__name__)
api = Api(app)
class Ping(Resource):
def get(self):
return {
'status': 'success',
'message': 'pong!'
}
api.add_resource(Ping, '/ping')
New to Flask-RESTX? Review the Quick start guide.
Next, let's configure the Flask CLI tool to run and manage the app from the command line.
First, add a manage.py file to the project root, "flask-tdd-docker":
# manage.py
from flask.cli import FlaskGroup
from src 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:
(env)$ export FLASK_APP=src/__init__.py
(env)$ python manage.py run
Navigate to http://127.0.0.1:5000/ping in your browser. You should see:
{
"message": "pong!",
"status": "success"
}
Shut down the server. Then, add a new file called config.py to the "src" directory, where we'll define environment-specific configuration variables:
# src/config.py
class BaseConfig:
TESTING = False
class DevelopmentConfig(BaseConfig):
pass
class TestingConfig(BaseConfig):
TESTING = True
class ProductionConfig(BaseConfig):
pass
Update __init__.py to pull in the development config on init:
# src/__init__.py
from flask import Flask, jsonify
from flask_restx import Resource, Api
# instantiate the app
app = Flask(__name__)
api = Api(app)
# set config
app.config.from_object('src.config.DevelopmentConfig') # new
class Ping(Resource):
def get(self):
return {
'status': 'success',
'message': 'pong!'
}
api.add_resource(Ping, '/ping')
Run the app again. This time, let's enable debug mode by setting the FLASK_DEBUG
environment variable to 1
:
(env)$ export FLASK_APP=src/__init__.py
(env)$ export FLASK_DEBUG=1
(env)$ python manage.py run
* Serving Flask app 'src/__init__.py'
* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
* Restarting with stat
* Debugger is active!
* Debugger PIN: 483-377-737
Now when you make changes to the code, the app will automatically reload. Try this out. You also have access to the interactive debugger. Shut down the server once done. Exit then remove the virtual environment as well. Then, add a requirements.txt file to the project root:
flask==2.3.3
flask-restx==1.2.0
Werkzeug==2.3.8
Finally, add a .gitignore to the project root:
__pycache__
env
Init a git repo and commit your code.
✓ Mark as Completed