Flask Internals 
Tags: flask, webdev, cli
Published: 2022-01-05 11:00:22

---
title: Flask Internals 
published: true
description: Commands we can execute to inspect a Flask web application via the CLI ( command-line interface).
tags: flask, webdev, cli
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okkuzcqywfkp37vywkq7.jpg
canonical_url: https://blog.appseed.us/flask-internals-cli-commands/
---
Hello Coders! 

This article presents a few commands we can execute inside the **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 when our own web app is getting bigger.

> Thanks for reading! Topics covered: 

- 👉 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.

```bash
$ pip install flask
```

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

```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:

```bash
$ 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:

```python
$ flask shell
>>>            # The shell is waiting for our commands
```

---

> 👉 Flask CLI - `List Routes`

```python
$ 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`

```python
$ flask shell
>>> from app import app
>>> app.static_folder
'D:\\work\\flask-material-kit\\app\\static'
```

--- 

> 👉 Flask CLI - `Print templates folder`

```python
$ 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.

```python
$ flask shell
>>> from app import db      # <-- db is the object 
```

--- 

> 👉 List All Tables

```python
>>> 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:

```python
>>> 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`

```python
>>> 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](https://appseed.us/apps/flask-apps/flask-material-kit).

- 👉 [Flask Material Kit](https://github.com/app-generator/flask-material-kit) - source code
- 👉 [Flask Material Kit](https://flask-material-kit.appseed-srv1.com/) - LIVE Demo

---

![Flask Material Kit - Bootstrap 5 Design.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd1qbq91tqfqchp9y595.png)

---

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

---

- [Flask Command-Line](https://dev.to/sm0ke/flask-command-line-open-source-sample-34m9) - published on Dev.to
- [Flask - A list of useful “HOW TO’s”](https://dev.to/sm0ke/flask-a-list-of-useful-how-to-s-42m7)