Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cyyc1/jupyter-notebook-parser
A Python library to parse cell contents from Jupyter notebooks
https://github.com/cyyc1/jupyter-notebook-parser
jupyter jupyter-notebook jupyter-notebooks python
Last synced: about 2 months ago
JSON representation
A Python library to parse cell contents from Jupyter notebooks
- Host: GitHub
- URL: https://github.com/cyyc1/jupyter-notebook-parser
- Owner: cyyc1
- License: mit
- Created: 2022-10-08T07:27:02.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-07T14:51:37.000Z (about 1 year ago)
- Last Synced: 2024-09-20T01:18:34.150Z (3 months ago)
- Topics: jupyter, jupyter-notebook, jupyter-notebooks, python
- Language: Python
- Homepage: https://pypi.org/project/jupyter-notebook-parser/
- Size: 24.4 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jupyter-notebook-parser
A Python library to parse cell contents from Jupyter notebooks## Installation
```bash
pip install jupyter-notebook-parser
```## Usage
### Parser
```python
from jupyter_notebook_parser import JupyterNotebookParserparsed = JupyterNotebookParser('my_notebook.ipynb')
parsed.get_all_cells() # returns List[Dict], each Dict is a notebook cell
parsed.get_markdown_cells() # returns List[Dict], each Dict is a markdown cel
parsed.get_markdown_cell_indices() # returns List[int], each is a markdown cell's index
parsed.get_markdown_cell_sources() # returns List[str], each is a markdown cell's textparsed.get_code_cells() # returns List[Dict], each Dict is a code cell
parsed.get_code_cell_indices() # returns List[int], each int is a code cell's index
parsed.get_code_cell_sources() # returns List[SourceCodeContainer]source = parsed.get_code_cell_sources()[0]
source.raw_source # str
source.source_without_magic # str (all ipython magics excluded)
source.magics # Dict[int, str] (all magics, from line number to magic text)
```### Rewriter
```python
from jupyter_notebook_parser import JupyterNotebookParser
from jupyter_notebook_parser import JupyterNotebookRewriterparsed = JupyterNotebookParser('my_notebook.ipynb')
rewriter = JupyterNotebookRewriter(parsed_notebook=parsed)rewriter.replace_source_in_code_cell(index=5, new_source='print(2)')
```### Source code container
```python
from jupyter_notebook_parser import SourceCodeContainercontainer = SourceCodeContainer('a = 2\n%timeit b = 2 ** 10\nprint(b)')
container.raw_source # same as the input
container.source_without_magic # 'a = 2\n b = 2 ** 10\nprint(b)'
container.magics # {1: '%timeit'}
```