[IPython-dev] Removing "Out[]" prompts in nbconvert PDF output

Maximilian Albert maximilian.albert at gmail.com
Sun Mar 29 06:30:37 EDT 2015


Ok, I had time to look a bit more into this and the following does what I
want:

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

% Disable input cells
((* block input_group *))
((* endblock input_group *))

((* block execute_result scoped *))
    ((*- for type in output.data | filter_data_type -*))
        ((*- if type in ['text/plain']*))
\begin{Verbatim}[commandchars=\\\{\}]
((( output.data['text/plain'] | escape_latex )))
\end{Verbatim}
        ((* else -*))
            ((* block data_priority scoped *))
            ((( super() )))
            ((* endblock *))
        ((*- endif -*))
    ((*- endfor -*))
((* endblock execute_result *))
<==

The problem with the previous suggestion was that the call to super() would
call the block "execute_result" in style_ipython.tplx, which would re-add
the Out[] prompt that had just been stripped. What we really want is to
call the "grandparent" instead of the parent block. So copying the
execute_result block from templates/latex/base.tplx (which is what
style_ipython.tplx inherits from) works.

The other minor problem was that some of the output wasn't formatted
correctly. This is because the "add_prompt" filter in style_ipython.tplx
doesn't just add the prompt but also wraps the cell contents in
\begin{Verbatim} ... \end{Verbatim}. So once I added this to my custom
template it also works correctly.

Even though this achieves what I need it's obviously not ideal because it
may break if something in the IPython templates changes or gets rearranged.
If I find the time I may try to have a look at how they can be refactored
so that less copying of template snippets is needed.

Many thanks again to Matthias and Aaron for their help which got me on the
right track!

Cheers,
Max


2015-03-26 11:32 GMT+00:00 Maximilian Albert <maximilian.albert at gmail.com>:

> Hi Aaron,
>
> Thanks a lot for your suggestion! This is actually exactly what I tried
> before sending my email to the list, and it does indeed work in certain
> cases, but some of my notebooks didn't convert correctly with it. I've put
> up a minimal example here:
>
>    https://github.com/maxalbert/nbconvert-templating-issue
>
> If you type "make" it will use different templates to generate three pdf
> documents from the notebook "minimal_example.ipynb" (more information on
> what the templates do is in the README of the repo). You will see that for
> the cell containing the image with the musical note, the "Out[]" prompt is
> retained. The notebook "sample_notebook.ipynb" has a slightly longer
> example, comparing with some other types of output cells for which the
> "Out[]" prompt is indeed removed.
>
> I won't have time to dig into this before the weekend, and my jinja-fu is
> almost zero, so if anyone has a quick idea what's going on here I'd
> appreciate it.
>
> Many thanks!
> Max
>
> 2015-03-24 11:27 GMT+00:00 Aaron O'Leary <aaron.oleary at gmail.com>:
>
>> Sorry, I misread your question! You only want to remove the output
>> prompts, not the whole output.
>>
>> Try this:
>>
>> ((*- extends 'article.tplx'
>> -*))
>>
>>
>>
>> % Disable input
>> cells
>>
>> ((* block input_group
>> *))
>>
>> ((* endblock input_group
>> *))
>>
>>
>>
>> ((* block execute_result scoped
>> *))
>>
>>     ((*- for type in output.data | filter_data_type
>> -*))
>>
>>         ((*- if type in
>> ['text/plain']*))
>>
>>             ((( output.data['text/plain'] | escape_latex
>> )))
>>
>>         ((* else
>> -*))
>>
>>             ((( super()
>> )))
>>
>>         ((*- endif
>> -*))
>>
>>     ((*- endfor
>> -*))
>>
>> ((* endblock execute_result *))
>>
>>
>> i.e. get rid of both the call to add_prompt and the manual prompt
>> creation.
>>
>> On 24 March 2015 at 10:35, Aaron O'Leary <aaron.oleary at gmail.com> wrote:
>>
>>> Hi Max,
>>>
>>> This is what you need:
>>>
>>> ((*- extends 'article.tplx' -*))
>>>
>>> % Disable input cells
>>> ((* block input_group *))
>>> ((* endblock input_group *))
>>>
>>> % Disable output cells
>>> ((* block output_group *))
>>> ((* endblock output_group *))
>>>
>>>
>>> You need to use 'output_group' instead of 'execute_result'. There are
>>> quite a number of elements in the output and 'output_group' is the block
>>> that covers all of them.
>>>
>>> You can get an overview of the block hierarchy by looking at
>>> 'IPython/nbconvert/templates/latex/skeleton/null.tplx'.
>>>
>>> aaron
>>>
>>>
>>> On 24 March 2015 at 01:08, Maximilian Albert <
>>> maximilian.albert at gmail.com> wrote:
>>>
>>>> 2015-03-23 19:06 GMT+00:00 Matthias Bussonnier <
>>>> bussonniermatthias at gmail.com>:
>>>>
>>>>>
>>>>> I guess you need to inherit the following block :
>>>>>
>>>>>
>>>>> ((* block execute_result scoped *))
>>>>>     ((*- for type in output.data | filter_data_type -*))
>>>>>         ((*- if type in ['text/plain']*))
>>>>>             ((( add_prompt(output.data['text/plain'] | escape_latex,
>>>>> cell, 'Out', 'outcolor') )))
>>>>>         ((* else -*))
>>>>> \texttt{\color{outcolor}Out[{\color{outcolor}((( cell.execution_count
>>>>> )))}]:}((( super() )))
>>>>>         ((*- endif -*))
>>>>>     ((*- endfor -*))
>>>>> ((* endblock execute_result *))
>>>>>
>>>>>
>>>>> form IPython/nbconvert/templates/latex/style_ipython.tplx
>>>>>
>>>>> And replace by what you wish.
>>>>>
>>>>
>>>>
>>>> Thanks for the suggestion! That's precisely what I tried, but the
>>>> problem seems to be that the call to super() somehow adds an additional
>>>> Out[] prompt. I'll try to dig deeper into this when I get the time (and/or
>>>> make a minimal example available so that it can easily be reproduced), but
>>>> if anyone has any quick ideas I'm happy to hear them.
>>>>
>>>> Cheers,
>>>> Max
>>>>
>>>> P.S.: I noticed I forgot to include the link to the StackOverflow
>>>> question I mentioned in my original email. Here it is:
>>>>
>>>>
>>>> http://stackoverflow.com/questions/19524554/suppress-code-in-nbconvert-ipython
>>>>
>>>>
>>>> _______________________________________________
>>>> IPython-dev mailing list
>>>> IPython-dev at scipy.org
>>>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>>>
>>>>
>>>
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150329/94c2440f/attachment.html>


More information about the IPython-dev mailing list