Member-only story
Python Web Development with Flask — Logging, App Commands, and Blueprints
3 min readJan 27, 2021
Flask is a simple web framework written in Python.
In this article, we’ll look at how to develop simple Python web apps with Flask.
Logging
Flask comes with a logger.
We can use it with the app.logger
object.
For example, we can write:
from flask import Flask
from markupsafe import escapeapp = Flask(__name__)@app.route('/')
def index():
app.logger.debug('A value for debugging')
app.logger.warning('A warning occurred (%d apples)', 42)
app.logger.error('An error occurred')
return 'hello'
to call methods to do various levels of logging.
Add App Commands
We can add app commands with Flask.
This way, we can automate tasks that are otherwise manual.
For example, we can create a task to initialize a database by writing:
app.py
from flask import Flask, current_app, g
import os
import sqlite3
import click
from flask.cli import with_appcontextdef get_db():
if 'db' not in g:
g.db = sqlite3.connect(…