![](/img/trans.png)
[英]how can I download data to jupyter notebook in kubeflow from minio?
[英]How can I download (save) all the codes from Jupyter Notebook?
我在 Jupyter Notebook 中编写了许多程序。 我一直在手动下载 HTML 和 python 格式。 我想问是否有更快的方法来下载我在文件夹中编写的所有程序。
非常感谢
Jupyter notebook 是一个 JSON 文件。
您可以将其作为文本文件打开并将所有具有code
的单元格保存为cell_type
。
import json
PATH_TO_NOTEBOOK = r"/home/yam/notebook.ipynb"
def get_notebook(notebook_path):
with open(notebook_path, 'r', encoding='utf-8') as notebook:
return json.load(notebook)
def is_code_cell(cell):
return cell['cell_type'] == "code"
def get_source_from_code_cell(cell):
return ''.join(cell['source'])
def save_as_python_file(filename, code):
with open(f'{filename}.py', 'w', encoding='utf-8') as f:
f.write(code)
def get_code_cells_content(notebook_cells):
yield from (
(i, get_source_from_code_cell(current_cell))
for i, current_cell in enumerate(notebook_cells, 1)
if is_code_cell(current_cell)
)
notebook = get_notebook(PATH_TO_NOTEBOOK)
for filename, code in get_code_cells_content(notebook['cells']):
save_as_python_file(filename, code)
试试这个。
$ ipython nbconvert --to FORMAT notebook.ipynb
中运行此命令。这会将 IPython 文档文件 notebook.ipynb 转换为 FORMAT 字符串给出的 output 格式。 默认的 output 格式为 HTML,其中--to
参数可以省略:
这意味着如果您运行ipython nbconvert notebook.ipynb
那么您的 notebook.ipynb 将转换为 HTML 文件。
$ ls
notebook.ipynb notebook.html notebook_files/
Jupyter 笔记本在您初始化 jupyter 的本地目录中保存为 .ipynb(ipython 笔记本)。
如果您只是想保存代码以供以后查找,您可以将笔记本保存为 PDF。
尝试这个
jupyter nbconvert path/to/your/ipynb --to=pdf --TemplateExporter.exclude_input=True
您可以从笔记本文件菜单中轻松完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.