Create project folder and virtual environment

mkdir myproject
cd myproject
python3 -m venv .venv     # create a virtual environment

Activate the environment

. .venv/bin/activate

Install Flask

pip install Flask

__**init__**.py

Python uses a funny file name to signal its modules, but it is very useful and a way to modularize our codebase and application. By placing a file named __init__.py inside a directory, we can now reference this module from the parent directory using the directory name.

from flask import Flask

app = Flask(__name__)

For a basic Flask starter project, you will want to import the Flask class from which we will construct an instance of the Flask, which we will refer to as the application or the app for short. However, we are going to create the Flask instance within a factory function like the following:

# application/schedulr/__init__.py

from flask import Flask

def create_app():
	app = Flask(__name__)

	@app.route('/')
	def index():
		return {'message': 'Welcome to the API v1'}, 200

	return app 

The create_app() factory does the following:

  1. Create an instance of our Flask application app
  2. Define a request route on the server to respond to. This is done by using the @app.route() decorator directly preceding a function definition index(). The method dispatch function, index() is referred to as a view function, and will return an HTTP response when the server matches the route (/) with the incoming request. All our dispatch functio does is return a simple message back to the client in a JSON format.
  3. Return the app for consumption.

Project Structure

/application 
├── schedulr/
│   ├── __init__.py
│   ├── db.py
│   ├── schema.sql
│   ├── auth.py
│   ├── users.py
│   ├── reservations.py
│   └── equipment.py
│       
├── tests/
│   ├── conftest.py
│   ├── data.sql
│   ├── test_factory.py
│   ├── test_db.py
│   ├── test_auth.py
│   └── test.py
│ 
├── .venv/
├── app.py 
├── pyproject.toml
└── MANIFEST.in