简体   繁体   English

我需要什么降价模板,以便在使用nbconvert导出时,Jupyter笔记本中的输出单元格看起来与输入单元格不同

[英]What markdown template do I need such that output cells in Jupyter notebooks look different from input cells when exporting using nbconvert

I am trying to use mkdocs with mknotebooks to build a website out of Jupyter Notebook and markdown files. 我正在尝试将mkdocsmknotebooks结合使用,以从Jupyter Notebook和markdown文件构建网站。 All works well, except, the visual appearance of input and output cells in the resulting html pages is identical, making it hard to understand. 一切运行良好,除了在生成的html页面中输入和输出单元格的视觉外观相同之外,这使其难以理解。

For instance, in a notebook, input and output cells differently as shown below: 例如,在笔记本电脑中,输入和输出单元格如下所示: 正确的外观

However, when I export to markdown, then to html, they appear similar: 但是,当我导出到markdown,然后导出到html时,它们看起来类似: 当前外观

I tried handling this with CSS. 我尝试使用CSS处理此问题。 However, the div s of input and output cells are not of different classes, making it hard to define a different style. 但是,输入和输出单元格的div不是不同的类,因此很难定义不同的样式。

I am currently playing with nbconvert markdown templates . 我目前正在玩nbconvert markdown模板 However, I cannot figure out, what to modify so output cells appear differently. 但是,我不知道要修改什么,因此输出单元格看起来会有所不同。 By default, they are indented by 1 tab space, which it appears is not sufficient to distinguish them when exported to HTML. 默认情况下,它们缩进1个制表符空间,当导出到HTML时似乎不足以区分它们。

My custom template file looks like below: 我的自定义模板文件如下所示:

{% extends 'markdown.tpl' %}

<!-- adds call number to input prompts -->
{% block in_prompt %}
**In [{{ cell.execution_count }}]:**
{% endblock in_prompt %}

<!-- need help - make outputs appear different, perhaps different background cell color? -->
{% block output %}
    {{cell.source}}
{% endblock output %}


{% block markdowncell scoped %} 
{{ cell.source | wrap_text(80) }} 
{% endblock markdowncell %} 
...

You have a few options, generate a fenced code block or raw HTML. 您有几种选择,可以生成受保护的代码块或原始HTML。

Fenced code block 围栏代码块

A fenced code block natively includes a way to assign a class to the code block. 受保护的代码块本身包含一种将类分配给代码块的方法。 Normally the class is expected to be the language of the code contained within the block, but it does not have to be. 通常,该类应为该块中包含的代码的语言,但不一定必须如此。 Therefore, this should do the trick: 因此,这应该可以解决问题:

{% block output %}
``` nb-output
{{cell.source}}
```
{% endblock output %}

Note that we have set the class to nb-output , which will be set on the <code> tag of the HTML ( <pre><code class="nb-output"> ). 请注意,我们已将类设置为nb-output ,它将在HTML的<code>标记上设置( <pre><code class="nb-output"> )。

Now you can define a CSS style for the nb-output class. 现在,您可以为nb-output类定义CSS样式。 MkDocs already enables the fenced_code Markdown extension by default. 默认情况下,MkDocs已经启用了fenced_code Markdown扩展。

Raw HTML 原始HTML

As the Markdown rules explain : 正如Markdown规则所解释的

HTML is a publishing format; HTML是一种发布格式; Markdown is a writing format. Markdown是一种书写格式。 Thus, Markdown's formatting syntax only addresses issues that can be conveyed in plain text. 因此,Markdown的格式语法仅解决可以以纯文本形式传达的问题。

That being the case, there is no mechanism in Markdown to provide styling information (or styling hooks). 在这种情况下,Markdown中没有提供样式信息(或样式挂钩)的机制。 However, as the rules continue: 但是,随着规则的继续:

For any markup that is not covered by Markdown's syntax, you simply use HTML itself. 对于Markdown语法未涵盖的任何标记,您只需使用HTML本身。 There's no need to preface it or delimit it to indicate that you're switching from Markdown to HTML; 无需在其前添加或定界以表明您已从Markdown切换为HTML; you just use the tags. 您只需使用标签。

Therefore, in your template, include some raw HTML. 因此,在模板中包括一些原始HTML。 Perhaps something like this: 也许是这样的:

{% block output %}
<div class="nb-output">
    {{cell.source}}
</div>
{% endblock output %}

Now you can define a CSS style for the nb-output class. 现在,您可以为nb-output类定义CSS样式。

Note that as you do not provide the output (the screen shots are not particularly helpful), I can't be certain that a <div> is the best tag to use. 请注意,由于您没有提供输出(屏幕截图不是特别有用),因此我不确定<div>是最佳使用标记。 If you provided the generated HTML for the two examples, then it might be more clear what HTML to use. 如果提供了两个示例的生成的HTML,则可能会更清楚地使用哪种HTML。

For example, Markdown processing is not done inside raw HTML blocks. 例如,Markdown处理未在原始HTML块内完成。 Therefore, the cell.source would not get converted to a proper code block. 因此, cell.source不会转换为适当的代码块。 Perhaps a better approach would be to manually create the code block yourself: 也许更好的方法是自己手动创建代码块:

{% block output %}
<pre><code class="nb-output">
{{ cell.source|e }}
</code></div>
{% endblock output %}

Note that we manually create a code block, (wrapping the cell.source in <pre><code> tags) while assigning the nb-output class to the code block. 请注意,我们在将nb-output类分配给代码块时,手动创建了一个代码块(将cell.source包裹在<pre><code>标记中)。 We also escape the cell.source with the e filter to ensure that it displays properly in the code block. 我们还使用e过滤器对cell.source进行转义,以确保其在代码块中正确显示。 These are all things that Markdown normally does when creating a code block, minus the class. 这些都是Markdown在创建代码块时通常要做的事情,减去类。

暂无
暂无

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

相关问题 使用 `--to script` 忽略 `jupyter nbconvert` 中的 Markdown 单元格 - ignore markdown cells in `jupyter nbconvert` with `--to script` 从 VSCode 导出 Juypter Notebook 时,如何只显示某些单元格输入或 output? - How do I only show certain cells input or output when exporting a Juypter Notebook from VSCode? 仅显示从Jupyter Notebook用nbconvert创建的pdf中的特定(带标签)输入单元格 - Show only specific (tagged) input cells in pdf created with nbconvert from jupyter notebook nbconvert to markdown:避免代码单元格中的缩进 - nbconvert to markdown: avoid indents in code cells 防止 Jupyter Notebook 合并 markdown 个单元格 - Prevent Jupyter Notebook from merging markdown cells Jupyter Notebooks:如何只执行具有特定标签的单元格 - Jupyter Notebooks: How do I execute only cells that have a particular tag 调试 Python 脚本时是否可以复制 Jupyter Notebooks 单元的功能? - Is it possible to replicate the functionality of Jupyter Notebooks cells when debugging a Python script? 我需要互联网才能使用 jupyter notebooks 吗? - Do I need internet to use jupyter notebooks? 如何为Jupyter nbconvert编写Jinja2模板以显示降价并抑制输出数量? - How to write Jinja2 template for Jupyter nbconvert to show markdown and suppress output number? 如何在 Jupyter 笔记本中以编程方式生成 markdown output? - How to programmatically generate markdown output in Jupyter notebooks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM