Skip to main content

A package to run Jupyter notebook cells from the command line.

Project description

cell_runner

cell_runner is a Python command-line tool that allows you to execute a specific code cell from a Jupyter notebook (.ipynb file) directly from the command line. It is particularly useful when you want to run a single code cell from a notebook without opening the Jupyter interface.

Features

  • Execute any specific code cell from a Jupyter notebook (.ipynb file) via CLI.
  • Works without requiring the entire notebook to be executed or opened.
  • Simple and easy-to-use.

Installation

You can install cell_runner locally by running the following command:

pip install cell-runner

Requirements

Make sure you have the following dependencies installed before using cell_runner:

  • Python 3.6 or higher
  • Jupyter (for working with .ipynb files)
  • json
  • subprocess
  • argparse (comes with Python 3.6+)

You can install the required packages using pip:

pip install -r requirements.txt

Usage

Once installed, you can use the cell_runner command to run a code cell from a Jupyter notebook.

Command Syntax

cell_runner <notebook_file> <cell_index>
  • <notebook_file>: The path to the .ipynb file (Jupyter notebook).
  • <cell_index>: The index of the code cell to execute (starting from 0).

Examples

1. Running a specific cell from a notebook

To execute the code in the 3rd cell (index 2) of a notebook named example.ipynb:

cell_runner example.ipynb 2

2. Handling Errors

  • If the file is not a Jupyter notebook: If you pass a file that does not end with .ipynb, you'll see an error:

    File must be a .ipynb file
    
  • If the cell index is out of range: If you specify an index that is out of the range of available code cells, the tool will raise an error:

    Cell index is out of range. Please check the cell index. (Index starts from 0)
    

Example Notebook

Suppose example.ipynb has the following cells:

  • Cell 0:

    print("Cell 0")
    
  • Cell 1:

    print("Cell 1")
    
  • Cell 2:

    print("Cell 2")
    

Running the command:

cell_runner example.ipynb 1

Will execute the code in Cell 1, and you will see the following output:

Cell 1

Testing

To test the functionality of the cell_runner package, you can use the following steps:

  1. Create a test Jupyter notebook (test.ipynb) with several code cells.
  2. Run the cell_runner command on various cell indices to ensure that the correct cell is executed.

For example, to run a test on the 1st cell (index 0):

cell_runner test.ipynb 0

Additionally, you can add automated tests using pytest:

pip install pytest
pytest tests/

Make sure you have test cases in the tests/ directory.

Development

To contribute or modify the package, follow these steps:

  1. Clone the repository:

    git clone <repository-url>
    cd my_python_package
    
  2. Install the package in editable mode:

    pip install -e .
    
  3. Modify the code as needed in the cell_runner directory.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

cell_runner-0.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

cell_runner-0.2-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file cell_runner-0.2.tar.gz.

File metadata

  • Download URL: cell_runner-0.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for cell_runner-0.2.tar.gz
Algorithm Hash digest
SHA256 37d21ae0c2c62364b081cfcca8531bc961ff80c4e2e0daabc504dea97d633a21
MD5 2207b240b83488e0d86d469fed7ec728
BLAKE2b-256 edc8aa4189472d78a3b77aa2c3322af7b9d1910d032575610a035af06f60f87f

See more details on using hashes here.

File details

Details for the file cell_runner-0.2-py3-none-any.whl.

File metadata

  • Download URL: cell_runner-0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.5

File hashes

Hashes for cell_runner-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f98fac0962562d2785cbac5b4ebf2b3f61333d8ce8cd479cdb20731f71cbee78
MD5 4243f19e8382645d8f378618480e4863
BLAKE2b-256 4f835ed69de340ddc9a1948e30b0744af0f16da2174da8e33a6163ee84da31f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page