73

I have a large notebook with a lot of figures and text. I want to convert it to a html file. However, I don't want to export the code. I am using the following command

ipython nbconvert --to html notebook.ipynb

But this option also exports the code. Is there a way to convert the notebook to html without the code?

Thomas K
  • 39,200
  • 7
  • 84
  • 86
aloha
  • 4,554
  • 6
  • 32
  • 40

6 Answers6

83

I found this article interesting it explains how to remove the input columns :

you have to create a template file named "hidecode.tplx" in same directory as the notebook you want to convert and add those line in it :

    ((*- extends 'article.tplx' -*))

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

And after run this command it will use pdfLatex to convert the notebook in pdf via latex:

jupyter nbconvert --to pdf --template hidecode Example.ipynb

or if you want to edit you can convert it to a .tex document and use pdfLatex to put it in pdf :

jupyter nbconvert --to latex --template hidecode Example.ipynb

EDIT Sept 2018:

ipython nbconvert is deprecated. It will be replaced by jupyter nbconvert: So we replace the command ipython with jupyter

EDIT Feb 2021: (This is my best answer here, so let me take care of it)

Following @Mrule comment adding --no-input flag will make it work without the template...

jupyter nbconvert --to latex --no-input Example.ipynb 

PS: If you are getting issue saying :

LaTeX error related to tcolorbox.sty not found

Please refer to this guide to update your tex installation and this question

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
44

I was seeking the same question in SO and finally turned out to a very straightforward way:

Assuming using Firefox(57) + Win7

  1. Run Jupyter notebook and download the notebook in the browser: File->Download as->HTML and you will get a html page with code and output.
  2. Open the exported HTML with browser and activate the browser console with key F12
  3. Run following command in the console:

    document.querySelectorAll("div.input").forEach(function(a){a.remove()})
    
  4. The code removes all input div DOM. Then right mouse button and chose "Save Page As" and Save the "Complete page" (not single page).

  5. You will get a page with an associated folder in windows. Use a trick by zip the html page and then extract to unbind the associated. The folder is useless.

  6. Now it is a single html page without code. You can re-distribute it or print it as PDF.

If you are not using Firefox or Windows, please adjust the above 3-6 steps.

Tim Skov Jacobsen
  • 3,583
  • 4
  • 26
  • 23
Boying
  • 1,404
  • 13
  • 20
  • 3
    Works for Chrome too! – datalifenyc Sep 11 '18 at 16:16
  • 2
    Might seem crazy, but this is one-line simple and it works perfectly. Thanks for digging this up! – Dan Nissenbaum Apr 23 '19 at 03:14
  • I copied the command in the console as u said, but it yields `undefined` and nothing was removed – ahmed abdelmalek Jan 27 '21 at 16:29
  • 1
    I think now is `document.querySelectorAll("div.jp-InputArea.jp-Cell-inputArea").forEach(function(a){a.remove()})` but this for example will keep the errors and markdown with maybe you don't want. To remove all input cells, not only code cells you could use `document.querySelectorAll("jp-Cell jp-CodeCell jp-Notebook-cell").forEach(function(a){a.remove()})` but is a method to check which columns you want and remove them checking the name of the div. – set92 Feb 11 '21 at 12:02
  • This works now `document.querySelectorAll("div.jp-Cell.jp-CodeCell.jp-Notebook-cell>.jp-Cell-inputWrapper").forEach(function(a){a.remove()})` – EnesZ May 16 '22 at 01:23
10

Here is how I do it: I simply download my notebook as html.

Then run this python script to convert that html file so that prompts and code cells are gone:

FILE = "/somewhere/myHTMLFile.html"

with open(FILE, 'r') as html_file:
    content = html_file.read()

# Get rid off prompts and source code
content = content.replace("div.input_area {","div.input_area {\n\tdisplay: none;")    
content = content.replace(".prompt {",".prompt {\n\tdisplay: none;")

f = open(FILE, 'w')
f.write(content)
f.close()

That script bascially adds the CSS 'display: none' attribute for all divs of class 'prompt' or 'input_area'.

amagard
  • 339
  • 3
  • 8
  • 1
    Just want to thank you for this code. I have used many 'janky' ways of removing the input areas and prompts, and none are as simple and clean as this. Thanks! – Nicholas Apr 10 '20 at 12:43
  • Thank you so much! This is definitely the easiest way, and works like a charm! – Cloudy_Green Oct 09 '20 at 14:07
8

As a generalization of this answer, so that hidecode template could be accessible from multiple locations:

  1. Go into your home directory:

    cd ~/.jupyter
    
  2. Create jupyter_nbconvert_config.py under this directory.

  3. Write the following into the .py file (Change user_name to your user name):

    c = get_config()
    c.TemplateExporter.template_path = ['.', "~/.jupyter" ]
    c.LatexExporter.template_path = ['.', "~/.jupyter"]
    
  4. Create a template file under this directory, named hidecode.tplx or hidecode.tpl:

        ((*- extends 'article.tplx' -*))
    
    ((* block input_group *))
        ((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*))
                ((( super() )))
        ((*- endif -*))
    ((* endblock input_group *))
    
  5. Then, the following should generate a PDF without .ipynb files' codes (Change file_name to your file name):

    jupyter nbconvert --to pdf '<file_name>.ipynb' --template=hidecode.tpl
    
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
Herpes Free Engineer
  • 2,425
  • 2
  • 27
  • 34
7

You can add this css to your page and then print into PDF from the browser. Note that the code is only hidden, not removed.

div.output_prompt { visibility: hidden; }
*{ font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif !important }
.input,#header { display: none; }

To make sure you remove the code you should use something like

$(".input").remove()

As far as I know there is no way to generate via latex a PDF that does not include the code. It would be great to have a jupyter notebook extension that does it though...

teddy browns
  • 141
  • 1
  • 6
3

nbconvert uses templates to convert the notebook's JSON into a document; for example, in the markdown template that comes with nbconvert, markdown.tpl:

{% block input %}
{% if nb.metadata.language_info %}{{ nb.metadata.language_info.name }}{% endif %}
{{ cell.source}}
{% endblock input %}

which prints each cell's source in the output. If you delete {{ cell.source}} then the cell source will not print out. There are corresponding blocks in the HTML and Latex templates as well.

Better yet, make your own template that does exactly what you want :)

Neal Fultz
  • 9,282
  • 1
  • 39
  • 60
  • Where is that template located at? Or if I want to use another one, should I specify the new template as a parameter to the jupyter nbconvert command? – anilbey Mar 28 '18 at 16:12
  • 1
    https://github.com/jupyter/nbconvert/blob/master/nbconvert/templates/markdown.tpl for the stock template - it should be installed with nbconvert somewhere on your python path. I usually specify templates in a config file, but there's a CLI argument also IIRC. – Neal Fultz Mar 28 '18 at 16:17