Skip to main content

Crunch Convert Tool

PyTest

This Python library is designed for the CrunchDAO Platform, exposing the conversion tools in a very small CLI.

Installation

Use pip to install the crunch-convert.

pip install --upgrade crunch-convert

Usage

Convert a Notebook

crunch-convert notebook ./my-notebook.ipynb --write-requirements --write-embedded-files
Show a programmatic way
from crunch_convert.notebook import extract_from_file
from crunch_convert.requirements_txt import CrunchHubWhitelist, format_files_from_imported

flatten = extract_from_file("notebook.ipynb")

# Write the main.py
with open("main.py", "w") as fd:
  fd.write(flatten.source_code)

# Map the imported requirements using the Crunch Hub's whitelist
whitelist = CrunchHubWhitelist()
requirements_files = format_files_from_imported(
  flatten.requirements,
  header="extracted from a notebook",
  whitelist=whitelist,
)

# Write the requirements.txt files (Python and/or R)
for requirement_language, content in requirements_files.items():
  with open(requirement_language.txt_file_name, "w") as fd:
    fd.write(content)

# Write the embedded files
for embedded_file in flatten.embedded_files:
  with open(embedded_file.normalized_path, "w") as fd:
    fd.write(embedded_file.content)

Freeze Requirements

crunch-convert requirements-txt freeze requirements.user.txt
Show a programmatic way
from crunch_convert import RequirementLanguage
from crunch_convert.requirements_txt import CrunchHubVersionFinder, CrunchHubWhitelist, format_files_from_named, freeze, parse_from_file

whitelist = CrunchHubWhitelist()
version_finder = CrunchHubVersionFinder()

# Open the requirements.txt to freeze
with open("requirements.txt", "r") as fd:
    content = fd.read()

# Parse it into NamedRequirement
requirements = parse_from_file(
    language=RequirementLanguage.PYTHON,
    file_content=content,
)

# Freeze them
frozen_requirements = freeze(
    requirements=requirements,

    # Only freeze if required by the whitelist
    freeze_only_if_required=True,
    whitelist=whitelist,

    version_finder=version_finder,
)

# Format the new requirements.txt using now frozen requirements
frozen_requirements_files = format_files_from_named(
    frozen_requirements,
    header="frozen from registry",
    whitelist=whitelist,
)

# Write to the new file
with open("requirements.frozen.txt", "w") as fd:
    content = frozen_requirements_files[RequirementLanguage.PYTHON]
    fd.write(content)

Features

Automatic line commenting

Only includes the functions, imports, and classes will be kept.

Everything else is commented out to prevent side effects when your code is loaded into the cloud environment. (e.g. when you're exploring the data, debugging your algorithm, or doing visualizating using Matplotlib, etc.)

You can prevent this behavior by using special comments to tell the system to keep part of your code:

  • To start a section that you want to keep, write: @crunch/keep:on
  • To end the section, write: @crunch/keep:off
# @crunch/keep:on

# keep global initialization
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# keep constants
TRAIN_DEPTH = 42
IMPORTANT_FEATURES = [ "a", "b", "c" ]

# @crunch/keep:off

# this will be ignored
x, y = crunch.load_data()

def train(...):
    ...

The result will be:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

TRAIN_DEPTH = 42
IMPORTANT_FEATURES = [ "a", "b", "c" ]

#x, y = crunch.load_data()

def train(...):
    ...

Ignore everything

To ignore everything when submitting, use the @crunch/keep:none command to exclude even imports, functions, and classes.

# @crunch/keep:none
from google.colab import files
files.download("test.joblib")

def score_local():
    ...

The result will be:

# from google.colab import files
# files.download("test.joblib")

# def score_local():
#     ...

Specifying package versions

Since submitting a notebook does not include a requirements.txt, users can instead specify the version of a package using import-level requirement specifiers in a comment on the same line.

# Valid statements
import pandas # == 1.3
import sklearn # >= 1.2, < 2.0
import tqdm # [foo, bar]
import sklearn # ~= 1.4.2
from requests import Session # == 1.5

Inconsistent versions

Specifying multiple times will cause the submission to be rejected if they are different.

# Inconsistant versions will be rejected
import pandas # == 1.3
import pandas # == 1.5

Standard libraries

Specifying versions on standard libraries does nothing (but they will still be rejected if there is an inconsistent version).

# Will be ignored
import os # == 1.3
import sys # == 1.5

Optional dependencies

If an optional dependency is required for the code to work properly, an import statement must be added, even if the code does not use it directly.

import castle.algorithms

# Keep me, I am needed by castle
import torch

Name conflicts

It is possible for multiple import names to resolve to different libraries on PyPI. If this happens, you must specify which one you want. If you do not want a specific version, you can use @latest, as without this, we cannot distinguish between commented code and version specifiers.

# Prefer https://pypi.org/project/EMD-signal/
import pyemd # EMD-signal @latest

# Prefer https://pypi.org/project/pyemd/
import pyemd # pyemd @latest

Ignore an import

If you do not want the process to add the package to the requirements.txt file, you can use @ignore as a version specifier.

# Ignore pandas, use already installed (if any; else, import error is expected!)
import pandas # @ignore

R imports via rpy2

For notebook users, the packages are automatically extracted from the importr("<name>") calls, which is provided by rpy2.

# Import the `importr` function
from rpy2.robjects.packages import importr

# Import the "base" R package
base = importr("base")

The following format must be followed:

  • The import must be declared at the root level.
  • The result must be assigned to a variable; the variable's name will not matter.
  • The function name must be importr, and it must be imported as shown in the example above.
  • The first argument must be a string constant, variables or other will be ignored.
  • The other arguments are ignored; this allows for custom import mapping if necessary.

The line will not be commented, read more about line commenting here.

Embedded Files

Additional files can be embedded in cells to be submitted with the Notebook. In order for the system to recognize a cell as an Embed File, the following syntax must be followed:

---
file: <file_name>.md
---

<!-- File content goes here -->
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Aenean rutrum condimentum ornare.

Submitting multiple cells with the same file name will be rejected.

While the focus is on Markdown files, any text file will be accepted. Including but not limited to: .txt, .yaml, .json, ...

User Warnings

The converter can detect potential issues that might arise when using the output files.

Nested imports

Importing a package into a function will generate a warning indicating that the package will not be used when the requirements.txt file is generated.

Global Constants

Global variables that are not guarded using the @crunch/keep:on command will likely be commented out. This means that referencing them in a nested scope without re-declaration will likely cause a guaranteed crash.

Contributing

Pull requests are always welcome! If you find any issues or have suggestions for improvements, please feel free to submit a pull request or open an issue in the GitHub repository.

License

MIT

Metadata

Release files for crunch-convert 0.12.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crunch-convert 0.12.1
File Size Uploaded
crunch_convert-0.12.1.tar.gz 24.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crunch-convert 0.12.1
File Interpreter ABI Platform
crunch_convert-0.12.1-py3-none-any.whl Python 3 none any Details

Total release size: 50.2 kB

Release files / crunch_convert-0.12.1.tar.gz

Download URL crunch_convert-0.12.1.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ba4bc33abfff71adf17148f96a6da030ad44237cf174529da4fe422b44b4e527
BLAKE2b-256 checksum
How to use checksums
e450da033acceb40e79bf651ad0579ad96879ecb6d99f398d7d3c6bfdef06bfc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / crunch_convert-0.12.1-py3-none-any.whl

Download URL crunch_convert-0.12.1-py3-none-any.whl
Size 25.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
076bf36021fcdc5daf1d12b79947bcea5b6b9c071ed1223fac4bbb51867cc555
BLAKE2b-256 checksum
How to use checksums
5472155f231e4e95ea42cedf3583590a48ac090fca30ddaedcf9e15005ffbd04
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

0.12.3

2 release files

0.12.2

2 release files

This release

0.12.1 This release

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page