Flask: Handle app factory in FLASK_APP

Created on 1 May 2017  路  9Comments  路  Source: pallets/flask

FLASK_APP=myproject.app:create_app('dev')

Gunicorn does this with eval
, which I'm not super happy with. Instead, we could use literal_eval to allow a simple list of arguments. The line should never be so complicated that eval would be necessary anyway.

~~~python

might need to fix this regex

m = re.search(r'(\w+)((.*))', app_obj)

if m:
app = getattr(mod, m.group(1))(*literal_eval(m.group(2)))
~~~

cli

Most helpful comment

I really like the shortcut of being able to call the factory in uWSGI and Gunicorn config. I think the ease of setup outweighs the complexity here. If users want to use env vars instead, that's fine and should probably be documented as an alternative.

All 9 comments

Should be re.search instead of re.match, right?

Also handle myproject.app:create_app without parens.

literal_eval("('dev')") evaluates to a string, not a tuple, so need to special case one positional arg, probably by adding a comma at the end. Need to check that it's not empty parens though, because then inserting is a syntax error.

This solution only allows positional arguments (and named script_info argument implicitly), but I'm ok with not supporting keyword arguments.

Down vote on this feature.

The environ FLASK_ENV=dev way would be better.

Something like FLASK_ENV would not belong into the core of a microframework like Flask. You don't necessarily have multiple configs available - actually, if you want to be able to specify which config to use I'd just use app.config.from_envvar('MYAPP_CONFIG').

I'm 卤0 on this issue: It does make FLASK_APP more flexible, but on the other hand always calling the app factory with no arguments - which. in my opinion, should use the most common default values - keeps things nice and simple.

@ThiefMaster Actually I don't use FLASK_ENV either. The FLASK_ENV way is a suggested replacement for this issue.

I've always been using the MYAPP_CONF=/path/to/conf.py to bootstrap the flask app.

I'm not that fond either with such flexibility in FLASK_APP, a simple call with no arguments to the application factory might be enough, IMO. By looking at the deployment options in the docs we may need to write our own wsgi.py (or something similar) application file for a handful of cases anyway.

I merged a pull request yesterday which provides this functionality

The PR looked good to me, and from what I could tell, this was @davidism's plan for the project, so I went ahead with it. Leaving this ticket open for further discussion, if any is needed.

I really like the shortcut of being able to call the factory in uWSGI and Gunicorn config. I think the ease of setup outweighs the complexity here. If users want to use env vars instead, that's fine and should probably be documented as an alternative.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidhariri picture davidhariri  路  3Comments

stillesjo picture stillesjo  路  4Comments

xliiv picture xliiv  路  3Comments

westonplatter picture westonplatter  路  3Comments

greyli picture greyli  路  3Comments