简体   繁体   中英

How to write Jinja2 template for Jupyter nbconvert to show markdown and suppress output number?

I am trying to write a Jinja2 template to convert my Jupyter notebook to a PDF via LaTex using nbconvert . My current attempt does not show Markdown cells or image captions and also shows the following output above all of my matplotlib graphs:

out[1]: <matplotlib.axes._subplots.AxesSubplot at 0x2e62e885cc0>

I would like to display markdown cells and captions and to suppress matplotlib object descriptions.

My current template is adapted from one hosted on the nbconvert github repo and is as follows:

% Default to the notebook output style
((* if not cell_style is defined *))
    ((* set cell_style = 'style_ipython.tplx' *))
((* endif *))

% Inherit from the specified cell style.
((* extends cell_style *))


%===============================================================================
% Latex Book
%===============================================================================

((* block docclass *))
\documentclass{report}
((* endblock docclass *))

% Author and Title from metadata
((* block maketitle *))
((*- if nb.metadata["latex_metadata"]: -*))
((*- if nb.metadata["latex_metadata"]["title"]: -*))
    \title{((( nb.metadata["latex_metadata"]["title"] )))}
((*- endif *))
((*- else -*))
    \title{((( resources.metadata.name )))}
((*- endif *))

\date{\today}
\maketitle
((* endblock maketitle *))

((* block markdowncell scoped *))
((( cell.source | citation2latex | strip_files_prefix | convert_pandoc('markdown+tex_math_double_backslash', 'json',extra_args=[]) | resolve_references | convert_pandoc('json','latex', extra_args=["--chapters"]) )))
((* endblock markdowncell *))


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

This hardly seems like much of an "answer," but have you tried using a semicolon at the end of the last line of your code cells? Jupyter's normal behavior is to display the last object returned by the code cell. If your last line was something to do with matplotlib , that line of code typically returns a matplotlib object, which you typically aren't very interested in. So, for example:

from matplotlib.pyplot import plot, xlabel, grid
from numpy import linspace, sin
x = linspace(-20, 20, 200)
plot(x, sin(x))
grid()
xlabel('x')

Produces the an output like you describe. The following does not produce the one-line print. The only difference is the semicolon at the end.

from matplotlib.pyplot import plot, xlabel, grid
from numpy import linspace, sin
x = linspace(-20, 20, 200)
plot(x, sin(x))
grid()
xlabel('x');

Another way to avoid the output is to rearrange your lines. The grid() function normally returns None , so if you put that line last, you also get no output.

Is this what you were asking about?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM