We all start with building our applications on our own desktop. We start making a new project or build an idea from scratch on our own local host. But what when it is fully finished or ready enough to share with the rest of the world? A production environment is necessary for this. As you might know, or maybe hear for the first time, an application deployment is needed to run your app. In this blog post, we share with you how we deployed our Python application on our IBMi power system.
For our project setup, we created the products_module, and the users_module to separate these two from each other. Modules are used when you import statements such as import Flask. These created modules contain a model and a Controller. The Model interacts with the database and the Controller interacts with the model and fetches the asked information through triggers or events.
The __init__.py file contains the import of packages and the configuration of the project, our example.
Connecting with DB2
For the connection with our db2 database, we used the package “Flask-SQLAlchemy”. This package can be easily installed and configured. After setting up the Flask application, the connection to the database can be configured with SQLAlchemy. This package is managed by IBM, which provides that this package will stay up to date. The package is also open source, which makes expansions possible by the community.
In our demo, we added the configuration through a separate config file. In the picture below, you can see the import of Flask and SQLAlchemy and define the first steps of the connection.
The config file is the next step, this defines which database schema you use, your connection host, and the connection user.
Deployment with FastCGI
The first step of deploying our application is using FastCGI, this is a deployment option for Python. To deploy your Python application with FastCGI, you need to create a WSGI server that will run your application. This package can be easily imported and used as following:
Configuring an HTTP Server
For the last step of the real deployment of our Python application, we created an HTTP Server on our IBMi system. Creating the HTTP Server is the easy step, the next step is adding the configuration. With the IBM Web Administration for I platform aka HTTP Admin, it is easy to configure the httpd configuration file. The first step is adding the FastCGI module to handle your Python script. Afterwards, the document root and the directory of your Python application must be added to the configuration file as well, this is the location of your application on your system.
Here is an example of the httpd.conf file:
Demo of our small Python application
For this proof of concept of our Python application named “pythonflask”, we created an API application that returns raw JSON data. Two routes were created that call our model, which connects to our database to get the data from the needed table. In the controller, we define the resource fields and then get the data we need, this can be all the data from that table or just one product by product name or ID. This will return the needed info and send it back to the interface.
Our two route calls contain all the data of our products, and the users.
Below is an example of the controller to retrieve the product data, the first returns the one product that identifies with the requested product ID, whereas the second one return all the product data from the table.