export notebook to pdf without code
As a generalization of this answer, so that hidecode template could be accessible from multiple locations:
Go into your home directory:
cd ~/.jupyter
Create
jupyter_nbconvert_config.py
under this directory.Write the following into the
.py
file (Changeuser_name
to your user name):c = get_config() c.TemplateExporter.template_path = ['.', "~/.jupyter" ] c.LatexExporter.template_path = ['.', "~/.jupyter"]
Create a template file under this directory, named
hidecode.tplx
orhidecode.tpl
:((*- extends 'article.tplx' -*)) ((* block input_group *)) ((*- if cell.metadata.get('nbconvert', {}).get('show_code', False) -*)) ((( super() ))) ((*- endif -*)) ((* endblock input_group *))
Then, the following should generate a PDF without
.ipynb
files' codes (Changefile_name
to your file name):jupyter nbconvert --to pdf '<file_name>.ipynb' --template=hidecode.tpl
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'.
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
I was seeking the same question in SO and finally turned out to a very straightforward way:
Assuming using Firefox(57) + Win7
- 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.
- Open the exported HTML with browser and activate the browser console with key
F12
Run following command in the console:
document.querySelectorAll("div.input").forEach(function(a){a.remove()})
The code removes all input div DOM. Then
right mouse button
and chose "Save Page As" and Save the "Complete page" (not single page).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.
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.