Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standard way to remove all code cells from PDF export #155

Closed
bbirand opened this issue Nov 5, 2015 · 22 comments
Closed

Standard way to remove all code cells from PDF export #155

bbirand opened this issue Nov 5, 2015 · 22 comments
Milestone

Comments

@bbirand
Copy link

bbirand commented Nov 5, 2015

What is the standard way of removing all code cells from the exported PDF? I searched for it, and found many different "tplx" templates, for various versions. I tried some of them but they didn't seem to work. Is there a standard way of doing this?

Thanks!

@Carreau
Copy link
Member

Carreau commented Nov 5, 2015

No. We are working on ability for external libraries to provides templates that woudl do such things.

@juhasch
Copy link
Contributor

juhasch commented Nov 5, 2015

This custom template has what you want:
https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/templates/printviewlatex.tplx
Look at the "Input" part.

If your codecell contains the metadate cell.metadata.hide_input=True, it will be hidden in the generated PDF. There are also some extensions in this repository that allow setting this metadata (hide_input, hide_input_all, runtools).

@bbirand
Copy link
Author

bbirand commented Nov 7, 2015

@juhasch Thanks a lot for pointing me to that repo! Most of it is working great.

I added two PRs and an issue on this point:
ipython-contrib/jupyter_contrib_nbextensions#399

@bbirand bbirand closed this as completed Nov 7, 2015
@minrk minrk added this to the no action milestone May 31, 2016
@JamesOwers
Copy link

I found this googling around how to suppress output of individual cells when exporting to PDF (like you can in R markdown documents by using code chunk echo=False).

It took me a little while to understand the above so I'll spell out what I did more explicitly.

Versions I'm using:
$ jupyter notebook --version
4.1.0
$ jupyter nbconvert --version
4.2.0

  1. Download the ipython notebook extension templates by following install instructions on Github: pip install https://github.com/ipython-contrib/IPython-notebook-extensions/tarball/master
  2. run jupyter notebook
  3. go to localhost:8888/nbextensions (or whatever port you started on) and activate Printview
  4. go back to localhost:8888/tree, create a new notebook and go into it
  5. create a code cell with some code in it that produces output e.g. print("You can see me") #but not me
  6. go to View > Cell Toolbar > Edit Metadata
  7. click the Edit Metadata button now showing to the top right of the cell
  8. add 'hide_input':True to the json e.g. mine looked like { "collapsed": false, "hide_input": true, "trusted": true } after
  9. save notebook
  10. go back to the terminal and execute jupyter nbconvert --to pdf --template printviewlatex.tplx notebookname.ipynb (if your notebook is called notebookname.ipynb.ipynb)

You should now have a document called notebookname.pdf in the directory. Hopefully it should have just the text You can see me in it...fingers crossed.

@achiappo
Copy link

Hi @kungfujam , I installed the ipython_notebook_extensions tarball, I can locate it in /usr/local/lib/python2.7/dist-packages under the directory jupyter_contrib_nbextentions/templates, but when I try to execute the command jupyter nbconvert --to pdf --template printviewlatex.tplx I get the following error: jinja2.exceptions.TemplateNotFound: printviewlatex.tplx. Do you know how can I solve this? Thanks a lot in advance

@juhasch
Copy link
Contributor

juhasch commented Jan 29, 2017

You need to set the template_path configuration for nbconvert to point to the template:
http://nbconvert.readthedocs.io/en/latest/config_options.html

Example for jupyter_nbconvert_configuration.py:

c = get_config()
c.Exporter.template_path = ['.']

@Houd1ny
Copy link

Houd1ny commented Feb 1, 2017

Is there a way to do it for html?

@juhasch
Copy link
Contributor

juhasch commented Feb 1, 2017

Sure, use the nbextensions.tpl file. Documentation is here.

@Houd1ny
Copy link

Houd1ny commented Feb 2, 2017

How to convert using python api?
I have found

converter = NbConvertApp()
converter.export_format = 'html'
converter.initialize()
converter.notebooks = ["report.ipynb"]
converter.convert_notebooks()

But i don`t know how to set temaplte using this class

@takluyver
Copy link
Member

I wouldn't use NbConvertApp, that's really there for the command line interface. I would do something like this:

from nbconvert import HTMLExporter
exporter = HTMLExporter(template_file='full.tpl')
output, resources = exporter.from_filename('report.ipynb')
# output is a string; you can write it to a file, save it to disk, etc.

@Houd1ny
Copy link

Houd1ny commented Feb 2, 2017

That is what I wanted
thanks!!!

@TMorville
Copy link

TMorville commented Nov 8, 2017

Any news on this functionality from nbconvert? In essence being able to suppress code but not necessarily output when converting a notebook to e.g. .pdf.

Never mind, just found the hide_code extension.

@mpschr
Copy link

mpschr commented Aug 15, 2018

The hide_code extension should really be baked in the main juptyer notebook repository. I think this can be perceived as a basic functionality in many settings.

The person performing the analysis normally needs to create a report - with hide_code a report with Markdown cells, plot cells & table outputs is comfortably created and can be handed over to co-workers/collaborators/customers who do not care about the coding part. For those people the code is really disturbing, and handing over a document which is half code half report does seem half-baked and they could think that the analyst is too lazy to create a tidy report.

@mwouts
Copy link

mwouts commented Jun 21, 2019

It could be useful to point to the new --no-input option of jupyter nbconvert. The option works with both PDF and HTML output. For instance:

echo 'In this notebook we compute $1+1=\dots$

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

produces
image

Many more options (including TemplateExporter.exclude_input) are available, cf. the nbconvert documentation.

@mpschr
Copy link

mpschr commented Jun 23, 2019

Thank you so much for the pointer, @mwouts - I haven't stumbled across this option yet!

@frohro
Copy link

frohro commented Dec 8, 2019

Thanks @mwouts! That worked for me just wonderfully.

@kylechan2017
Copy link

@mwouts this is very useful !

@evanlynch
Copy link

@mwouts thanks for the help!

@maxhartshorn
Copy link

@mwouts Great feature! It seems like it only works when exporting to HTML. When exporting to PDF with --no-input the code cells are still included.

Is this a bug or expected behavior? Adding this feature to the PDF exporter would make it easy to generate professional looking reports with jupyter

@mwouts
Copy link

mwouts commented May 5, 2020

Thanks everyone!

@maxhartshorn , the above example still works, with no code cell in the PDF, at least here on Ubuntu 20.04 LTS with textlive-xetex and the jupytext-dev conda environment. What is the outcome of jupyter nbconvert --version for you? (mine is 5.6.1)

@maxhartshorn
Copy link

@mwouts I'm on 5.6.1 as well...on Windows 10. I'm having trouble getting the above sample to run in my Anaconda prompt. However when I take an existing .ipynb file and save out to a pdf with the --no-input flag I still get the code cells. My command is as follows:

`[FILENAME].ipynb --execute --no-input --to pdf --output [FILENAME].pdf

@mwouts
Copy link

mwouts commented May 6, 2020

Well, just to be sure I gave it a try on Windows 10. I installed MikTex, and then created a simple environment with

conda env create --file environment.yml

based on this environment.yml file:

name: mini-env
channels:
  - default
  - conda-forge
dependencies:
  - nbconvert
  - jupyter_client
  - ipykernel
  - jupytext

Then, after running conda activate mini-env, I was able to run the nbconvert --no-input example, and I did got the same PDF than on Linux, without the input cells. So it is possible to get it working on Windows as well... @maxhartshorn , would you like to try the command in the mini-env environment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests