I use ag
to search through my notes. My notes are written down in Markdown files and Markdown cells contained within Jupyter notebooks.
I can search the Markdown files conveniently with ag --markdown ...
. It would be very handy if something similar could be done with the Jupyter notebook files. But this would require that ag
understands the format of these notebooks.
My question: is there a way to search only the Markdown cells for a given string in a Jupyter notebook file? Any pattern matcher used in the solution is acceptable for me (ag
, grep
, ack
, ...).
p.s. The notebooks are composed in JSON. Here's a sample:
$ head notebook.ipynb
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"THIS IS A MARKDOWN STRING"
]
},
{
I'd look to use jq
to filter out all markdown cells of a python notebook. For instance, if you just wanted to spit out all the markdown source, you could use the following:
$< notebook.ipynb | jq '.cells[]|select(.cell_type == "markdown")|.source[]'
jq is fast, and used for far more elaborate solutions when saving ipython notebooks to git, for example: Using IPython notebooks under version control
I don't know if ag
can be interfaced with a filter, but to
get the Markdown out of a notebook file the following Python code will suffice
import nbformat
from sys import argv
nb = nbformat.read(argv[1], nbformat.NO_CONVERT)
for cell in nb.cells:
if cell.cell_type == 'markdown' : print(cell.source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With