MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

AlmaLinux Flask

with one comment

This post shows how to install and test Flask with Python on AlmaLinux. You install the flask Python libraries with the following commands as the student user. The student user is in the sudoer group.

pip3 install flask_sslify --user student

It produces the following log file:

You can use the following hello.py test program

# Import libraries.
from flask import Flask, escape, request
from markupsafe import escape
 
# Define the application.
app = Flask(__name__)
 
# Define a base URI route and function.
@app.route('/')
def index():
  return "Hello World!"
 
# Define an application URI route and function.
@app.route("/hello")
def hello():
  name = request.args.get("name","Simon")
  return f'Hello {escape(name)}!'
 
# Define an about URI route and function.
@app.route("/about")
def about():
  return "About Page."
 
# Define an <username> variable rule for a route.
@app.route("/user/<string:username>")
def show_user_profile(username):
  return 'User [%s].' % escape(username)
 
# Define an <username> variable rule for a route.
@app.route("/year/<int:year>")
def show_post(year):
  return 'Year [%d].' % year
 
# Run the file.
if __name__ == "__main__":
  app.run()

You can start the Flask server with the following two commands in a separate shell session. This allows you to monitor activities and writes an activity log:

export FLASK_APP=hello.py
flask run

It also writes a compiled version of the hello.py program to the __pycache__ directory. If you make changes to the base file, you must delete the cached version in the __pycache__ directory.

You can test it by typing any of the following URL in a browser:

  1. The index page without a routing label in the URL:

    http://localhost:5000/hello

    It’ll print the following:

    Hello World!
  2. The index page with a hello routing label in the URL:

    http://localhost:5000/hello

    It’ll print the following:

    Hello Simon!
  3. The index page with a about routing label in the URL:

    http://localhost:5000/about

    It’ll print the following:

    About Page.
  4. The index page with a user routing label in the URL:

    http://localhost:5000/user/Somebody

    It’ll print the following:

    User [Somebody].
  5. The index page with a year routing label in the URL:

    http://localhost:5000/year/1986

    It’ll print the following:

    Year [1986].

The activity log shows the following:

 * Serving Flask app 'hello.py'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
Press CTRL+C to quit
127.0.0.1 - - [29/Dec/2022 19:33:12] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [29/Dec/2022 19:33:21] "GET /hello HTTP/1.1" 200 -
127.0.0.1 - - [29/Dec/2022 19:33:27] "GET /about HTTP/1.1" 200 -
127.0.0.1 - - [29/Dec/2022 19:33:48] "GET /year/1986 HTTP/1.1" 200 -
127.0.0.1 - - [29/Dec/2022 19:34:09] "GET /user/Somebody HTTP/1.1" 200 -

If you stop the process with the Ctrl+Z, the process will not stop but not the listener process. As a sudoer user, you can find the open listener process with the following command:

sudo netstat -nlp | grep 5000
[sudo] password for student:

It will return something like this:

tcp        0      0 127.0.0.1:5000          0.0.0.0:*               LISTEN      143363/python3

You kill the process without prejudice by using the following command:

kill -9 143363

As always, I hope this helps those who are looking for step-by-step instructions.

Written by maclochlainn

December 29th, 2022 at 4:35 pm

Posted in flask,Python,Python 3.x