A tool to format SQL code in Jupyter notebooks.
Project description
sqlnbfmt
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 Python comments, query parameters (e.g.,
%s,?), and SQL comments - ⚙️ Highly Configurable: Customize formatting through YAML configuration
- 🔄 Pre-commit Ready: Seamlessly integrates with pre-commit hooks
- 📦 Lightweight: Only three runtime dependencies (sqlglot, nbformat, pyyaml)
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
- Install pre-commit:
pip install pre-commit
- Add to
.pre-commit-config.yaml:
repos:
- repo: https://github.com/flyersworder/sqlnbfmt
rev: v0.2.0
hooks:
- id: sqlnbfmt
name: sqlnbfmt
types: [jupyter]
args: [--config, config.yaml, --dialect, postgres]
Please run the following command for help:
sqlnbfmt -h
- Install the hook:
pre-commit install
- (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:
- Clone the repository:
git clone https://github.com/flyersworder/sqlnbfmt.git
cd sqlnbfmt
- Use
uvto sync the environment:
uv sync
source .venv/bin/activate # On Windows: .venv\Scripts\activate
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlnbfmt-0.2.0.tar.gz.
File metadata
- Download URL: sqlnbfmt-0.2.0.tar.gz
- Upload date:
- Size: 12.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6f344bc7d2b68ba524960e3f0f655972c19b26e74c019b9f1be7dd8f3087547
|
|
| MD5 |
99d38f48fc58bce53d728c3ab936080c
|
|
| BLAKE2b-256 |
dd42f176d88b569301d6eebcee93ca324187266825f83b6284b7036060f50f3d
|
Provenance
The following attestation bundles were made for sqlnbfmt-0.2.0.tar.gz:
Publisher:
ci-and-publish.yml on flyersworder/sqlnbfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlnbfmt-0.2.0.tar.gz -
Subject digest:
e6f344bc7d2b68ba524960e3f0f655972c19b26e74c019b9f1be7dd8f3087547 - Sigstore transparency entry: 1107726120
- Sigstore integration time:
-
Permalink:
flyersworder/sqlnbfmt@a2c54faae2e87a22f8d081ebf78ec41dffa46622 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/flyersworder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-and-publish.yml@a2c54faae2e87a22f8d081ebf78ec41dffa46622 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sqlnbfmt-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sqlnbfmt-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78b8f0a20af0a1b3e3d82e3a39743a442126e5d8206510a9502e2c10766d43d4
|
|
| MD5 |
ad415f8252958f8ccdb71955aa3cadaa
|
|
| BLAKE2b-256 |
21c131f3ee4ac76d0ef6d09f8ab6b31921dacc8cd87b480d2269726d54971848
|
Provenance
The following attestation bundles were made for sqlnbfmt-0.2.0-py3-none-any.whl:
Publisher:
ci-and-publish.yml on flyersworder/sqlnbfmt
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlnbfmt-0.2.0-py3-none-any.whl -
Subject digest:
78b8f0a20af0a1b3e3d82e3a39743a442126e5d8206510a9502e2c10766d43d4 - Sigstore transparency entry: 1107726122
- Sigstore integration time:
-
Permalink:
flyersworder/sqlnbfmt@a2c54faae2e87a22f8d081ebf78ec41dffa46622 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/flyersworder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci-and-publish.yml@a2c54faae2e87a22f8d081ebf78ec41dffa46622 -
Trigger Event:
release
-
Statement type: