Flask: Support zero-ceremony sub-app mounting (including Blueprints)

Created on 3 Aug 2015  路  8Comments  路  Source: pallets/flask

1361, #1498, and #1494 seem to indicate that some people don't see Blueprints as templates for application behavior, but want to treat them as little self-contained applications that are composed onto the main application. Effectively, we would be taking the app composition pattern and building it into the API.

What sort of an API would we need to properly support app composition for such purposes, since that would solve all of these use cases (404 / 405 error handlers at the "Blueprint" level, only able to see its own templates, etc.)?

The minimum API is Flask#mount_app(app_or_blueprint) - but that still leaves a few questions:

  1. Does the parent app need to be able to route to sub-app routes via url_for?
  2. Does the child app need to be able to route to parent or sibling-app routes via url_for?
  3. Ditto for static and templates (although #1361 suggests that such uses would want the static and template folders to be distinct).
  4. Other questions which I am certainly missing

What do _you_ need from Blueprints? What else would you _expect_ Flask#mout_app to do?

blueprints

Most helpful comment

Indeed it is annoying to workaround the fact you cannot mount sub-blueprints.

All 8 comments

Possibly also related to #593 (nestable blueprints).

this is a _hard__ problem ^^

The parent should be able to route to sub-apps using url_for. (The feature almost seems _built for_ sub-apps anyway)

The sub-apps do not need to route to a parent app or sibling. The point of a sub-app (in my use case) is to create fully modular apps that I can re-use in other applications.

I would be ec-static if templates and static files would try all expected template or static locations, starting from most specific (sub-app's directory), and ending with least specific (parent app's). Though, I could see how a developer would want the parent templates to override the sub templates, or vice versa. Could this be configurable?

Also, would Flask#mount_app be able to mount other Werkzeug apps?

@spight - I'm not sure how one would mount a Werkzeug app differently than any other raw WSGI application. That said, I don't see why we couldn't also allow mounting arbitrary WSGI apps, but they wouldn't be routable to (unless you provided a url_map for it).

The url_for function is really just a wrapper around the current app's url_adapter.build method. If a parent app has a reference to a child app, its very easy to write one's own url_for_subapp function.

In my case the child app would need to be able to redirect the main app. For instance when redirecting to home. What I use blueprints for is to having the app structure split up into different parts. These different parts do tend to interact with eachother sometimes, although not often.

It is very useful to have a common url prefix and a different template folder and static folder.

Indeed it is annoying to workaround the fact you cannot mount sub-blueprints.

I would really love Blueprint level error handlers, in particular. if a url routes into the Blueprint's suffix then the Blueprint handles the errors if the user implements them otherwise error handling falls-back to the parent of the Blueprint.


Before I read up on why I couldn't do the above I always thought of Blueprints as "flask sub-app" esp. because of the fact that Blueprints have the same blueprint.route() api as a flask app.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sungjinp11 picture sungjinp11  路  3Comments

davidism picture davidism  路  3Comments

chuanconggao picture chuanconggao  路  4Comments

tomjaguarpaw picture tomjaguarpaw  路  3Comments

jab picture jab  路  4Comments