Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - Suppress code in NBConvert? IPython

I have figured out how to suppress large code blocks from showing up in final NB convert (PDF) output.

By putting the LaTex command in a "raw cell before the code I don't want to have in the final output

iffalse

Followed By this at the end In a raw cell

fi

But That still leaves me with some ugly code when I need to show figures and the like and while the base purpose of the notebook is to show code with results, sometimes for a non tech audience we only need the output.. Any Ideas?

Somewhat related if anyone is inspired.. any way to include python variables in the markdown cells so one could have dynamic text with calculated result? Sorry for a second issue but I'm not sure I want to ask this one separately for some strange reason.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To suppress the code cells (only input) a custom template can be used. Similar as discussed in this question, a template e.g. latex_nocode.tplx has to be created (in the working directory) with the following content (for IPython 1.x)

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

use this template like
ipython nbconvert --to=latex --template=latex_nocode.tplx --post=pdf file.ipynb

Maybe I should add that this way the input block is simply replaced by a blank block (actually a latex comment that input cells are disabled).
When checking the predefined latex templates, the individual blocks (code, markdown, heading, etc) can be identified and a respective custom templates can be set-up to style the output as desired.

Edit

as user1248490 pointed out since IPython 2.0 the latex templates to be extended are called article.tplx, report.tplx or base.tplx. Hence the example from above should look like

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...