The flask object implements a WSGI application and acts as the central object. It is passed the name of the module or package of the application. Once it is created it will act as a central registry for the view functions, the URL rules, template configuration and much more. The n
| 79 | |
| 80 | |
| 81 | class Flask(App): |
| 82 | """The flask object implements a WSGI application and acts as the central |
| 83 | object. It is passed the name of the module or package of the |
| 84 | application. Once it is created it will act as a central registry for |
| 85 | the view functions, the URL rules, template configuration and much more. |
| 86 | |
| 87 | The name of the package is used to resolve resources from inside the |
| 88 | package or the folder the module is contained in depending on if the |
| 89 | package parameter resolves to an actual python package (a folder with |
| 90 | an :file:`__init__.py` file inside) or a standard module (just a ``.py`` file). |
| 91 | |
| 92 | For more information about resource loading, see :func:`open_resource`. |
| 93 | |
| 94 | Usually you create a :class:`Flask` instance in your main module or |
| 95 | in the :file:`__init__.py` file of your package like this:: |
| 96 | |
| 97 | from flask import Flask |
| 98 | app = Flask(__name__) |
| 99 | |
| 100 | .. admonition:: About the First Parameter |
| 101 | |
| 102 | The idea of the first parameter is to give Flask an idea of what |
| 103 | belongs to your application. This name is used to find resources |
| 104 | on the filesystem, can be used by extensions to improve debugging |
| 105 | information and a lot more. |
| 106 | |
| 107 | So it's important what you provide there. If you are using a single |
| 108 | module, `__name__` is always the correct value. If you however are |
| 109 | using a package, it's usually recommended to hardcode the name of |
| 110 | your package there. |
| 111 | |
| 112 | For example if your application is defined in :file:`yourapplication/app.py` |
| 113 | you should create it with one of the two versions below:: |
| 114 | |
| 115 | app = Flask('yourapplication') |
| 116 | app = Flask(__name__.split('.')[0]) |
| 117 | |
| 118 | Why is that? The application will work even with `__name__`, thanks |
| 119 | to how resources are looked up. However it will make debugging more |
| 120 | painful. Certain extensions can make assumptions based on the |
| 121 | import name of your application. For example the Flask-SQLAlchemy |
| 122 | extension will look for the code in your application that triggered |
| 123 | an SQL query in debug mode. If the import name is not properly set |
| 124 | up, that debugging information is lost. (For example it would only |
| 125 | pick up SQL queries in `yourapplication.app` and not |
| 126 | `yourapplication.views.frontend`) |
| 127 | |
| 128 | .. versionadded:: 0.7 |
| 129 | The `static_url_path`, `static_folder`, and `template_folder` |
| 130 | parameters were added. |
| 131 | |
| 132 | .. versionadded:: 0.8 |
| 133 | The `instance_path` and `instance_relative_config` parameters were |
| 134 | added. |
| 135 | |
| 136 | .. versionadded:: 0.11 |
| 137 | The `root_path` parameter was added. |
| 138 |