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 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
- Install pre-commit:
pip install pre-commit
- Add to
.pre-commit-config.yaml
:
repos:
- repo: https://github.com/flyersworder/sqlnbfmt
rev: v0.1.1
hooks:
- id: sqlnbfmt
name: sqlnbfmt
types: [jupyter]
args: [--config, config.yaml, --dialect, postgres]
Please fun the following command in your CMD 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
uv
to 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
File details
Details for the file sqlnbfmt-0.1.6.tar.gz
.
File metadata
- Download URL: sqlnbfmt-0.1.6.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2e616891d9dbf36daf6e2b931d520de6e7683f4e54fec829c951c442249ce68c |
|
MD5 | 6d945aacb9f7a548afa1239f37a97044 |
|
BLAKE2b-256 | e217c3ea5262a0810ffaeb505cf1409120a47da9227ef8ad5c14a408cb3040da |
File details
Details for the file sqlnbfmt-0.1.6-py3-none-any.whl
.
File metadata
- Download URL: sqlnbfmt-0.1.6-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ecc9f0a0f4aac47479a8384ac8efdbe91cb8da4c66094d64733fd261fc45895 |
|
MD5 | 1dbde2e8cdcd00d6c321e5a13f42e5cd |
|
BLAKE2b-256 | 2b210df8800d414d6ae800c760e3763e511f44e3afc9e27f91d97d6859564601 |