[英]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.