nbqol.process.reformat#
Notebook conversion utilities for extending nbconvert functionality.
This module provides extended notebook conversion capabilities beyond standard nbconvert, including VSCode-compatible interactive Python scripts, cleaning options to remove non-essential content, and output conversion utilities.
Functions
|
Clean a Jupyter notebook by removing specified elements. |
|
Convert a Jupyter notebook to different formats with extended options. |
|
Convert a Jupyter notebook to a VSCode-compatible Python script with cell markers. |
|
Get the path to the current notebook. |
Classes
|
Notebook preprocessor that removes specified elements like markdown cells, intermediate executions, large outputs, or images. |
|
Exports a basic HTML document. |
|
Exports to a markdown document (.md) |
|
A dict-like node with attribute-access |
|
PurePath subclass that can make system calls. |
|
A configurable preprocessor |
|
Exports a Python code file. |
|
Exporter that converts Jupyter notebooks to VSCode-compatible Python scripts with cell markers. |
|
Converts ipywidget outputs to other formats for better compatibility or static rendering. |
- convert_to_vscode_script(notebook_path_str: str | None = None, output_path: str | None = None) str[source]#
Convert a Jupyter notebook to a VSCode-compatible Python script with cell markers.
- Parameters:
notebook_path_str – Path to the notebook file. If None, uses the current notebook.
output_path – Path to save the converted script. If None, uses the same name as the notebook with a .py extension.
- Returns:
Path to the saved Python script.
Examples
>>> convert_to_vscode_script('my_notebook.ipynb', 'my_script.py') 'my_script.py'>>> # Convert current notebook >>> script_path = convert_to_vscode_script()
- clean_notebook(notebook_path_str: str | None = None, output_path: str | None = None, remove_markdown: bool = False, remove_intermediates: bool = False, remove_images: bool = False, remove_large_outputs: bool = False, large_output_threshold: int = 1000) str[source]#
Clean a Jupyter notebook by removing specified elements.
- Parameters:
notebook_path_str – Path to the notebook file. If None, uses the current notebook.
output_path – Path to save the cleaned notebook. If None, uses a ‘_clean’ suffix on the original name.
remove_markdown – If True, remove non-header markdown cells
remove_intermediates – If True, remove intermediate expressions
remove_images – If True, remove image outputs
remove_large_outputs – If True, remove outputs exceeding the threshold
large_output_threshold – Size threshold for large outputs in characters
- Returns:
Path to the saved cleaned notebook.
Examples
>>> clean_notebook('my_notebook.ipynb', remove_markdown=True, remove_images=True) 'my_notebook_clean.ipynb'
- convert_notebook(notebook_path_str: str | None = None, output_path: str | None = None, to_format: str = 'python', clean_options: dict | None = None, widget_format: str | None = None) str[source]#
Convert a Jupyter notebook to different formats with extended options.
- Parameters:
notebook_path_str – Path to the notebook file. If None, uses the current notebook.
output_path – Path to save the converted file. If None, determines based on format.
to_format – Target format (‘python’, ‘vscode_python’, ‘html’, ‘markdown’)
clean_options – Dictionary of cleaning options (see clean_notebook)
widget_format – How to convert ipywidgets (‘html’, ‘text’, or None to keep as-is)
- Returns:
Path to the saved converted file.
Examples
>>> convert_notebook('my_notebook.ipynb', to_format='vscode_python', ... clean_options={'remove_markdown': True}, ... widget_format='html') 'my_notebook.py'
- class VSCodeCellExporter(**kwargs: Any)[source]#
Bases:
PythonExporterExporter that converts Jupyter notebooks to VSCode-compatible Python scripts with cell markers.
This exporter creates Python files that can be executed cell-by-cell in VSCode using the #%% cell markers that VSCode recognizes for interactive execution.
- class CleaningPreprocessor(**kwargs: Any)[source]#
Bases:
PreprocessorNotebook preprocessor that removes specified elements like markdown cells, intermediate executions, large outputs, or images.
- __init__(remove_markdown: bool = False, remove_intermediates: bool = False, remove_images: bool = False, remove_large_outputs: bool = False, large_output_threshold: int = 1000, **kwargs)[source]#
Initialize the cleaning preprocessor.
- Parameters:
remove_markdown – If True, remove all non-header markdown cells
remove_intermediates – If True, remove cells that don’t affect later execution
remove_images – If True, remove image outputs
remove_large_outputs – If True, remove outputs exceeding the threshold
large_output_threshold – Size threshold in characters for large outputs
**kwargs – Additional arguments
- class WidgetConverter(**kwargs: Any)[source]#
Bases:
PreprocessorConverts ipywidget outputs to other formats for better compatibility or static rendering.