在jupyter笔记本中隐藏代码 [英] hide code in jupyter notebook
问题描述
我有一个jupyter笔记本,它是markdown和代码的混合体.最后,我想将其渲染为pdf报告并隐藏代码.我仍然希望看到代码,图表和表格的输出,只是不想在最终报告中看到代码.我发现下面的帖子具有下面的代码,如果将其添加到笔记本中,则会创建一个切换按钮,该按钮可用于隐藏或显示输入代码.这样做的问题是,我在报告的顶部显示了一个切换按钮.有谁知道该怎么做?
I have a jupyter notebook that is a mixture of markdown and code. In the end I want to render it out as a pdf report and hide the code. I still want to see the output of the code, the plots and tables, I just don't want to see the code in the final report. I found the post below that has the code below, which if added to the notebook creates a toggle button that can be used to hide or display the input code. The problem with that is I wind up with a toggle button at the top of my report. Does anyone know how to do this?
帖子:
<script>
function code_toggle() {
if (code_shown){
$('div.input').hide('500');
$('#toggleButton').val('Show Code')
} else {
$('div.input').show('500');
$('#toggleButton').val('Hide Code')
}
code_shown = !code_shown
}
$( document ).ready(function(){
code_shown=false;
$('div.input').hide()
});
</script>
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form>
推荐答案
如果您不愿意编写自己的模板,并且不担心输出,则可以使用 jupyter contrib nbextensions 软件包.这提供了用于隐藏所有代码单元的输入(尽管不是输出)的按钮,以及设置元数据项,允许您使用包提供的模板将隐藏的输入导出为html,latex或pdf:
If you don't fancy writing your own template, and you're not bothered about outputs, you could use the hide_input_all nbextension, which is provided as part of the jupyter contrib nbextensions package. This provides buttons to hide the inputs (though not outputs) of all code cells, as well as setting metadata items that allow you to export with hidden inputs to html, latex or pdf using templates provided by the package:
jupyter nbconvert --template=nbextensions --to=html my_notebook.ipynb
这篇关于在jupyter笔记本中隐藏代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!