Framework Guides

Flask

Overview

All of LaunchFlow's resource can be easily plugged in and used in your Flask application with most working out of the box. For more complex dependencies, like Postgres, LaunchFlow includes utilities to help you plug into Flask extensions, like Flask-SQLAlchemy.

For all resources you can call .connect() at the start of your application to connect to them to your Flask application.

Most resource clients can simply be injected as is. For example if you are using a GCS or S3 bucket you can do the following:

infra.py

1
import launchflow as lf
2
3
gcs_bucket = lf.gcp.GCSBucket('my-bucket')

main.py

1
from app.infra import gcs_bucket
2
from flask import Flask
3
4
app = Flask(__name__)
5
6
# Connect to the bucket on startup
7
gcs_bucket.connect()
8
9
@app.route("/", methods=["GET"])
10
def read_root() -> str:
11
return gcs_bucket.download_file(file_name).decode("utf-8")

Complex Dependencies

LaunchFlow includes utilities for more complex dependencies like Postgres. For example, if you are using Flask-SQLAlchemy you can use the sqlalchemy_engine_options methods on Postgres resources to get the SQLAlchemy engine options.

SQLAlchemy

infra.py

1
import launchflow as lf
2
3
postgres = lf.gcp.CloudSQLPostgres("my-pg-db")

main.py

1
from app.infra import postgres
2
from app.models import Base, StorageUser
3
from flask import Flask, jsonify
4
from flask_sqlalchemy import SQLAlchemy
5
from sqlalchemy.orm import Session
6
7
app = Flask(__name__)
8
9
db = SQLAlchemy(
10
app,
11
model_class=Base,
12
engine_options=postgres.sqlalchemy_engine_options(),
13
)
14
15
with app.app_context():
16
db.create_all()
17
18
19
@app.route("/", methods=["GET"])
20
def list_users():
21
storage_users = db.session.execute(select(StorageUser)).scalars().all()
22
return jsonify([{"id": user.id, "name": user.name} for user in storage_users])
Previous
FastAPI Integration