remove nbconvert --to html 'in' and 'out' prompts based on cell metadata

ぐ巨炮叔叔 提交于 2019-12-09 13:42:15

问题


I'd like to remove the red and blue 'In' and 'Out' prompts when running nbconvert --to html, based on cell metadata. With cell metadata such as:

{'cell_tags': {'cutcode_html': true}}

The following sucessfully removes the 'In' prompt:

{% block input_group %}
 {% if cell['metadata'].get('cell_tags',{}).get('cutcode_html','') == True -%}
  <div></div>
 {% else %}
  {{ super() }}
 {% endif %}
{% endblock input_group %}

I'd like to do the equivalent thing for the output prompt.

There has been discussion of how to do this for latex, but I can't figure out how to do it for HTML.

The output_prompt blocks for HTML don't appear to do anything, and whenever I try to make slightly modified versions of the primary templates, they won't load properly.


回答1:


Currently, changing the output prompt is a bit more demanding because if you simply override the block output by extending the full.tpl you have to include a super() call to include the outputs. Unfortunately, the parent block (included by the super call) will add the Out prompt again and will mess up the html.

To get something similar to what you want, I did the following. (Note, I haven't included a complete template here, because the templates are currently changing due to the implementation of the v4 notebook format. Here, I'm using IPython 2.3)

  1. Copy the base.tpl e.g. noprompt.tpl
  2. As this template cannot be used directly, add the code from the full.tpl to noprompt.tpl
  3. change the block input_group as you showed
  4. change the block output to something like

    {% block output %}
    <div class="output_area">
    {%- if cell['metadata'].get('cell_tags',{}).get('cutcode_html','') != True and output.output_type == 'pyout' -%}
    <div class="prompt output_prompt">
    Out[{{ cell.prompt_number }}]:
    {%- else -%}
    <div class="prompt">
    {%- endif -%}
    </div>
    {{ super() }}
    </div>
    {% endblock output %}
    

With this I could convert a notebook with the prompts conditionally removed.



来源:https://stackoverflow.com/questions/26897800/remove-nbconvert-to-html-in-and-out-prompts-based-on-cell-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!