Skip to main content

nb2py

nb2py is a comprehensive tool designed to convert Jupyter Notebooks (.ipynb files) into Python scripts (.py files). This tool simplifies the process of transforming interactive notebook cells into a structured Python script, automatically separating import statements and wrapping executable code within an if __name__ == '__main__': guard to maintain the script's portability and usability.

This tool has two versions, a command-line version based on python and a vscode extension.

Features

  • VSCode Extension: A one-click solution for converting notebooks to python files.
  • Command-Line Simplicity: Offers a straightforward command-line interface for easy conversion of notebooks to Python scripts.
  • Import Optimization: Automatically moves all import statements to the top of the generated Python script.
  • Executable Wrap: Wraps all executable code within an if __name__ == '__main__': block, ensuring that the script can be imported without unintended execution.
  • Markdown Conversion: Converts markdown cells into Python comments, preserving the notebook's documentation in the script.
  • Handling Multiline Strings: Converts """ """ to multiple " "\ so there is no spacing issues. Especially useful for prompting LLMs. (added in v0.7.0)

VSCode Extension

Installation

You can install the nb2py extension directly from the Visual Studio Code Marketplace:

  1. Open VS Code.
  2. Navigate to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window.
  3. Search for nb2py. [if not found here is the link]
  4. Click on the Install button.

Usage

After installation, open the Jupyter Notebook you wish to convert. You'll see a new icon in the editor title bar that looks like a Python logo. Click this icon to convert the currently open notebook.

The converted Python script will be saved in the same directory as the notebook with the extension of .py.

Convert Notebook to Python Script Button

CLI Tool

Installation

To install nb2py, run the following setup script. Ensure you have Python 3.6 or later installed on your system.

pip install cli-nb2py

Usage

To convert a Jupyter Notebook to a Python script, use the following command:

nb2py input_notebook.ipynb output_script.py

If the output file name is not provided, nb2py will generate a Python script with the same name as the input notebook (replacing the .ipynb extension with .py).

License

nb2py is released under the MIT License. See the LICENSE file for more details.

Contact

Metadata

Release files for cli-nb2py 0.7.5

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

Source distribution (sdist)

Source distribution for cli-nb2py 0.7.5
File Size Uploaded
cli_nb2py-0.7.5.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cli-nb2py 0.7.5
File Interpreter ABI Platform
cli_nb2py-0.7.5-py3-none-any.whl Python 3 none any Details

Total release size: 10.2 kB

Release files / cli_nb2py-0.7.5.tar.gz

Download URL cli_nb2py-0.7.5.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2b3af33da1e89e0fec16e012db93cb55408954d96e859eb04b9ca3be342a4b47
BLAKE2b-256 checksum
How to use checksums
34814c441618816d2feb7564e5965bdacc5083744e86b7bd0525a668d2212733
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release files / cli_nb2py-0.7.5-py3-none-any.whl

Download URL cli_nb2py-0.7.5-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14c81bc92401ac1d9e71b8aa36e1e02cccc0509748b6e94079801bd95519f01e
BLAKE2b-256 checksum
How to use checksums
e4acdc96187abc1d7a4fb3bdbab07f2c898422dca08423b7bfdd3fccd6d946f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.7.5 This release

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

2 release files

0.5.0

2 release files

0.4.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