How to select and export Jupyter cells by tag to a python script
Asked Answered
L

2

0

I'm looking for a work flow to extract specific cells from a notebook. If I can tag the cells I want to end up in the final script, how would I extract them?

I've tried nbmanips but it does not work as expected.

Loquacious answered 24/4, 2023 at 5:54 Comment(1)
Related point for anyone, you can really fine-tune programmatic selections using nbformat. It has the concepts of notebooks, cells, and tags baked in. See here for a related example with code and here for more a more general introduction to nbformat. I feel nbformat is more general than nbmanips as nbformat is an integrated part of the Jupyter ecosystem and thus your generated code would be more conveniently useful in more places going forward.Benefaction
L
1

Figured it out

from nbmanips import Notebook

nb = Notebook.read_ipynb("scratch.ipynb")

nb.select(lambda cell: cell.metadata != {} and 'prod' in cell.metadata.tags).keep()

nb.to_py("prod.py")
Loquacious answered 24/4, 2023 at 6:1 Comment(0)
P
0

little changed,and passed at jupyter notebook v6.4.11

from nbmanips import Notebook

nb = Notebook.read_ipynb("scratch.ipynb")

nb.select(lambda cell: 'tags' in cell.metadata and 'm01' in cell.metadata['tags']).keep()

nb.to_py("m01.py")
Pampero answered 3/6 at 18:28 Comment(1)
IMO This answer needs more background information.Yore

© 2022 - 2024 — McMap. All rights reserved.