【问题标题】:jupyter notebook - run notebook (and start running cells) from command line and access it from browserjupyter notebook - 从命令行运行笔记本(并开始运行单元)并从浏览器访问它
【发布时间】:2020-02-27 08:29:29
【问题描述】:

我想执行 jupyter notebook - 但能够连接到浏览器以监控进度全部来自命令行 >)。

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

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

最好, 哈雷尔

【问题讨论】:

    标签: amazon-web-services jupyter-notebook ipython jupyter jupyter-lab


    【解决方案1】:

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

    答案基于观察到,如果您执行 javascript“Jupyter.notebook.execute_all_cells()”,所有单元格都会重新计算 在托管 .ipynb(带有检查元素)的浏览器控制台中

    很遗憾,我无法直接执行此操作。 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()
    
    

    【讨论】:

    • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
    • O,那么也许我误解了这个问题,因为我的解决方案确实允许您启动 ipynb 并在浏览器中监控它的进度。我误会了什么?
    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 2017-03-15
    • 2016-05-30
    • 2018-09-23
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多