Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Code when exporting Jupyter notebook to HTML

Tags:

python

jupyter

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

like image 279
Ahmed Lahlou Mimi Avatar asked Apr 18 '18 19:04

Ahmed Lahlou Mimi


People also ask

Is there a way to hide code in Jupyter notebook?

There are two ways to hide content: To hide Markdown, use the {toggle} directive. To hide or remove code cells or their outputs, use notebook cell tags.

How do I export a Jupyter notebook output to HTML?

Jupyter Notebook's Built-In Capability: If including code, this is the easiest way to create the JNaaP. This method is as simple as clicking File, Download as, HTML (. html). Jupyter will then download the notebook as an HTML file to wherever the browser defaults for downloaded files.

How do you hide the output in Jupyter notebook Vscode?

It seems to be that the shortcuts from jupyter notebook are integrated into the jupyter notebook in visual studio code. If you press "o" on a cell, it will hide the cell output.

What does %% capture do Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.


2 Answers

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

like image 161
vincentVega Avatar answered Sep 18 '22 16:09

vincentVega


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

like image 24
Autumn Avatar answered Sep 17 '22 16:09

Autumn