Building IBM i WEB-APP with Python and Flask

Flask is a fantastic and very light Python framework for creating powerful web applications and is an excellent tool for creating your own websites in a dynamic and interactiv
e way. With this technique you can create interesting application integrations on Ibm-I, even on versions antecedent to 7.X. The m
ost famous python framework for building a web application is django, but with flask you can immediately get excellent results and modernize management processes with new applications that take advantage of current technologies such as boostrap, jquery, etc.
In this article I illustrate a simple example to understand the basic structure of the application and its potential.

Requirements

Flask python application

This is the heart of the python program in which the engine of the html page is made through the python flask framework.
The “render_template” is all that the server renders in the form of html and the “route” renders the name of the page to insert in the url of the browser.

from flask import Flask, render_template
from tkISeries import tkISeries 
app = Flask (__ name__)
@ app.route ('/ articles', methods = ('GET', 'POST'))
 def articles ():
     iseries = tkISeries ()
     iseries.connection ()
     rows = iseries.selectArticoli (iseries.c1)
     return render_template ('/ articolo.html', len = len (rows), lenr = len (rows)[0], rows = rows)

IBM-I connection

This is an example of the python class for the connection to the database and the select for column return, instantiated by the main application. The python packa
ge “pypyodbc” and the odbc Ibm driver are the prerequisites for the connection and the execution of the desired sql specifications.

mainapp.py

import pypyodbc
class tkISeries ():
     def init (self):
         super (tkISeries, self) .init ()
     def connection (self):
         # xxx.xxx.xxx.xxx Ip Ibm-I user = Ibm-I user password = Ibm-I user password 
         connection = pypyodbc.connect (driver = '{iSeries Access ODBC Driver}',
                         system = 'xxx.xxx.xxx.xxx' uid = 'user', pwd = 'password')
         self.c1 = connection.cursor ()
     def selectArticles (self, c1):
         # LIBRARY = IBM-I library name, FILE = Table name 
         string = ("select cdar, dsar, dssa, umba, decimal (csbc, 11,2) from LIBRERIA.FILE")
         try:
             c1.execute (string) 
             row = c1.fetchall ()
         except Exception as e:
             print ("error")
         return row

 

Html

The html code contains links to boostrap and jquery to perform a search on the page.
Delimiters in braces {% ….%} express instructions (for, if, ..).
Delimiters in braces {{…}} express the variables.

Http server

Using a simple wsgi python server named waitress:
To run the application run the following command: python waitress_server.py.
The IP address to be entered in the browser corresponds to the machine where the http server was launched (pc, Windows server, Linux server, ..) A
t this point it is sufficient to type in the browser the address http: // ip_address: 8080 / articles and you will see the final result!

 
waitress_server.py 

from waitress import serves
import mainapp
serve (mainapp.app, host = '0.0.0.0', port = 8080)

Final results

Jquery javascript searches all HTML. The boostrap classes make the html page responsive and modern.
… .And now have fun and space for creativity.

Related Posts
DB2 for i SQL – String Manipulation – POSSTR-LOCATE-LOCATE_IN_STRING (EN)

Introduction Often, in our applications, we need to work with text strings, and DB2 SQL can come in very useful Read more

DB2 for i – FAQ & Howtos (EN)

DB2 Database and SQL ... maybe the most important things on IBM i platform: here's a collection of FAQs, tips Read more

IBM i 7.4 Announcement (En)

Comes directly with the Easter egg this IBM announcement for the news of the IBM i 7.4 version, iNext version Read more

Generated Always Columns (EN)

Introduction "Generated Always Column": are columns, table fields, filled by DB2 engine: something like columns with a default value but Read more

Recent Posts

VsCode Extention: Bob Cozzi’s RPG IV to RPG Free Conversion

👉 Review: Bob Cozzi’s RPG IV to RPG Free Conversion – a useful VS Code extension for RPG modernization If…

2 weeks ago

IBM i & SQL Tips #010 – Locating Programs in the Call Stack with STACK_INFO

Hello everyone, I’d like to highlight another excellent contribution by Massimo Duca, part of his ongoing IBM i & SQL…

2 weeks ago

Trying out “Display File DDS Edit” for VS Code

Intrigued by some recent posts from Cristian Larsen on LinkedIn (New Release – Display File DDS Edit v 0.10.1), I…

4 weeks ago

Project Bob: the next-gen AI partner for IBM i and IBM Z application development

Hello everyone, Today I’d like to draw your attention to a major new announcement from IBM: Project Bob — a…

4 weeks ago

IBM i & SQL Tips #6: Calling REST APIs and Parsing JSON Responses with SQL

I want to share with you a particularly useful article by Massimo Duca in the IBM i & SQL Tips…

2 months ago

How Parameter Passing Works in IBM i Programs (RPG / Cobol)

Hello everyone, I’d like to highlight a very useful article by Marco Riva on Markonetools, where he clearly explains how…

2 months ago