Python 将 Jupyter 笔记本导出为 HTML 时隐藏代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49907455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide Code when exporting Jupyter notebook to HTML
提问by Ahmed Lahlou Mimi
I'm looking for a way to hide code cells (inputs) when export my .iipynb file to a HTML. I don't want the code cells to be visible at all (not some button that turn them off/on). The output is for people that have no idea what a programming language is. I tried many things that I found on the internet but nothing seems to work.
我正在寻找一种在将我的 .iipynb 文件导出为 HTML 时隐藏代码单元格(输入)的方法。我不希望代码单元格是可见的(不是一些关闭/打开它们的按钮)。输出适用于不知道什么是编程语言的人。我尝试了很多我在互联网上找到的东西,但似乎没有任何效果。
Thanks
谢谢
采纳答案by Ahmed Lahlou Mimi
I finaly found that : https://pypi.org/project/hide_code/0.2.0/
我终于发现:https: //pypi.org/project/hide_code/0.2.0/
It's a jupyter extension and it's working like a charm. I use the command prompt to convert the notebook into an html, since the buttons that comes with the extension don't work for me.
这是一个 jupyter 扩展程序,它的工作原理就像一个魅力。我使用命令提示符将笔记本转换为 html,因为扩展附带的按钮对我不起作用。
回答by vincentVega
as of now (nbconvert version 5.6.0) the easiest solution seems to be to provide the argument --no-input
when using the CLI interface of nbconvert:
截至目前(nbconvert 5.6.0 版),最简单的解决方案似乎是--no-input
在使用 nbconvert 的 CLI 接口时提供参数:
jupyter nbconvert yourNotebook.ipynb --no-input
jupyter nbconvert yourNotebook.ipynb --no-input
it works like magic more info here
它的工作原理就像魔法更多信息在这里
回答by Autumn
You can do this with an NBConvert template. Most of the examples out there are for latex/PDF, and won't work with HTML, which uses a different set of templates (and, for some reason, a different extension and slightly different file syntax).
您可以使用 NBConvert 模板执行此操作。那里的大多数示例都适用于 Latex/PDF,并且不适用于 HTML,它使用一组不同的模板(并且,出于某种原因,不同的扩展名和略有不同的文件语法)。
Write the following into a template file called hidecode.tpl
:
将以下内容写入名为 的模板文件hidecode.tpl
:
{%- extends 'full.tpl' -%}
{% block input_group %}
{%- if cell.metadata.get('nbconvert', {}).get('show_code', False) -%}
((( super() )))
{%- endif -%}
{% endblock input_group %}
Then convert your notebook to HTML with:
然后使用以下命令将您的笔记本转换为 HTML:
jupyter nbconvert --to html --template hidecode YourNotebook.ipynb
jupyter nbconvert --to html --template hidecode YourNotebook.ipynb
回答by Marc Wouts
In recent versions of jupyter nbconvert
you can use the --no-input
option:
在最近的版本中,jupyter nbconvert
您可以使用该--no-input
选项:
echo 'A Markdown cell with an equation $x=y+1$
```python
1 + 1
```
' | jupytext --to ipynb | jupyter nbconvert --stdin --execute --no-input --to html --output notebook.html
Now if you don't have the --no-input
option, use --TemplateExporter.exclude_input=True
, which is available from version 5.2.1 on.
现在,如果您没有该--no-input
选项,请使用--TemplateExporter.exclude_input=True
,它从 5.2.1 版开始可用。
回答by fvanden
I used nbinteract (https://www.nbinteract.com/) to publish the page and #HIDDEN (https://gitter.im/nbinteract/Lobby/) on top of the cell. It is undocumented and bound to change, but they'll keep it for backwards compatibility..
我用nbinteract(https://www.nbinteract.com/)发布的页面和#HIDDEN(https://gitter.im/nbinteract/Lobby/在电池的顶部)。它没有记录并且肯定会改变,但为了向后兼容,他们会保留它。
回答by Tom Roth
For others that might want to hide a specific code cell, one solution is to use a command line tool in nbdev
package (developed by fastai) to export jupyter notebooks to markdown. The command is nbdev_nb2md
.
对于可能想要隐藏特定代码单元的其他人,一种解决方案是使用nbdev
包中的命令行工具(由 fastai 开发)将 jupyter notebooks 导出到 Markdown。命令是nbdev_nb2md
.
When you do this, if you put #hide
at the top of any notebook cell, it won't be exported to markdown. It will be ignored.
当你这样做时,如果你放在#hide
任何笔记本单元格的顶部,它不会被导出到降价。它将被忽略。
See this blog post for full details: https://www.fast.ai/2020/01/20/nb2md/
有关完整详细信息,请参阅此博客文章:https: //www.fast.ai/2020/01/20/nb2md/