Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

There's more...

Sometimes, you may want a single SQLAlchemy db instance to be used across multiple applications, or create an application dynamically. In such cases, we might not prefer to bind our db instance to a single application. Here, we will have to work with the application context to achieve the desired outcome.

In this case, we will register our application with SQLAlchemy differently as follows:

from flask import Flask 
from flask_sqlalchemy import SQLAlchemy 
 
db = SQLAlchemy() 
 
def create_app(): 
    app = Flask(__name__) 
    db.init_app(app) 
    return app 
The preceding approach can be taken up while initializing the app with any Flask extension, and is very common when dealing with real-life applications.

Now, all the operations that were ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content