Home > Back-end >  flask server won't run if I named my application factory function other than "create_app&q
flask server won't run if I named my application factory function other than "create_app&q

Time:12-30

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:

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

CodePudding user response:

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.

  • Related