Jupyter Notebook中显示/隐藏代码块

对于用Jupyter Notebook编写的python脚本,如果可以将较长的代码部分隐藏,只显示Markdown说明文本、和代码块的运行结果,便可将脚本直接用作动态分析报告。即在保持可读性的同时,可通过刷新原数据/调整代码即时生成新的报告内容。

实现显示/隐藏代码块的步骤:
  1. 安装jupyter_contrib_nbextentions
    (Ref: https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html#install-the-python-package
    conda安装命令:conda install -c conda-forge jupyter_contrib_nbextensions
    python安装命令:pip install jupyter_contrib_nbextensions

  2. 安装jupyter_nbextentions_contributor,以便在Jupyter Notebook页面设置nbextentions
    (Ref: https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator
    conda安装命令:conda install -c conda-forge jupyter_nbextensions_configurator
    python安装命令:pip install jupyter_nbextensions_configurator

  3. 安装完成后,点击Jupyter Notebook首页的Nbextensions标签,打开在Nbextensions configuration设置页面。
    1)勾选Hide input(显示/隐藏选定代码块)
    2)勾选Hide input all(显示/隐藏所有代码块)


    notebook_configure_LI.jpg
  1. 打开任意python脚本文件,可看到工具栏中已添加相应的按钮。


    notebook_hidecode_LI.jpg
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。