Open In App

How to Hide all Codes in Jupyter Notebook

Last Updated : 16 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Jupyter Notebooks have become indispensable tools for data scientists, researchers, and educators alike. They provide an interactive and dynamic environment for writing code, documenting processes, and presenting results. However, when it comes to sharing your work, particularly in lectures or presentations, the raw code in Jupyter Notebooks can sometimes be a distraction which can hamper your impact on the results. There are several techniques and methods that allow you to easily hide code cells while preserving the clarity of your explanations. In this article, we will cover all possible options to hide the code/output of the jupyter notebook cells.

Why Hide Code in Jupyter Notebook?

There are several benefits to hiding code in a Jupyter Notebook:

  1. Privacy: In certain situations, you may need to share your notebook with others while keeping the code concealed to safeguard sensitive data or proprietary algorithms.
  2. Hiding code simplifies documents, aiding non-technical readers’ focus on content.
  3. If you want your reports or presentations to look clean and professional, consider hiding code cells to make the document more reader-friendly.

How to hide code in Jupyter Notebook

There are different ways to hide code in Jupyter Notebook, depending on your preference and the level of control required over the visibility of your code. Below are some of the most frequently used techniques:

Using “Hide Input All” Extension

To use the “Hide Code (Report Mode)” feature in Jupyter Notebook, you must install the “Hide Input All” extension. Here are the steps:

  • Open Jupyter Notebook. Click “NbExtensions” tab in the top menu bar.
  • Search “Hide Input All” extension and click on the checkbox to enable it.
  • Click on Save

After installing the “Hide Input All” extension, you can turn on “Report Mode” in your Jupyter Notebook. Here are the steps:

  • Click on “View” tab then click on Cell Toolbar option.
  • Choose the “Hide input” option from the dropdown menu.

Now that you have enabled “Report Mode,” you can easily hide code cells from your notebook. Follow these steps:

  • Select code cell you want to hide.
  • Select Cell Toolbar button
  • Choose Hide input
  • You can hide multiple code cells simultaneously by selecting them and clicking the “Cell Toolbar” button.

To save and share your notebook after hiding code cells, simply save it and share it with others. When they open it, the code cells you’ve hidden won’t be visible to them.

Using Jupyter Notebook Toolbaar

  1. To hide the code of the cell, click on view. And Now click collapse select Code
  2. To hide the code of the whole notebook
    • Click on view
    • Click collapse all code

oo8-(1)

Using NBConvert to disable output for cells

To prevent the code from generating any output or simply disabling output of the cell :

  • In notebook toolbar
  • Click on the dropbox on the right which has a default value of ‘code’
  • Click on the option ‘Raw NBConvert’
  • This is disable output for the selected cell

oo12-(1)

Hide all codes by Using CSS

Follow these steps :

  • In the notebook click on file
  • Download as .html file
  • Open the .html file in a text editor
  • Now paste the below code in the CSS section

CSS




div.code_mirror {
    display: none;
}
 
div.input_area {
    display: none;
}


  • Reopen the .html file and view the changes

Hide codes using Built-in Feature

Follow these steps :

  • In the toolbar click on cell . Now Click on all outputs
  • Click on clear (this will clear all outputs)
  • Now click view
  • Click on cell toolbar
  • Click on ‘edit metadata’
  • A button will appear on the right side of all cells
  • Click on that button(named ‘edit metadata’)
  • Here you have to add some JSON text
{
     "tags" : [
              "hide-input"
     ]
}
  • Hit SHIFT+ENTER to save the metadata

Hide codes using jupyter Notebook extension

Warning : make a copy of important content as the newer version of Jupyter (jupyter 7) has some issues with running the extensions.

Follow these steps:

Installing the extension

Open terminal run the command

 pip install jupyter_contrib_nbextensions && jupyter contrib nbextension install 

Enabling the extension

  • Launch Jupyter Notebook
  • In the home page a new tab will appear named ‘nbextensions’
  • Click on nbextensions button
  • Scroll down and click on ‘Codefolding’ option and save

Hiding code cells

  • Now open the notebook and on the right of cell you will see a small triangle button
  • Click on it to hide the code.

Saving the notebook

  • In the toolbar click on file. Now click on save button to save the notebook (keyboard shortcut : ctrl + S ). This will save the notebook and create a checkpoint



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads