简体   繁体   English

如何使用 Bash 在 Jupyter Notebook 中打印单元格的内容

[英]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.我有一个 .ipynb 文件,想在不运行本地 jupyter 笔记本服务器的情况下查看内容。

I try with cat my-file.ipynb but this outputs a lot of json that's hard to read.我尝试使用cat my-file.ipynb但这会输出很多难以阅读的 json。 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.如果我之前运行过笔记本,有时这个outputs值会很长。 All I want is the value of source .我想要的只是source的价值。 I don't have access to jq .我无权访问jq

How can I print the contents of all source blocks with just grep , sed , awk , or some other simple bash command? 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.第二个脚本首先移动到文件末尾,然后向上移动 1,然后删除该行和文件末尾之间的所有内容。 I did it this way in order to use relative line numbering, so it will work regardless of how long the "source" key is.我这样做是为了使用相对行编号,因此无论“源”键有多长,它都会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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