nbmanips


Namenbmanips JSON
Version 2.1.0 PyPI version JSON
download
home_page
Summarynbmanips allows you easily manipulate notebook files
upload_time2024-02-29 00:23:34
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT License Copyright (c) 2021 Hmila Dhia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords dbc ipynb jupyter notebook notebooks slides zeppelin zpln
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # nbmanips
![PyPI](https://img.shields.io/pypi/v/nbmanips)
![PyPI - License](https://img.shields.io/pypi/l/nbmanips)
![PyPI - Wheel](https://img.shields.io/pypi/wheel/nbmanips)

![Tests](https://github.com/hmiladhia/nbmanips/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/hmiladhia/nbmanips/branch/main/graph/badge.svg)](https://codecov.io/gh/hmiladhia/nbmanips)

A collections of utilities to manipulate IPython/Jupyter Notebooks via a python script.

## I - Usage/Examples
### 1 - Basic usage
A simple example of using nbmanips:

```python
from nbmanips import Notebook

# Read ipynb file
nb = Notebook.read_ipynb("my_notebook.ipynb")

# delete empty cells
nb.select("empty").delete()

# save ipynb file
nb.to_ipynb("new_notebook.ipynb")
```

Examples of operations you can perform on a Notebook:

- `replace`: Replace matching text in the selected cells
- `tag`: Add metadata to the selected cells
- `erase`: Erase the content of the selected cells
- `delete`: Delete the selected cells
- `keep`: Kepp the selected cells

### 2 - Selectors
To select cells on which to apply the previous operations, you can use:

- The cell number

```python
nb[0].show()

# OR
nb.select(0).show()
```
- A slice object

```python
nb[1:6:2].show()

# OR
selected_cells = slice(1, 6, 2)

nb.select(selected_cells).show()
```
- A predefined selector. Available predefined selectors are the following:

    - `code_cells` / `markdown_cells` / `raw_cells`: Selects cells with the given type
    - `contains`: Selects Cells containing a certain text.
    - `is_empty` / `empty`: Selects empty cells
    - `has_output`: Checks if the cell has any output
    - `has_output_type`: Select cells that have a given output_type
    - `has_slide_type`: Select cells that have a given slide type
    - `is_new_slide`: Selects cells where a new slide/subslide starts
    - `has_byte_size`: Selects cells with byte size within a given range of values.

```python
# Show Markdown Cells
nb.select('markdown_cells').show()

# Show Cells containing the equal sign
nb.select('contains', '=').show()
```



- A function that takes a Cell object and returns True if the cell should be selected
```python
# Show Cells with length > 10
nb.select(lambda cell: len(cell.source) > 10).show()
```
- A list of Selectors
```python
# Show Empty Markdown Cells
nb.select(['markdown_cells', 'is_empty']).show()

# Show Markdown or Code Cells
nb.select(['markdown_cells', 'code_cells'], type='or').show()
```

### 3 - Export Formats
You can export the notebooks to these formats:

- to_ipynb
- to_dbc
- to_html
- to_slides (using reveal.js)
- to_md (to markdown)
- to_py (to python)
- to_text (textual representation of the notebook)

### 4 - Slide manipulations
You can manipulate the slides by tagging which cells to keep and which to skip.
The following actions are available:

- set_slide
- set_subslide
- set_skip
- set_fragment
- set_notes

A neat trick is to use `auto_slide` method to automatically create slides out of your notebook:
```python
from nbmanips import Notebook

# Read ipynb file
nb = Notebook.read_ipynb("my_notebook.ipynb")

# Automatically create slides
nb.auto_slide()

# Export to Reveal.js slides (HTML)
nb.to_slides("new_slides.slides.html", theme='beige')
```

## II - CLI
### 1 - Show a notebook
To get a readable representation of the notebook
```bash
nb show my_notebook.ipynb
```

Other options are available. For example, you can customize the style, weather to truncate the output of cells:
```bash
nb show -s double -t 100 my_notebook.ipynb
```

To show a subset of the notebook cells, you can perform a select operation:
```bash
nb select 0:3 | nb show my_notebook.ipynb

# Or if you're using negative indexes ( to show the last 3 cells )
nb select [-3:] | nb show my_notebook.ipynb
```
### 2 - Basic usage
A simple example of using nbmanips via the cli:

```bash
# delete empty cells
nb select empty | nb delete my_notebook.ipynb --output new_notebook.ipynb

# Or equivalently:
nbmanips select empty | nbmanips delete my_notebook.ipynb --output new_notebook.ipynb
```

You could also show the table of contents of a certain notebook:
```bash
nb toc nb.ipynb
```

Or split a notebook into multiple notebooks:

```bash
nb split nb.ipynb 5,9
```

### 3 - Export Formats
You can convert a notebook to the following formats:

- html: `nb convert html my_notebook.ipynb --output my_notebook.html`
- slides (using reveal.js): `nb convert slides my_notebook.ipynb --output my_notebook.slides.html`
- md (to markdown): `nb convert md my_notebook.ipynb --output my_notebook.md`
- py (to python): `nb convert py my_notebook.ipynb --output my_notebook.py`

### 4 - Slide manipulations
```bash
# Automatically set slides
nb auto-slide -f my_notebook.ipynb

# Generate a my_notebook.slides.html file
nb convert slides my_notebook.ipynb
```

Or if you do not wish to modify your original notebook:
```bash
# Automatically set slides
nb auto-slide my_notebook.ipynb -o my_temp_notebook.ipynb

# Generate a my_notebook.slides.html file
nb convert slides my_temp_notebook.ipynb -o my_notebook.slides.html
```

If you need more details you can check the --help option:
```
nbmanips --help
```

## III - Optional Requirements

There are optional requirements you may want to install to render images in the terminal.
The results, however, are not always convincing.
If you want to enable this feature, you can just run the following command:

```bash
pip install nbmanips[images]
```

## Roadmap

- Add Custom Templates

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "nbmanips",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Dhia Hmila <dhiahmila.dev@gmail.com>",
    "keywords": "dbc,ipynb,jupyter,notebook,notebooks,slides,zeppelin,zpln",
    "author": "",
    "author_email": "Dhia Hmila <dhiahmila.dev@gmail.com>",
    "download_url": "",
    "platform": null,
    "description": "# nbmanips\n![PyPI](https://img.shields.io/pypi/v/nbmanips)\n![PyPI - License](https://img.shields.io/pypi/l/nbmanips)\n![PyPI - Wheel](https://img.shields.io/pypi/wheel/nbmanips)\n\n![Tests](https://github.com/hmiladhia/nbmanips/actions/workflows/tests.yml/badge.svg)\n[![codecov](https://codecov.io/gh/hmiladhia/nbmanips/branch/main/graph/badge.svg)](https://codecov.io/gh/hmiladhia/nbmanips)\n\nA collections of utilities to manipulate IPython/Jupyter Notebooks via a python script.\n\n## I - Usage/Examples\n### 1 - Basic usage\nA simple example of using nbmanips:\n\n```python\nfrom nbmanips import Notebook\n\n# Read ipynb file\nnb = Notebook.read_ipynb(\"my_notebook.ipynb\")\n\n# delete empty cells\nnb.select(\"empty\").delete()\n\n# save ipynb file\nnb.to_ipynb(\"new_notebook.ipynb\")\n```\n\nExamples of operations you can perform on a Notebook:\n\n- `replace`: Replace matching text in the selected cells\n- `tag`: Add metadata to the selected cells\n- `erase`: Erase the content of the selected cells\n- `delete`: Delete the selected cells\n- `keep`: Kepp the selected cells\n\n### 2 - Selectors\nTo select cells on which to apply the previous operations, you can use:\n\n- The cell number\n\n```python\nnb[0].show()\n\n# OR\nnb.select(0).show()\n```\n- A slice object\n\n```python\nnb[1:6:2].show()\n\n# OR\nselected_cells = slice(1, 6, 2)\n\nnb.select(selected_cells).show()\n```\n- A predefined selector. Available predefined selectors are the following:\n\n    - `code_cells` / `markdown_cells` / `raw_cells`: Selects cells with the given type\n    - `contains`: Selects Cells containing a certain text.\n    - `is_empty` / `empty`: Selects empty cells\n    - `has_output`: Checks if the cell has any output\n    - `has_output_type`: Select cells that have a given output_type\n    - `has_slide_type`: Select cells that have a given slide type\n    - `is_new_slide`: Selects cells where a new slide/subslide starts\n    - `has_byte_size`: Selects cells with byte size within a given range of values.\n\n```python\n# Show Markdown Cells\nnb.select('markdown_cells').show()\n\n# Show Cells containing the equal sign\nnb.select('contains', '=').show()\n```\n\n\n\n- A function that takes a Cell object and returns True if the cell should be selected\n```python\n# Show Cells with length > 10\nnb.select(lambda cell: len(cell.source) > 10).show()\n```\n- A list of Selectors\n```python\n# Show Empty Markdown Cells\nnb.select(['markdown_cells', 'is_empty']).show()\n\n# Show Markdown or Code Cells\nnb.select(['markdown_cells', 'code_cells'], type='or').show()\n```\n\n### 3 - Export Formats\nYou can export the notebooks to these formats:\n\n- to_ipynb\n- to_dbc\n- to_html\n- to_slides (using reveal.js)\n- to_md (to markdown)\n- to_py (to python)\n- to_text (textual representation of the notebook)\n\n### 4 - Slide manipulations\nYou can manipulate the slides by tagging which cells to keep and which to skip.\nThe following actions are available:\n\n- set_slide\n- set_subslide\n- set_skip\n- set_fragment\n- set_notes\n\nA neat trick is to use `auto_slide` method to automatically create slides out of your notebook:\n```python\nfrom nbmanips import Notebook\n\n# Read ipynb file\nnb = Notebook.read_ipynb(\"my_notebook.ipynb\")\n\n# Automatically create slides\nnb.auto_slide()\n\n# Export to Reveal.js slides (HTML)\nnb.to_slides(\"new_slides.slides.html\", theme='beige')\n```\n\n## II - CLI\n### 1 - Show a notebook\nTo get a readable representation of the notebook\n```bash\nnb show my_notebook.ipynb\n```\n\nOther options are available. For example, you can customize the style, weather to truncate the output of cells:\n```bash\nnb show -s double -t 100 my_notebook.ipynb\n```\n\nTo show a subset of the notebook cells, you can perform a select operation:\n```bash\nnb select 0:3 | nb show my_notebook.ipynb\n\n# Or if you're using negative indexes ( to show the last 3 cells )\nnb select [-3:] | nb show my_notebook.ipynb\n```\n### 2 - Basic usage\nA simple example of using nbmanips via the cli:\n\n```bash\n# delete empty cells\nnb select empty | nb delete my_notebook.ipynb --output new_notebook.ipynb\n\n# Or equivalently:\nnbmanips select empty | nbmanips delete my_notebook.ipynb --output new_notebook.ipynb\n```\n\nYou could also show the table of contents of a certain notebook:\n```bash\nnb toc nb.ipynb\n```\n\nOr split a notebook into multiple notebooks:\n\n```bash\nnb split nb.ipynb 5,9\n```\n\n### 3 - Export Formats\nYou can convert a notebook to the following formats:\n\n- html: `nb convert html my_notebook.ipynb --output my_notebook.html`\n- slides (using reveal.js): `nb convert slides my_notebook.ipynb --output my_notebook.slides.html`\n- md (to markdown): `nb convert md my_notebook.ipynb --output my_notebook.md`\n- py (to python): `nb convert py my_notebook.ipynb --output my_notebook.py`\n\n### 4 - Slide manipulations\n```bash\n# Automatically set slides\nnb auto-slide -f my_notebook.ipynb\n\n# Generate a my_notebook.slides.html file\nnb convert slides my_notebook.ipynb\n```\n\nOr if you do not wish to modify your original notebook:\n```bash\n# Automatically set slides\nnb auto-slide my_notebook.ipynb -o my_temp_notebook.ipynb\n\n# Generate a my_notebook.slides.html file\nnb convert slides my_temp_notebook.ipynb -o my_notebook.slides.html\n```\n\nIf you need more details you can check the --help option:\n```\nnbmanips --help\n```\n\n## III - Optional Requirements\n\nThere are optional requirements you may want to install to render images in the terminal.\nThe results, however, are not always convincing.\nIf you want to enable this feature, you can just run the following command:\n\n```bash\npip install nbmanips[images]\n```\n\n## Roadmap\n\n- Add Custom Templates\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2021 Hmila Dhia  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "nbmanips allows you easily manipulate notebook files",
    "version": "2.1.0",
    "project_urls": {
        "Issues": "https://github.com/hmiladhia/nbmanips/issues",
        "Repository": "https://github.com/hmiladhia/nbmanips.git"
    },
    "split_keywords": [
        "dbc",
        "ipynb",
        "jupyter",
        "notebook",
        "notebooks",
        "slides",
        "zeppelin",
        "zpln"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "43ca598e51762f71683b38bd0c5a3ad08f5756304cc1a7b353951867c63c67e6",
                "md5": "88190b39b088e1ddac36415eb24c458c",
                "sha256": "71d4f46871278151004fdf14df9f78e9aefc985ddfcc3221313b859030ac2921"
            },
            "downloads": -1,
            "filename": "nbmanips-2.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "88190b39b088e1ddac36415eb24c458c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 40355,
            "upload_time": "2024-02-29T00:23:34",
            "upload_time_iso_8601": "2024-02-29T00:23:34.126621Z",
            "url": "https://files.pythonhosted.org/packages/43/ca/598e51762f71683b38bd0c5a3ad08f5756304cc1a7b353951867c63c67e6/nbmanips-2.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-29 00:23:34",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hmiladhia",
    "github_project": "nbmanips",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "nbmanips"
}
        
Elapsed time: 0.21261s