Getting StartedΒΆ

Install Flask-ImpΒΆ

Install in a Virtual environment

pip install flask-imp

To get started right away, you can use the CLI commands to create a new Flask-Imp project.

flask-imp init

Minimal Flask-Imp SetupΒΆ

Run the following command to create a minimal Flask-Imp project.

flask-imp init -n app --minimal

The minimal structureΒΆ

Folder StructureΒΆ

app/
β”œβ”€β”€ resources
β”‚   └── routes.py
β”‚
β”œβ”€β”€ static
β”‚   └── css
β”‚       └── water.css
β”œβ”€β”€ templates
β”‚   └── index.html
β”‚
β”œβ”€β”€ __init__.py
β”œβ”€β”€ config.py
β”œβ”€β”€ extensions.py
└── globals.py

File: app/__init__.py

from flask import Flask

from app.extensions import imp
from app.config import FLASK_CONFIG, IMP_CONFIG


def create_app():
    app = Flask(
        __name__,
        static_url_path="/",
        static_folder="static",
        template_folder="templates",
    )
    app.config.from_object(FLASK_CONFIG.as_object())

    imp.init_app(app, IMP_CONFIG)
    imp.import_resources()

    return app

File: app/resources/routes.py

from flask import Flask
from flask import render_template


def include(app: Flask):
    @app.route("/")
    def index():
        return render_template("index.html")

File: app/templates/index.html

<!DOCTYPE html>
<!--HTML HERE-->
</html>

For more examples see: CLI Commands / flask-imp init


Securing RoutesΒΆ

Use the checkpoint decorator to secure routes, here’s an example:

Update the app/resources/routes.py file with the following code:

from flask import Flask
from flask import render_template
from flask import url_for
from flask import redirect
from flask import session
from flask_imp.security import checkpoint, SessionCheckpoint
from flask_imp.utilities import lazy_url_for

LOGIN_REQUIRED = SessionCheckpoint(
    session_key="logged_in",
    values_allowed=True,
).action(
    fail_url=lazy_url_for("login_required")
)


def include(app: Flask):
    @app.route("/")
    def index():
        return render_template("index.html")

    @app.route("/protected")
    @checkpoint(LOGIN_REQUIRED)
    def protected():
        return "You are logged in!"

    @app.route("/login-required")
    def login_required():
        return "You need to login first!"

    @app.route("/login")
    def login():
        session["logged_in"] = True
        return redirect(url_for("protected"))

    @app.route("/logout")
    def logout():
        session["logged_in"] = False
        return redirect(url_for("index"))

See more at: Security / checkpoint

Setup a Virtual EnvironmentΒΆ

Setting up a virtual environment is recommended.

Linux / Darwin

python3 -m venv venv
source venv/bin/activate

Windows

python -m venv venv
.\venv\Scripts\activate