Pan*_*Yan 33 ipython ipython-notebook
有没有办法在IPython笔记本中有选择地隐藏一个特定的输入或输出单元?
我只能找到以下代码来显示/隐藏所有输入单元格.
http://blog.nextgenetics.net/?e=102
但是,如果我只想隐藏笔记本的第一个输入单元怎么办?
Tom*_*ger 38
现在使用标签将其内置到nbconvert(从5.3.0开始).
这是从输出中删除特定单元格的示例.用这个笔记本.该示例有三个单元格:一个markdown单元格,一个将被隐藏的代码单元格,以及一个不会被隐藏的代码单元格.
remove_cell
使用笔记本或JupyterLab内置的标签编辑器将标签添加到您要隐藏的任何单元格中(具体名称"remove_cell"无关紧要)用nbconvert转换
jupyter nbconvert nbconvert-example.ipynb --TagRemovePreprocessor.remove_cell_tags='{"remove_cell"}'
带有标记的任何单元格remove_cell
都将从输出中删除.
除了整个单元格,您只需过滤输入或仅输出:
TagRemovePreprocessor.remove_input_tags
TagRemovePreprocessor.remove_single_output_tags
TagRemovePreprocessor.remove_all_outputs_tags
Fer*_*ard 16
这是Mathmagician答案的扩展,它使您能够:
您需要做的是首先运行以下代码来定义hide_toggle
函数:
from IPython.display import HTML
import random
def hide_toggle(for_next=False):
this_cell = """$('div.cell.code_cell.rendered.selected')"""
next_cell = this_cell + '.next()'
toggle_text = 'Toggle show/hide' # text shown on toggle link
target_cell = this_cell # target cell to control with toggle
js_hide_current = '' # bit of JS to permanently hide code in current cell (only when toggling next cell)
if for_next:
target_cell = next_cell
toggle_text += ' next cell'
js_hide_current = this_cell + '.find("div.input").hide();'
js_f_name = 'code_toggle_{}'.format(str(random.randint(1,2**64)))
html = """
<script>
function {f_name}() {{
{cell_selector}.find('div.input').toggle();
}}
{js_hide_current}
</script>
<a href="javascript:{f_name}()">{toggle_text}</a>
""".format(
f_name=js_f_name,
cell_selector=target_cell,
js_hide_current=js_hide_current,
toggle_text=toggle_text
)
return HTML(html)
Run Code Online (Sandbox Code Playgroud)
然后在像这样的单元格中使用它:
x = 1
y = 2
print('Result is {} + {}'.format(x, y))
hide_toggle()
Run Code Online (Sandbox Code Playgroud)
或者这个(如果你想切换下一个单元格)
hide_toggle(for_next=True)
Run Code Online (Sandbox Code Playgroud)
Jam*_*ers 12
这是一种方法,允许您通过仅编辑单元格元数据来隐藏HTML/PDF输出中的单元格.
我正在使用的版本:
$ jupyter notebook --version
4.1.0
$ jupyter nbconvert --version
4.2.0
jupyter notebook
localhost:8888/nbextensions
(或你开始的任何端口)并激活Printview
localhost:8888/tree
,创建一个新的笔记本,然后进入它print("You can see me") #but not me
View
> Cell Toolbar
>Edit Metadata
Edit Metadata
现在显示在单元格右上角的按钮'hide_input':True
到JSON如矿山看起来像{
"collapsed": false,
"hide_input": true,
"trusted": true
}
后jupyter nbconvert --to pdf --template printviewlatex.tplx notebookname.ipynb
(如果你的笔记本被调用notebookname.ipynb.ipynb
)您现在应该在目录中有一个名为notebookname.pdf的文档.希望它应该只有文本You can see me
...手指交叉.
小智 9
好的,在尝试没有成功之后,这里给出的答案。我找到了 kirbs 的这个扩展。Hide_code nbextension它工作得很好。但建议执行以下操作:
首先,确保您已更新 jupyter、nbconverter、nbconverter 扩展和 jupyter serverextension。如果您这样做了,那么您可以在 anaconda 提示符下执行以下操作(以管理员权限打开):
pip install hide_code
jupyter nbextension install --py hide_code
jupyter nbextension enable --py hide_code
jupyter serverextension enable --py hide_code
最后,如果您使用 anaconda 发行版打开笔记本,请确保也使用以下命令:
jupyter nbextension install --sys-prefix --py hide_code
jupyter nbextension enable --sys-prefix --py hide_code
jupyter serverextension enable --sys-prefix --py hide_code
如果这些命令的执行没有错误,那么您将能够看到并使用工具栏中的隐藏代码选项,如下所示:
完毕!如果您使用导出按钮,瞧!
祝你好运
您可以更改隐藏所有输入单元格的解决方案,以便仅影响单个单元格.
更改'div.input'
到'div.cell.code_cell.rendered.selected div.input'
.
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.cell.code_cell.rendered.selected div.input').hide();
} else {
$('div.cell.code_cell.rendered.selected div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
To show/hide this cell's raw code input, click <a href="javascript:code_toggle()">here</a>.''')
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为当您单击单元格输出上的" 单击此处 "提示时,该单元格将成为"选定"单元格,从而变为隐藏状态.
如果您的JavaScript代码<script></script>
使用这样的代码行在标记内执行切换
$( document ).ready(code_toggle);
Run Code Online (Sandbox Code Playgroud)
然后,当执行输入单元格时,块将自动隐藏("默认情况下").
请记住,如果默认情况下隐藏单元格输入,则必须使用" 运行单元格"(Ctrl+ Return)选项运行单元格,而不是 " 运行单元格"和"选择/插入下方"选项.这些将在执行JavaScript之前提示将"选定"标签移动到下一个单元格,因此您可能最终隐藏在其输出中没有" 单击此处 "切换链接的单元格.在这种情况下,您将必须检查单元格并浏览相关标签并更改display='none';
为display='block';
.
请注意,这必须放在单元格中任何代码的末尾,并且在执行此代码之前需要从IPython.display导入HTML.你可以通过执行来实现
from IPython.display import HTML
Run Code Online (Sandbox Code Playgroud)
如果有人发现排除所有代码单元格有帮助(这不是这里要求的内容),您可以添加此标志 nbconvert --TemplateExporter.exclude_code_cell=True
归档时间: |
|
查看次数: |
37375 次 |
最近记录: |