question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SIP-24] Proposal to introduce Flask app factory pattern

See original GitHub issue

Motivation

Superset’s singletons are currently configured in the superset package’s __init__.py python module which means that the app itself (Superset) cannot be configured any other way than is described in the root package. The act of simply importing superset for other purposes, such as the cli forces the entire app to load, even though all you might need is SqlAlchemy, or some other component. A better approach is to leverage a pattern such as the “Factory Pattern” in which one or more functions can compose several components into a single “app”. What’s more is that certain portions of the app might need to be overridden in test, or for different use cases. Making the app more composable makes it easier to alter functionality as needed and customize.

Proposed Change

In order to get this done, we will need to add a few new modules, clean up superset.__init__.py and update the way views are added to FAB. This change is largely a refactor, with no change to functionality.

We should perform this migration in a few phases (likely several PRs): 1. Migrate all current direct references to app to leverage flask.current_app proxy 2. Introduce a new extensions.py in the base of the superset package whose job it will be to instantiate the Flask app, and other singletons, such as AppBuilder. 3. Move all logic currently in superset.__init__.py into a new app.py file which will define a create_app() function whose responsibility will be to call init_app() on the Flask app, along with other flask app aware objects 4. Move all calls to appbuilder.add_view_xxx() to app.py

New or Changed Public Interfaces

The biggest change will be the removal of superset.app from the global scope. Instead, we will leverage Flask’s current_app proxy in order to reference the current running app. There will be no changes to the set of dependent libraries.

New dependencies

n/a

Migration Plan and Compatibility

Documentation will likely need to be added which describes the “new way” of doing things once this is merged into master. Contributors that are accustomed to the current structure will likely need to spend a little time coming up to speed with the location of various parts.

Rejected Alternatives

n/a

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
john-bodleycommented, Oct 22, 2019

@craig-rueda I believe if you add the following in app.py,

@click.group(cls=FlaskGroup, create_app=create_app)
def cli() -> None:
    """
    A utility script for the Superset application.
    """

then having FLASK_APP="superset.app" should still work.

1reaction
mistercrunchcommented, Sep 30, 2019

+1!

Note: FAB still has some intricacies around using the factory pattern, for instance, as single security manager can be used across all generated apps.

Based on SIP-0, I think you’re supposed to start a [DISCUSS] thread on the mailing list that points to this issue

Also I think this should help with the looming circular dependencies we’ve been dealing with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[SIP-24] Proposal to introduce Flask app factory pattern #8318
2. Introduce a new extensions.py in the base of the superset package whose job it will be to instantiate the Flask app, and...
Read more >
Re: [VOTE][SIP-24]: Proposal to introduce Flask app factory pattern
Re: [VOTE][SIP-24]: Proposal to introduce Flask app factory pattern · Ville Brofeldt Wed, 21 Apr 2021 20:35:07 -0700. +1 (binding) Ville > On...
Read more >
Application Factories — Flask Documentation (2.2.x)
A common pattern is creating the application object when the blueprint is imported. But if you move the creation of this object into...
Read more >
How to set up a production-grade flask application using ...
Using the application factory pattern method of configuring flask applications enables you to define the configuration of the flask ...
Read more >
Application Factory Function - Flask Tutorial #7 - YouTube
Defining a main module entry point is great for development. But, if you plan on releasing your Flask application in a production setting, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found