Search code examples
pythonipythonjupyter-notebookjupyternbconvert

Show only specific (tagged) input cells in pdf created with nbconvert from jupyter notebook


In various jupyter notebooks I want to manually specify which input cells to show and per default not show any input cell when converting it to a pdf or tex document.

I already achieved not showing any input cells by using following latex template:

((*- extends 'article.tplx' -*))
% Disable input cells
((* block input_group *))
((* endblock input_group *))

But now, when trying to add cell tags and then showing them or not I'm confusing. I tried

% Disable input cells if there is no "show" tag, else show
((*- block input_group -*))
    ((*- if 'show' in cell.metadata.get('tags', []) -*))
        ((*- block in_prompt -*))((*- endblock in_prompt -*))
        ((*- block input -*))((*- endblock input -*))
    ((*- else -*))
    ((*- endif -*))
((*- endblock input_group -*))

but it's always removing all input cells, the one with "show" tag too.


Solution

  • While writing this question, I realized that i copied the lines 4 and 5 in the second code block from the wrong file. It's from the null.tplx (skeleton) template, I should've used the style_ipython.tplx from where it is most of the time inherited.

    I came up with the following code, which now really removes/hides/supresses all input cells except the ones with the "show" tag:

    % Disable input cells if there is no "show" tag, else show
    ((*- block input_group -*))
        ((*- if 'show' in cell.metadata.get('tags', []) -*))
            ((( add_prompt(cell.source | highlight_code(strip_verbatim=True), cell, 'In ', 'incolor') )))
        ((*- else -*))
        ((*- endif -*))
    ((*- endblock input_group -*))
    

    But then it will only work with the ipython style, not with the python one... maybe someone can help here?