Flask Internals

Commands we can execute to inspect a Flask web application via the CLI ( command-line interface).

Flask Framework - Official Banner.
Flask Framework - Official Banner

Hello! This article presents a few commands we can execute inside Flask Shell that might help us understand the structure and the business logic of a legacy web app. We might need this when we start working on a new project designed or coded in the past or just our own web app is getting bigger.

Thank for reading!

  • πŸ‘‰ Flask - Short introduction
  • πŸ‘‰ Flask CLI (command line interface)
  • πŸ‘‰ List Registered Models
  • πŸ‘‰ List Table Columns

✨ Flask Intro

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. Getting started with Flask is pretty easy. Once we have Python3 installed and a modern code editor like VsCode or Atom we can write our first web app.

$ pip install flask
Flask - Install via PIP

The above command will install the latest version of Flask via PIP, the official package manager for Python.

# Contents of - app.py
from flask import Flask, escape, request

app = Flask(__name__)

@app.route('/')
def hello():
    return f'Hello from Flask!'
Flask - Simple App

Once the file is saved, we can invoke Flask magic using the command:

$ flask run
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Start the App

✨ Flask CLI

Flask provides a convenient way to interact with all app internals via a simple command-line interface powered by the Click package. To invoke the CLI, we need to execute the following command:

$ flask shell
>>>            # The shell is waiting for our commands
Flask - Invoke the CLI
πŸ‘‰ Flask CLI - List Routes
$ flask shell
>>> from app import app
>>> app.url_map
Map([<Rule '/register.html' (HEAD, POST, GET, OPTIONS) -> register>,
 <Rule '/logout.html' (HEAD, GET, OPTIONS) -> logout>,
 <Rule '/sitemap.xml' (HEAD, GET, OPTIONS) -> sitemap>,
 <Rule '/login.html' (HEAD, POST, GET, OPTIONS) -> login>,
 <Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>,
 <Rule '/<path>' (HEAD, GET, OPTIONS) -> index>,
 <Rule '/' (HEAD, GET, OPTIONS) -> index>])
Flask CLI - List Routes
πŸ‘‰ Flask CLI - Print static folder
$ flask shell
>>> from app import app
>>> app.static_folder
'D:\\work\\flask-material-kit\\app\\static'
Flask CLI - Print Static Folder
πŸ‘‰ Flask CLI - Print templates folder
$ flask shell 
>>> from app import app
>>> app.template_folder
'templates'
Flask CLI - Print Template Folder
πŸ‘‰ Flask CLI - List Database Models

The db object holds the SqlAlchemy interface and we will pull the information via this object.

$ flask shell
>>> from app import db      # <-- db is the object 
Flask CLI - Import DB object
πŸ‘‰ List All Tables
>>> from app import app, db
>>> db.metadata.tables
FacadeDict({'Users': Table('Users', MetaData(), Column('id', Integer(), table=<Users>, primary_key=True, nullable=False), Column('user', String(length=64), table=<Users>), Column('email', String(length=120), table=<Users>), Column('password', String(length=500), table=<Users>), schema=None)})
Flask CLI - List Tables

In my project, we have a single Users table - let's take a look at the definition:

>>> from app import app, db
>>> db.metadata.tables['Users']
Table('Users', MetaData(), Column('id', Integer(), table=<Users>, primary_key=True, nullable=False), Column('user', String(length=64), table=<Users>), Column('email', String(length=120), table=<Users>), Column('password', String(length=500), 
table=<Users>), schema=None
Flask CLI - Extract Table 'Users'
πŸ‘‰ Flask CLI - List Table Columns
>>> from app import app, db
>>> for col in db.metadata.tables['Users'].columns:  
...     print ( col )
... 
Users.id
Users.user
Users.email
Users.password 
Flask CLI - List Table Columns

✨ Free Sample - Material Kit

Curious minds can experiment and play with all the above commands using this open-source sample provided with database, authentication, SqlAlchemy ORM, and Docker support - Flask Material Kit.

Flask Material Kit - Bootstrap 5 Design.
Flask Material Kit - Bootstrap 5 Design.

Thanks for reading! For more resources, feel free to access: