Command Line Interface#
nbrefactor provides a CLI to easily refactor notebooks into a structured project hierarchy.
Basic CLI Usage#
To use the CLI, run the following command:
jupyter nbrefactor <notebook_path> <output_path> [OPTIONS]
notebook_path: Path to the Jupyter notebook file you want to refactor.output_path: Directory where the refactored Python modules will be saved.
CLI Arguments#
Argument |
Type |
Default |
|---|---|---|
|
str |
(Required) |
|
str |
(Required) |
|
str |
|
|
flag |
|
|
str |
|
|
flag |
|
Example Usages#
Basic Refactor: Refactor a notebook and save the output in a specified directory:
jupyter nbrefactor notebook.ipynb output_dir
Specify a Root Package: Refactor and assign a root package name:
jupyter nbrefactor notebook.ipynb output_dir --root-package my_package
Generate a Plot: Refactor and generate a plot of the module hierarchy:
jupyter nbrefactor notebook.ipynb output_dir --generate-plot
Generate a Plot with Specific Format: Generate a plot in PNG format:
jupyter nbrefactor notebook.ipynb output_dir --generate-plot --plot-format png
Generate __init__.py Files: Refactor and generate __init__.py files in package directories:
jupyter nbrefactor notebook.ipynb output_dir --init-files
This is particularly useful when your refactored code uses relative imports between modules. The generated __init__.py files properly expose the modules, making Python’s import system work correctly.