Hide Code when exporting Jupyter notebook to HTML

I'm looking for a way to hide code cells (inputs) when export my .iipynb file to a HTML. I don't want the code cells to be visible at all (not some button that turn them off/on). The output is for people that have no idea what a programming language is. I tried many things that I found on the internet but nothing seems to work.

Thanks

81783 次浏览

I finaly found that : https://pypi.org/project/hide_code/0.2.0/

It's a jupyter extension and it's working like a charm. I use the command prompt to convert the notebook into an html, since the buttons that comes with the extension don't work for me.

I used nbinteract (https://www.nbinteract.com/) to publish the page and #HIDDEN (https://gitter.im/nbinteract/Lobby/) on top of the cell. It is undocumented and bound to change, but they'll keep it for backwards compatibility..

You can do this with an NBConvert template. Most of the examples out there are for latex/PDF, and won't work with HTML, which uses a different set of templates (and, for some reason, a different extension and slightly different file syntax).

Write the following into a template file called hidecode.tpl:

{%- extends 'full.tpl' -%}


{% block input_group %}
{%- if cell.metadata.get('nbconvert', {}).get('show_code', False) -%}
((( super() )))
{%- endif -%}
{% endblock input_group %}

Then convert your notebook to HTML with:

jupyter nbconvert --to html --template hidecode YourNotebook.ipynb

In recent versions of jupyter nbconvert you can use the --no-input option:

echo 'A Markdown cell with an equation $x=y+1$


```python
1 + 1
```
' | jupytext --to ipynb | jupyter nbconvert --stdin --execute --no-input --to html --output notebook.html

Now if you don't have the --no-input option, use --TemplateExporter.exclude_input=True, which is available from version 5.2.1 on.

as of now (nbconvert version 5.6.0) the easiest solution seems to be to provide the argument --no-input when using the CLI interface of nbconvert:

jupyter nbconvert yourNotebook.ipynb --no-input

it works like magic more info here

For others that might want to hide a specific code cell, one solution is to use a command line tool in nbdev package (developed by fastai) to export jupyter notebooks to markdown. The command is nbdev_nb2md.

When you do this, if you put #hide at the top of any notebook cell, it won't be exported to markdown. It will be ignored.

See this blog post for full details: https://www.fast.ai/2020/01/20/nb2md/

Have your jupyter notbook ready Go to Anaconda Prompt -> location of the jupyter notebook and enter the below command

jupyter nbconvert yourNotebook.ipynb --no-input --no-prompt

This will convert the Jupyter notebook to html with all the cells aligned to right.

Complementing @vincentVega answer, you need to add the --to statement, otherwise it will throw the following error: ValueError: Please specify an output format with '--to <format>'.

jupyter nbconvert YourNotebook.ipynb --no-input --to html

For my use case I wanted to be able to create export without outputs from within a notebook, and with as little manual work as possible. An adequate solution I managed to glue together is as follows:

In first cell execute some javascript to get the name of the current notebook. The name will be stored in the nb_name variable which accessible within current python runtime scope.

Cell 1:

%%javascript
IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')

In cell 2 pass the obtained name to a shell call and store a html with --no-input. The "!" in a jupyter notebook indicates a shell call and "&" is used to pass a variable from current python runtime scope to the shell call.

Cell 2:

print(nb_name)
!jupyter nbconvert --output-dir='./docs' --no-input --to html $nb_name

Conversion of ipynb code file to a HTML file without code(Using Python)::

Step1: Suppose your file Untitled.ipynb is saved in your laptop's Downloads folder.

Step2: Open Anaconda prompt or Cmd , Paste the below command to hide the codes and save the file as Untitled.html:

  • cd Downloads
  • jupyter nbconvert Untitled.ipynb --to=html --TemplateExporter.exclude_input=True

Note: path to the Untitled.ipynb could be different and needs to be changed where we are using cd Downloads to cd new_path.

I wrote an article about different ways how to hide code in Jupyter Notebook. According to my findings, there are several ways in which this can be done.

1. Hide all code in nbconvert

When exporting notebook with nbconvert you need to add --no-input in the command:

jupyter nbconvert --to html --no-input your-notebook.ipynb

2. Hide selected cells

You can hide selected cells by adding a tag hide_code, like in the animation below:

The command that hide code only for selected cells:

jupyter nbconvert --to html --TagRemovePreprocessor.remove_cell_tags='{"hide_code"}' my-notebook.ipynb

3. Export to HTML with Mercury

The Mercury is an open-source for sharing notebooks with non-technical users. It can hide code. It can also generate widgets for the notebook that are connected with variables in the code. The notebook export process is controlled with YAML header, below is the example YAML that hides the code:

---
title: My notebook
description: My amazing notebook with hidden code
show-code: False
---

The example notebook with YAML header: example notebook with YAML header

The HTML (website) generated for the notebook with Mercury: