Skip to main content

Add your description here

Project description

nblite

A lightweight wrapper around nbdev for streamlined notebook-driven development

nblite simplifies the workflow between Jupyter notebooks, Python scripts, and module code, enhancing the notebook-driven development process.

Note: nblite is merely a wrapper around nbdev with some adjustments and additions adapted to the needs of the Autonomy Data Unit. Full credit of the concept and implementation of notebook-driven development using Jupyter notebooks should go to the creators of nbdev.

Installation

pip install nblite

Core Concepts

Code locations

Directories containing code in different formats (notebooks, scripts, modules). Each code location is defined in the nblite.toml configuration file and will store different representations of your code. Available formats are:

Format Format key File Extension
Python module module py
Jupyter notebook ipynb ipynb
Percent percent pct.py
Light light lgt.py
Sphinx sphinx spx.py
Myst myst myst.md
Pandoc pandoc pandoc.md

In the nblite.toml you define the code locations and the formats of the code within them:

[cl.nbs]
format="ipynb"
path="notebooks"

[cl.pts]
format="percent"
path="percent_notebooks"

[cl.lib]
format="module"
path="nblite"

Here we have defined three code locations (nbs, pts and lib) and specified their paths (relative to the project root) and their formats. Read more about plaintext notebook formats here.

Export pipeline

Defines the flow of code conversion between different code locations. For example, a typical pipeline might be:

nbs -> pts
pts -> lib

This means:

  1. Start with notebooks (.ipynb) as the source
  2. Convert them to percent scripts (.pct.py)
  3. Finally export to Python library modules (.py)

Notebook 'twins'

Corresponding versions of the same content in different formats. When you write a notebook my_notebook.ipynb, nblite can create twins like:

  • my_notebook.pct.py (percent script)
  • my_notebook.lgt.py (light script)
  • my_module/my_notebook.py (Python module)

These twins contain the same logical content but in different formats, allowing you to use the format that's most appropriate for the task at hand.

Why store plaintext versions?

While Jupyter notebooks (.ipynb) are excellent for interactive development, they pose challenges for version control systems like Git:

  1. Git-Friendly: Plaintext formats (.pct.py, .lgt.py, .py) are better handled by Git, making diffs and merge conflicts easier to resolve.
  2. GitHub UI: GitHub's interface more effectively displays changes in plaintext Python files compared to JSON-formatted notebook files.
  3. Code Review: Reviewing code changes is more straightforward with plaintext formats.
  4. Cleaner History: By cleaning notebook outputs before committing, you avoid polluting your Git history with large output cells and changing execution counts.
  5. Collaboration: Team members can work with the format they prefer—notebooks for exploration, Python files for implementation.

The export pipeline ensures that changes made in one format are propagated to all twins, maintaining consistency across representations.

Key Features

  • Export Pipeline: Convert notebooks between different formats (.ipynb, percent scripts, light scripts, and Python modules)
  • Documentation: Generate documentation from notebooks using Quarto
  • Git Integration: Clean notebooks and enforce consistent git commits
  • Parallel Execution: Execute notebooks in parallel for faster workflow
  • Export as Functions: Notebooks can be exported as functions

Quick Start

Initialize a project

# Create a new nblite project
nbl init --module-name my_project

Set up Git hooks

# Install pre-commit hooks for automatic notebook cleaning
nbl install-hooks

Git hooks ensure that notebooks are properly cleaned before committing. The pre-commit hook automatically:

  • Validates that notebooks are clean (removes metadata and outputs)
  • Ensures that all notebook twins are consistent
  • Prevents accidental commits of unclean notebooks

Create a new notebook

# Create a new notebook in a code location
nbl new nbs/my_notebook.ipynb

Fill Notebooks with Outputs

The nbl fill command is used to execute all the cells in all .ipynb notebooks.

nbl fill

This command also works as a testing command.

Prepare your project

# Export, clean, and fill notebooks in one command
nbl prepare

Configuration

nblite uses a TOML configuration file (nblite.toml) at the project root:

export_pipeline = """
nbs -> pts
pts -> lib
"""
docs_cl = "nbs"
docs_title = "My Project"

[cl.lib]
path = "my_module"
format = "module"

[cl.nbs]
format = "ipynb"

[cl.pts]
format = "percent"

Common Commands

Run nbl to see all available commands.

Export and Conversion

  • nbl export: Export notebooks according to the export pipeline
  • nbl convert <nb_path> <dest_path>: Convert a notebook between formats
  • nbl clear: Clear downstream code locations

Notebook Management

  • nbl clean: Clean notebooks by removing outputs and metadata
  • nbl fill: Execute notebooks and fill with outputs
  • nbl test: Test that notebooks execute without errors (dry run of fill)

Documentation

  • nbl readme: Generate README.md from index.ipynb
  • nbl render-docs: Render project documentation using Quarto
  • nbl preview-docs: Preview documentation

Git Integration

  • nbl git-add: Add files to git staging with proper cleaning
  • nbl validate-staging: Validate that staged notebooks are clean
  • nbl install-hooks: Install git hooks for the project

Development Workflow

  1. Write code in Jupyter notebooks (.ipynb)
  2. Run nbl export to convert to other formats
  3. Run nbl clean before committing to git
  4. Use nbl fill (or nbl test if outputs are not to be rendered) to verify your notebooks execute correctly
  5. Use nbl render-docs to generate documentation, or use nbl preview-docs to preview the documentation.

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

nblite-0.6.1.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

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

nblite-0.6.1-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file nblite-0.6.1.tar.gz.

File metadata

  • Download URL: nblite-0.6.1.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for nblite-0.6.1.tar.gz
Algorithm Hash digest
SHA256 837c86ab97eb22b6e266212c48c5081dd27445d7ad3245030e748c42de8af30b
MD5 043bdaddd9721f6c9f654d1b454ad812
BLAKE2b-256 d1649751981c4a61da618f0dd5084e78b8f2bac4e8db4d722b33862ee80294ee

See more details on using hashes here.

File details

Details for the file nblite-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: nblite-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for nblite-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 35e07df321ff8889d062647664fb05a60ef4fd9c639fc7753b2c591a235928c0
MD5 fb80b39bc20a6133d224fd845ea311e0
BLAKE2b-256 24f80b7376a69ab8523e7296d25a871cd9c50c33f42428ce43ee9715759a3949

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