简体   繁体   中英

How to print contents of cells in Jupyter Notebook with Bash

I have a.ipynb file and would like to view the contents without running a local jupyter notebook server.

I try with cat my-file.ipynb but this outputs a lot of json that's hard to read. Something like:

  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [.............],
   "source": [
    "import os\n",
    "import sys\n",
    "from pyspark.sql import SparkSession\n",
    "import pyspark.sql.functions as F\n",
    ...
   ]
  }

Sometimes this outputs value is very long if I've run the notebook before. All I want is the value of source . I don't have access to jq .

How can I print the contents of all source blocks with just grep , sed , awk , or some other simple bash command?

Ed can do this.

#!/bin/sh

cp my-file.ipynb stack
cat >> my-file.ed << EOF
/source/
+1
.,\$W source.txt
q
EOF

cat >> source.ed << EOF
$
-1
.,\$d
wq
EOF

ed -s stack < my-file.ed
ed -s source.txt < source.ed

rm -v ./my-file.ed
rm -v ./source.ed
rm -v ./stack

The first script searches down to the "source" word, then goes down one more, and then writes everything between it and the end of the file, to a seperate file.

The second script first moves to the end of the file, then moves back up 1, and then deletes everything between that line and the end of the file. I did it this way in order to use relative line numbering, so it will work regardless of how long the "source" key is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM