简体   繁体   English

如何为Jupyter nbconvert编写Jinja2模板以显示降价并抑制输出数量?

[英]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 . 我正在尝试编写一个Jinja2模板,以使用nbconvert通过LaTexJupyter notebook转换为PDF。 My current attempt does not show Markdown cells or image captions and also shows the following output above all of my matplotlib graphs: 我目前的尝试未显示Markdown单元格或图像标题,并且在所有matplotlib图形上方都显示了以下输出:

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

I would like to display markdown cells and captions and to suppress matplotlib object descriptions. 我想显示markdown单元格和标题,并禁止显示matplotlib对象描述。

My current template is adapted from one hosted on the nbconvert github repo and is as follows: 我当前的模板改编自nbconvert github存储库上托管的模板,如下所示:

% 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. Jupyter的正常行为是显示代码单元返回的最后一个对象。 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: 如果您的最后一行与matplotlib ,则该行代码通常返回一个matplotlib对象,您通常对此并不十分感兴趣。因此,例如:

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. grid()函数通常返回None ,因此,如果将该行放在最后,也不会获得任何输出。

Is this what you were asking about? 这是您要问的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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