How to do it...
First, create a new folder in the application and move all files inside this new folder.
Then, create __init__.py in the folders, which are to be used as modules.
After that, create a new file called run.py in the topmost folder. As the name implies, this file will be used to run the application.
Finally, create separate folders to act as modules.
Refer to the following file structure to get better understanding:
flask_app/ - run.py - my_app/ - __init__.py - hello/ - __init__.py - models.py - views.py
Let's see how each of the preceding files will look.
The flask_app/run.py file will look something like the following lines of code:
from my_app import app app.run(debug=True)
The flask_app/my_app/__init__.py file will look something like the following lines of code:
from flask import Flask app = Flask(__name__) import my_app.hello.views
Next, we will have an empty file just to make the enclosing folder a Python package, flask_app/my_app/hello/__init__.py:
# No content. # We need this file just to make this folder a python module.
The models file, flask_app/my_app/hello/models.py, has a non-persistent key-value store, as follows:
MESSAGES = { 'default': 'Hello to the World of Flask!', }
Finally, the following is the views file, flask_app/my_app/hello/views.py. Here, we fetch the message corresponding to the requested key and can also create or update a message:
from my_app import app from my_app.hello.models import MESSAGES @app.route('/') @app.route('/hello') def hello_world(): return MESSAGES['default'] @app.route('/show/<key>') def get_message(key): return MESSAGES.get(key) or "%s not found!" % key @app.route('/add/<key>/<message>') def add_or_update_message(key, message): MESSAGES[key] = message return "%s Added/Updated" % key