Skip to main content

A tool to format SQL code in Jupyter notebooks.

Project description

sqlnbfmt

PyPI License Python Versions

A SQL formatter designed specifically for Jupyter Notebooks. sqlnbfmt automatically formats SQL queries embedded in code cells, including both Python strings and SQL magic cells (%%sql), helping you maintain clean and consistent code.

Features

  • 🎯 Smart SQL Detection: Automatically identifies and formats SQL queries in code cells and magic SQL cells
  • 🌳 AST-Powered: Uses Abstract Syntax Tree parsing for accurate SQL string identification
  • 🔒 Safe Formatting: Preserves query parameters (e.g., %s, ?) and SQL comments
  • ⚙️ Highly Configurable: Customize formatting through YAML configuration
  • 🔄 Pre-commit Ready: Seamlessly integrates with pre-commit hooks
  • 📦 Zero Dependencies: Minimal installation footprint

Installation

pip install sqlnbfmt

Usage

Command Line

Format a single notebook:

sqlnbfmt path/to/your_notebook.ipynb

Format all notebooks in a directory:

sqlnbfmt path/to/notebooks/

Pre-commit Integration

  1. Install pre-commit:
pip install pre-commit
  1. Add to .pre-commit-config.yaml:
repos:
  - repo: local
    hooks:
      - id: sqlnbfmt
        name: sqlnbfmt
        entry: sqlnbfmt
        language: system
        types: [jupyter]
        args: [--config, config.yaml, --dialect, mysql]

Please fun the following command in your CMD for help:

sqlnbfmt -h
  1. Install the hook:
pre-commit install
  1. (Optional) Run on all files:
pre-commit run --all-files

Configuration

Create a config.yaml file to customize formatting behavior. Here is a template.

Configuration Options

Option Description Default
sql_keywords SQL keywords to recognize and format Common SQL keywords
function_names Python functions containing SQL code []
sql_decorators Decorators indicating SQL code []
single_line_threshold Maximum length before splitting SQL 80
indent_width Number of spaces for indentation 4

Example

Before formatting:

execute_sql("""SELECT a.col1, b.col2 FROM table_a a JOIN table_b b ON a.id = b.a_id WHERE a.status = 'active' ORDER BY a.created_at DESC""")

After formatting:

execute_sql("""
SELECT
  a.col1,
  b.col2
FROM
  table_a AS a
JOIN
  table_b AS b
  ON a.id = b.a_id
WHERE
  a.status = 'active'
ORDER BY
  a.created_at DESC
""")

Contributing

We welcome contributions! Here's how to get started:

  1. Clone the repository:
git clone https://github.com/flyersworder/sqlnbfmt.git
cd sqlnbfmt
  1. Use uv to sync the environment:
uv sync
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Run tests:
pytest

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • sqlglot - SQL parsing and formatting engine
  • All contributors and early adopters who helped shape this tool

Made with ♥️ by the sqlnbfmt team

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

sqlnbfmt-0.1.4.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

sqlnbfmt-0.1.4-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file sqlnbfmt-0.1.4.tar.gz.

File metadata

  • Download URL: sqlnbfmt-0.1.4.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for sqlnbfmt-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1553e08e2100a57585da907bb16692407fd52d3de067ee5c3b5e298e4af871ea
MD5 c2070ff4a2d22c7cde686ae78a67ba4b
BLAKE2b-256 503a3bdc74d45a5f367758402b751f71dc92150bc26800a83f8a41c3562055ea

See more details on using hashes here.

File details

Details for the file sqlnbfmt-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: sqlnbfmt-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for sqlnbfmt-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 32e0a8d54c3d09e9cd0fbf6fa000a0b32e153046c36b5fcef3cd6305690c0372
MD5 e6ec2a5b4bb2cac20792482fff789cb6
BLAKE2b-256 f2611035e91afebe916254121239b701d0ed0f95bd1f4bf49d3450ae96e13b6d

See more details on using hashes here.

Supported by

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