Home / File/ db.py — flask Source File

db.py — flask Source File

Architecture documentation for db.py, a python file in the flask codebase. 4 imports, 2 dependents.

File python ApplicationCore ExtensionRegistry 4 imports 2 dependents 5 functions

Entity Profile

Dependency Diagram

graph LR
  09048b06_50a3_09e1_0ff4_ee97994a3e9b["db.py"]
  b8cab999_537d_0433_b3a2_91d5b3c5a7d7["sqlite3"]
  09048b06_50a3_09e1_0ff4_ee97994a3e9b --> b8cab999_537d_0433_b3a2_91d5b3c5a7d7
  bdb4799f_8b0c_fcfd_51cc_f0b833652f8f["datetime"]
  09048b06_50a3_09e1_0ff4_ee97994a3e9b --> bdb4799f_8b0c_fcfd_51cc_f0b833652f8f
  2681878a_119e_c28f_8e5e_e924c75161c4["click"]
  09048b06_50a3_09e1_0ff4_ee97994a3e9b --> 2681878a_119e_c28f_8e5e_e924c75161c4
  8c762fc5_c0b6_0d4d_3889_896d80fbf225["flask"]
  09048b06_50a3_09e1_0ff4_ee97994a3e9b --> 8c762fc5_c0b6_0d4d_3889_896d80fbf225
  749d9792_d880_75ed_5ccb_68e2218e2db3["auth.py"]
  749d9792_d880_75ed_5ccb_68e2218e2db3 --> 09048b06_50a3_09e1_0ff4_ee97994a3e9b
  bf08e60d_e030_cf15_e246_1de85afd9b5e["blog.py"]
  bf08e60d_e030_cf15_e246_1de85afd9b5e --> 09048b06_50a3_09e1_0ff4_ee97994a3e9b
  style 09048b06_50a3_09e1_0ff4_ee97994a3e9b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import sqlite3
from datetime import datetime

import click
from flask import current_app
from flask import g


def get_db():
    """Connect to the application's configured database. The connection
    is unique for each request and will be reused if this is called
    again.
    """
    if "db" not in g:
        g.db = sqlite3.connect(
            current_app.config["DATABASE"], detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db


def close_db(e=None):
    """If this request connected to the database, close the
    connection.
    """
    db = g.pop("db", None)

    if db is not None:
        db.close()


def init_db():
    """Clear existing data and create new tables."""
    db = get_db()

    with current_app.open_resource("schema.sql") as f:
        db.executescript(f.read().decode("utf8"))


@click.command("init-db")
def init_db_command():
    """Clear existing data and create new tables."""
    init_db()
    click.echo("Initialized the database.")


sqlite3.register_converter("timestamp", lambda v: datetime.fromisoformat(v.decode()))


def init_app(app):
    """Register database functions with the Flask app. This is called by
    the application factory.
    """
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

Subdomains

Dependencies

  • click
  • datetime
  • flask
  • sqlite3

Frequently Asked Questions

What does db.py do?
db.py is a source file in the flask codebase, written in python. It belongs to the ApplicationCore domain, ExtensionRegistry subdomain.
What functions are defined in db.py?
db.py defines 5 function(s): close_db, get_db, init_app, init_db, init_db_command.
What does db.py depend on?
db.py imports 4 module(s): click, datetime, flask, sqlite3.
What files import db.py?
db.py is imported by 2 file(s): auth.py, blog.py.
Where is db.py in the architecture?
db.py is located at examples/tutorial/flaskr/db.py (domain: ApplicationCore, subdomain: ExtensionRegistry, directory: examples/tutorial/flaskr).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free