Hide Code when exporting Jupyter notebook to HTML
PythonJupyterPython Problem Overview
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
Python Solutions
Solution 1 - Python
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
Solution 2 - Python
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
Solution 3 - Python
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.
Solution 4 - Python
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.
Solution 5 - Python
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
Solution 6 - Python
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.
Solution 7 - Python
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
Solution 8 - Python
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.
Solution 9 - Python
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..
Solution 10 - Python
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/