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 .outputs()
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
1import launchflow as lf
2
3gcs_bucket = lf.gcp.GCSBucket('my-bucket')
main.py
1from app.infra import gcs_bucket
2from flask import Flask
3
4app = Flask(__name__)
5
6# Connect to the bucket on startup
7gcs_bucket.outputs()
8
9@app.route("/", methods=["GET"])
10def 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
1import launchflow as lf
2
3postgres = lf.gcp.CloudSQLPostgres("my-pg-db")
main.py
1from app.infra import postgres
2from app.models import Base, StorageUser
3from flask import Flask, jsonify
4from flask_sqlalchemy import SQLAlchemy
5from sqlalchemy.orm import Session
6
7app = Flask(__name__)
8
9db = SQLAlchemy(
10 app,
11 model_class=Base,
12 engine_options=postgres.sqlalchemy_engine_options(),
13)
14
15with app.app_context():
16 db.create_all()
17
18
19@app.route("/", methods=["GET"])
20def 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])