Initializing the Flask project

So, we are finally entering the fun phase of our project where we will be building this project from scratch. So, let's not wait too long before we can see some action. The first thing we will do is to set up a basic project with Flask and get it running. To do this, let's fire up our code editor and set up our initial code base.

Let's open up the file bugzot/application.py and initialize our application code base:

'''
File: application.py
Description: The file contains the application initialization
logic that is used to serve the application.
'''
from flask import Flask, session
from flask_bcrypt import Bcrypt
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy

# Initialize our Flask application
app = Flask(__name__, instance_relative_config=True)

# Let's read the configuration
app.config.from_object('config')
app.config.from_pyfile('config.py')

# Let's setup the database
db = SQLAlchemy(app)

# Initializing the security configuration
bcrypt = Bcrypt(app)

# We will require sessions to store user activity across the application
Session(app)

Here we have completed the very basic setup of our application. Let's spend some time trying to understand what we did here.

At the very start of the file, we first imported the required packages over which we will be building our project. We imported the Flask application class from the package flask. Similarly, we import the code hashing library bcrypt, the Flask session class, and the SQLAlchemy support package for Flask, which provides SQLAlchemy integration with Flask.

Once we have imported all the required packages, the next thing is to initialize our Flask application. To do this, we create an instance of Flask class and store it in an object named app.

app = Flask(__name__, instance_relative_config=True)

While creating this instance, we pass the class constructor two parameters. The first parameter is used to signify the name of the application to Flask. __name__ provides, which we pass as the application name to the constructor. The second parameter, instance_relative_config allows us to override the application configuration from an instance folder.

With this, we have our Flask application instance setup done. Next thing inline is to load up the configuration for the application, which will be used to configure how the different components inside our application behave, and how our application will be served to the user. To do this, we need to read from our configuration file. The following two lines achieve this:

app.config.from_object('config')
app.config.from_pyfile('config.py')

The first line loads up our config.py file under the project root, treats it as an object, and loads up its configuration. The second line is responsible for reading the config.py file under the instance directory and loading any configuration that may be present there.

Once these configurations are loaded, they are available under the app.config object. Most of the Flask plugins are configured to read the configuration from the app.config, hence reducing the clutter that may happen if every plugin had a different mechanism for dealing with the configuration.

With our configuration loaded up inside our application, we can now move on to initialize the remaining modules that we may require. In particular, we require a few more modules to establish our application functionality. These modules include the SQLAlchemy engine, which we will use to build and interact with our database models, a sessions module, which will be required to manage user sessions across the application, and a bcrypt module, which will be required to provide encryption support across the application. The following lines of code provide this functionality:

db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
Session(app)

As we can see from these lines of code, to configure these modules, all we needed to do was pass the Flask application object as a parameter to the respective class constructors and their configuration will be picked up from there itself.

Now, we have our application initialization code in place, the next thing we need to do is to export the required components from our BugZot module so the application can be called from the project root.

To achieve this, all we need to do is to get these modules included in the module entry point. So, let's fire up the code editor and open bugzot/__init__.py where we need to get these objects.

'''
File: __init__.py
Description: Bugzot application entrypoint file.
'''
from .application import app, bcrypt, db

And we are done. We have all the required objects exported in our BugZot module. Now, the question is how to launch our application. So, to launch our application and make it serve the incoming requests, we need to complete a few more steps. So, let's open up the run.py file inside our project root and add the following lines to it:

'''
File: run.py
Description: Bugzot application execution point.
'''
from bugzot import app

if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)

And we are done. Wasn't that simple? All we did here was to import the flask app object we created inside our BugZot module and call the run method of the app object passing it the value for the hostname on which the application will be serving the users, and the port on which the application server should bind to listen to the requests.

We are now all set to start our application server and make it listen to the incoming requests. However, before we do that, we need to just complete one more step, which is to create the configuration for the application. So, let's get going and create the configuration.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.91.47