How it works...
We can see that we have a circular import between my_app/__init__.py and my_app/hello/views.py, where, in the former, we import views from the latter, and in the latter, we import the app from the former. Although this makes the two modules dependent on each other, there is no issue, as we won't be using views in my_app/__init__.py. Note that it is best to import the views at the bottom of the file so that they are not used.
In this recipe, we used a very simple non-persistent in-memory key-value store for the demonstration of the model layout structure. It is true that we could have written the dictionary for the MESSAGES hash map in views.py itself, but it is best practice to keep the model and view layers separate.
So, we can run this app using just run.py, as follows:
$ python run.py * Serving Flask app "my_app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production
environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 111-111-111
from my_app import app
app.env="development"
app.run(debug=True)
We can see that we have already defined a default message in MESSAGES. We can view the that by opening http://127.0.0.1:5000/show/default. To add a new message, we can type http://127.0.0.1:5000/add/great/Flask%20is%20greatgreat!!. This will update the MESSAGES key-value store to look like the following:
MESSAGES = { 'default': 'Hello to the World of Flask!', 'great': 'Flask is great!!', }
Now, if we open the link http://127.0.0.1:5000/show/great link in a browser, we will see our message, which would have otherwise appeared as a not-found message.