从命令行运行笔记本(并开始运行单元格),然后从浏览器访问它



我想执行jupyter笔记本,但能够连接到浏览器,以便监控进度(全部来自命令行(。

为什么?我有多次长时间的培训,我想在AWS中的机器加载后立即加载,我不想每次都去浏览器按"启动并运行所有单元格"。

我看到我可以转换成.py文件(但之后我将无法监控训练进度(-所以这不好。在从命令行运行浏览器后,我需要它的交互性。

最佳,哈雷

我实际上在寻找相同的答案。目前,我正在使用一种变通方法来调整.jupyter文件夹中的custom.js文件。我想我可能会分享我的解决方案,以便其他用户能够改进它。

答案是基于这样一个观察结果,即如果执行javascript"Jupyter.notebook.execute_all_cells((",所有单元格都会重新计算在托管.ipynb(带有inspect元素(的浏览器控制台中

不幸的是,我没能直接做到这一点。Selenium可能会为一些用户提供答案,但由于公司政策,我无法安装Edge的Selenium驱动程序(我必须使用它(。因此,我设计了这个变通方法:

#this js code enforces all cells to be executed once the notebook is loaded.
js='''
if (Jupyter.notebook.kernel) {
Jupyter.notebook.execute_all_cells();
} else {
Jupyter.notebook.events.one('kernel_ready.Kernel', (e) => {
Jupyter.notebook.execute_all_cells();
});
}
'''
#now temprorarily append this piece of code to your custom.js file
custom_js_path = os.path.join(jupyter_dir, 'custom', 'custom.js')
if os.path.isfile(custom_js_path):
f = open(custom_js_path,"r+")
original = f.read()
f.write(js)
f.close()
else:
f = open(custom_js_path,"x")
f.write(js)
f.close()
# the command to start Jupyter notebooks and the notebook itself, which needs to be executed in a seperate thread since it is blocking calculations
def f():
subprocess.check_call(["jupyter", "notebook", "C:\Users\Untitled.ipynb"], shell=True)
return 
pool = ThreadPool(processes=1)
async_result = pool.apply_async(f)  
#... Wait till notebook is started
time.sleep(25)
# now revert the custom.js to its original state
with open(custom_js_path, "w") as f:
f.write(original)
f.close()

最新更新