Skip to main content

QiTangle - IPython magic to tangle (quarto) specified code cells into python source files

Project description

QiTangle - IPython magic to entangle code from notebooks.

export code from notebook cells to python source files

The first Quatro document I created was used to implement a python module based on some math formula’s from an article (about measuring sap flux in tree stems) LaTeX formated formula’s. Step by step functions were build and documented along the formula’s and some example calculations. This worked like a charm but the disadvantage was that I had to copy the code from the document into a separate python file for use in our runtime environment. It worked fine but the disadvantage to this approach is that changes in one document had to be manually copied and paste into another. Such an arrangement will inevitably lead to errors.

What I needed was a way to convert or export selected code cells into a python source code file automatically and I found this in NBDev. Using it worked fine for a while, NBDev exports your code, builds the documentation and, if desired, packages the code uploads them to PyPi. I build packages with it that I relied on. But it is pretty big, requires projects to be laid out in a certain way and occasionally the PyPi packaging fails.

So, something smaller, without the PyPi packaging, was needed and I might as well look back at the original need of simply extracting or exporting python sourcefiles from Quarto and Jupyter documents.

Install

pip install qitangle

Usage

Loading the extension

import qitangle
%reload_ext qitangle.entangle

With %reload_ext we prevent errors when rerunning this cell, something that is likely to happen in a notebook.

Entangling

Entangling is the process of exporting the individual code cells to a file in the order as specified by the cell-specification and dependencies parameters.

Each entangle invocation adds a cell to a collection, the label and dependencies determine which cells depend on which and thus ultimately the order in which they are exporting by ensuring (if possible) that the dependencies are exported before the code cell.

With --export all cells in the collection are entangled and exported in this order.

Optionally each cell can be reformated through autopep8 with --fix. When combined with --export, this will reformat the entire produced file using Black.

%%entangle

It takes the following options:

  • -e, --export: Export all cells in the collection
  • -f, --fix: Fix/reFormat code with autopep8

And required arguments:

  • cell-specification: The modulepath and label for the code cell (required)
  • dependencies: The labels of dependencies for the code cell (optional)

The format of the cell-specification and dependencies is:

cell-specifiaction = module-path SEMICOLON cell-label
cell-label = name
dependencies = name *(SPACE name)
module-path = name *(DOT name)
name = 1*TEXTDATA

A module-path looks pretty much the same as in a python import statement, the cell-label is a name for the cell within the collection of cells - or python file - specified by the module-path and the dependencies are the labels of other cells within the specified collection upon which this cell depends and which are exported before this cell, if possible.

View documentation

You can view the documentation local with (and a local .venv active)

quarto preview docs/index.qmd

Or (with a local .venv active)

quarto render docs

from docs/_site

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

qitangle-0.4.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

qitangle-0.4.0-py3-none-any.whl (19.6 kB view details)

Uploaded Python 3

File details

Details for the file qitangle-0.4.0.tar.gz.

File metadata

  • Download URL: qitangle-0.4.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for qitangle-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9aa29701cc849d1481357f5b1a0ec413e8095b208419b7076fa5daadcb70ca25
MD5 421f469d8c1e5919c0a498b72a9f392d
BLAKE2b-256 597d57947e13855e927eee8fd4acab7baf57c35adcf4056420c0e3c1728f3b59

See more details on using hashes here.

File details

Details for the file qitangle-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: qitangle-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.5

File hashes

Hashes for qitangle-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf0f25a175d948f15552eb336653087b6dbf056b58ddcb7b2888cd2de9427159
MD5 274e50141537ffc3e44c2d670ca47ebc
BLAKE2b-256 815884340cf77a667903f46aae3b0c554a42d76a164148bc59cf8fa505305d3f

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