Flask Internals
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
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!'
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)
β¨ 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 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 - Print static
folder
$ flask shell
>>> from app import app
>>> app.static_folder
'D:\\work\\flask-material-kit\\app\\static'
π Flask CLI - Print templates
folder
$ flask shell
>>> from app import app
>>> app.template_folder
'templates'
π 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
π 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)})
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 - 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
β¨ 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 - source code
- π Flask Material Kit - LIVE Demo
Thanks for reading! For more resources, feel free to access:
- Flask Command-Line - published on Dev.to
- Flask - A list of useful βHOW TOβsβ Β