Skip to main content

Flask shell with steroids

Project description

Flask Shell Turbo

A drop-in utility that supercharges the Flask interactive shell, inspired by Django's shell_plus (from django-extensions). It auto-discovers your SQLAlchemy models, injects common utilities, and gives you a productive REPL (Read-Eval-Print Loop) for debugging, data exploration, and quick fixes — no repetitive imports required.

Description

Working with flask shell out of the box means manually importing every model and helper you need each time you open a session. Flask Shell Turbo solves this by:

  • Auto-discovering all SQLAlchemy models registered on db.Model, so every model in your project is available in the shell immediately — no imports.
  • Injecting common utilities like datetime, date, timedelta, timezone, and core SQLAlchemy query helpers (func, select, text, and_, or_, not_, desc, asc).
  • Using IPython automatically when available, with %autoreload enabled so code changes are picked up without restarting the shell.
  • Providing safe helperscommit() and rollback() — that wrap db.session operations with clear success/error feedback.
  • Enhancing the standard flask shell too, via a shell_context_processor, so you get the benefits even without the new command.
  • Adding a new CLI command, flask shell-turbo, with flags for a plain Python fallback and SQL query logging.

Requirements

  • Flask
  • Flask-SQLAlchemy (or a project using the db.Model declarative pattern)
  • SQLAlchemy 1.4+ (a legacy fallback is included for older versions)
  • IPython (optional, but recommended)

Installation

  1. Install flask-shell-turbo

    pip install flask-shell-turbo
    

Integration into a Flask Project

Call init_shell_turbo(app, db) right after your db is initialized — typically in your app factory, after db.init_app(app).

from flask import Flask
from your_project.extensions import db
from flask_shell_turbo import init_shell_turbo

def create_app():
    app = Flask(__name__)
    # ... your config ...
    db.init_app(app)
    # Register shell_turbo after db is initialized
    init_shell_turbo(app, db)

    return app

That's it — no further configuration is needed. Model discovery happens automatically each time the shell starts, so newly added models are picked up without any manual registration.

Usage

Launch the enhanced shell

FLASK_APP=app flask shell-turbo

This drops you into an IPython session (or the standard Python shell if IPython isn't installed) with everything pre-loaded:

Flask shell-turbo
Models: Account, Order, User
Extras : db, session, func, select, and_, or_, desc, asc, datetime, date, timedelta, text
Helpers: commit(), rollback()
──────────────────────────────

Work with your models directly — no imports

user = User.query.filter_by(username="ab").first()
user.name = "Alejandro"
commit()

If something goes wrong, commit() automatically rolls back and prints the error instead of leaving your session in a broken transaction state:

✘ rollback — (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint...

CLI flags

Flag Description
--plain Forces the standard Python shell (code.interact) even if IPython is installed.
--print-sql Enables SQL echo (db.engine.echo = True) so every generated query is printed to the console.

Example:

flask shell-turbo --print-sql

Standard flask shell also benefits

Even without using shell-turbo, the regular command is enriched automatically:

flask shell

Because init_shell_turbo registers a shell_context_processor, all discovered models and utilities are available there too — shell-turbo simply adds the nicer IPython experience, autoreload, and the extra CLI flags on top.

Benefits

  • Zero-import debugging — jump straight into querying and modifying data without hunting down import paths.
  • Faster iteration%autoreload picks up code changes to your models and helpers without restarting the shell.
  • Safer transactionscommit()/rollback() prevent half-finished transactions from silently blocking further queries.
  • Query visibility on demand--print-sql lets you inspect the exact SQL Flask-SQLAlchemy generates, useful for debugging N+1 queries or unexpected joins.
  • Works everywhere — enhances both the new shell-turbo command and the existing flask shell, so teammates who haven't adopted it yet still benefit.
  • No dependencies required — IPython is optional; the tool gracefully falls back to a standard shell with tab-completion via readline/rlcompleter.
  • Version-safe model discovery — supports both the modern SQLAlchemy registry.mappers API and the legacy _decl_class_registry, so it works across SQLAlchemy versions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_shell_turbo-1.0.0.tar.gz (42.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_shell_turbo-1.0.0-py3-none-any.whl (28.3 kB view details)

Uploaded Python 3

File details

Details for the file flask_shell_turbo-1.0.0.tar.gz.

File metadata

  • Download URL: flask_shell_turbo-1.0.0.tar.gz
  • Upload date:
  • Size: 42.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for flask_shell_turbo-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9ab2e39bb900a3ab7ff97352a5a4b16e8b96c512fa17d17a6045c54fb68ede3a
MD5 801e531bb5208493569bf05ce5cdd901
BLAKE2b-256 545cf2bd4a587741caf10bc5abe8eab51bbbec2e4451ac62df3b60274cac5c81

See more details on using hashes here.

File details

Details for the file flask_shell_turbo-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_shell_turbo-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40131e508137e4e65360f8bab3121f71ad4f7e2bcc0fea382ac7ff02e0e0cbf1
MD5 3abdd514d6261dffdeefc1b0917ba768
BLAKE2b-256 f5a0fdf9e5c5bba3fc160ff332963f73a9c4c54976cab14339267092b5c3933b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page