Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

flask server won't run if I named my application factory function other than "create_app"

My server will run if I name application factory function as create_app like this:

def create_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
       return 'Hello, World!'

    return app

but naming it other than create_app, will throw an error Failed to find Flask application or factory

def foo_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'Hello, World!'

    return app

changing the case will also throw the same error, like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def Create_app():
    app = Flask(__name__)
    @app.route('/')
    def hello():
        return 'Hello, World!'

    return app

Is this behavior is normal? or is there something wrong in my setup?

this is my project layout:

...\projects\mainfoo
|--packagefoo
|    |--__init__.py
|--venvfiles

in the cmd,

...\projects\mainfoo>set FLASK_APP=packagefoo
...\projects\mainfoo>set FLASK_ENV=development
...\projects\mainfoo>flask run

I just follow this tutorial Project Layout & Application setup

>Solution :

From the flask documentation

$ export FLASK_APP=hello

$ flask run

While FLASK_APP supports a variety of options for specifying your
application, most use cases should be simple. Here are the typical
values:

(nothing)

The name “app” or “wsgi” is imported (as a “.py” file, or
package), automatically detecting an app (app or application) or
factory (create_app or make_app).

FLASK_APP=hello

The given name is imported, automatically detecting an
app (app or application) or factory (create_app or make_app).

You cannot customize this behavior, as its a specification in flask to follow.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading