[英]Get partial output from nbconvert.preprocessors.ExecutePreprocessor
有沒有辦法從 nbconvert.preprocessors.ExecutePreprocessor 獲取部分 output? 目前,我使用 ExecutePreprocessor 以編程方式執行我的 Jupyter notebook,它在執行整個 notebook 后返回 output。 但是,如果能夠在運行筆記本時獲取並保存部分結果,那就太好了。 例如,如果我在 jupyter 筆記本中有一個進度條,有沒有辦法不斷讀取更新的執行 output 以便我可以看到它更新?
這是我當前的代碼:
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
with open('./test2.ipynb') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(nb)
print(nb)
with open('executed_notebook.ipynb', 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
然而,能夠在執行時連續讀取 nb 變量並將其寫入文件會很棒
我最終做了這樣的事情
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
import threading
f = open('./test2.ipynb')
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(kernel_name='python3')
def save_notebook():
threading.Timer(1.0, save_notebook).start()
with open('executed_notebook.ipynb', 'w', encoding='utf-8') as f:
nbformat.write(nb, f)
save_notebook()
ep.preprocess(nb)
print('ended')
似乎工作得很好。 如果有人有更好的解決方案,請隨時發布
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.