Skip to content

Commit

Permalink
Merge pull request #554 from mpacer/hide_all_x
Browse files Browse the repository at this point in the history
Global (input/output/prompt/&c..) filtering for all templates, improve customisation docs
  • Loading branch information
minrk authored Apr 4, 2017
2 parents 2b96d7d + 6d75bbe commit db30363
Show file tree
Hide file tree
Showing 16 changed files with 24,965 additions and 179 deletions.
24,642 changes: 24,582 additions & 60 deletions docs/source/customizing.ipynb

Large diffs are not rendered by default.

65 changes: 51 additions & 14 deletions docs/source/example.ipynb

Large diffs are not rendered by default.

48 changes: 46 additions & 2 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import uuid
import json

from traitlets import HasTraits, Unicode, List, Dict, default, observe
from traitlets import HasTraits, Unicode, List, Dict, Bool, default, observe
from traitlets.utils.importstring import import_item
from ipython_genutils import py3compat
from jinja2 import (
Expand Down Expand Up @@ -163,7 +163,39 @@ def _template_file_default(self):

#Extension that the template files use.
template_extension = Unicode(".tpl").tag(config=True, affects_environment=True)

exclude_input = Bool(False,
help = "This allows you to exclude code cell inputs from all templates if set to True."
).tag(config=True)

exclude_input_prompt = Bool(False,
help = "This allows you to exclude input prompts from all templates if set to True."
).tag(config=True)

exclude_output = Bool(False,
help = "This allows you to exclude code cell outputs from all templates if set to True."
).tag(config=True)

exclude_output_prompt = Bool(False,
help = "This allows you to exclude output prompts from all templates if set to True."
).tag(config=True)

exclude_code_cell = Bool(False,
help = "This allows you to exclude code cells from all templates if set to True."
).tag(config=True)

exclude_markdown = Bool(False,
help = "This allows you to exclude markdown cells from all templates if set to True."
).tag(config=True)

exclude_raw = Bool(False,
help = "This allows you to exclude raw cells from all templates if set to True."
).tag(config=True)

exclude_unknown = Bool(False,
help = "This allows you to exclude unknown cells from all templates if set to True."
).tag(config=True)

extra_loaders = List(
help="Jinja loaders to find templates. Will be tried in order "
"before the default FileSystem ones.",
Expand Down Expand Up @@ -235,7 +267,19 @@ def from_notebook_node(self, nb, resources=None, **kw):
"""
nb_copy, resources = super(TemplateExporter, self).from_notebook_node(nb, resources, **kw)
resources.setdefault('raw_mimetypes', self.raw_mimetypes)

resources['global_content_filter'] = {
'include_code': not self.exclude_code_cell,
'include_markdown': not self.exclude_markdown,
'include_raw': not self.exclude_raw,
'include_unknown': not self.exclude_unknown,
'include_input': not self.exclude_input,
'include_output': not self.exclude_output,
'include_input_prompt': not self.exclude_input_prompt,
'include_output_prompt': not self.exclude_output_prompt,
'no_prompt': self.exclude_input_prompt and self.exclude_output_prompt,
}

# Top level variables are passed to the template_exporter here.
output = self.template.render(nb=nb_copy, resources=resources)
return output, resources

Expand Down
94 changes: 94 additions & 0 deletions nbconvert/exporters/tests/test_templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from .base import ExportersTestsBase
from .cheese import CheesePreprocessor
from ..templateexporter import TemplateExporter
from ..html import HTMLExporter
from ..markdown import MarkdownExporter
from testpath import tempdir

import pytest
Expand Down Expand Up @@ -144,8 +146,100 @@ def test_fail_to_find_template_file(self):
with pytest.raises(TemplateNotFound):
out, resources = exporter.from_notebook_node(nb)

def test_exclude_code_cell(self):
no_io = {
"TemplateExporter":{
"exclude_output": True,
"exclude_input": True,
"exclude_input_prompt": False,
"exclude_output_prompt": False,
"exclude_markdown": False,
"exclude_code_cell": False,
}
}
c_no_io = Config(no_io)
exporter_no_io = TemplateExporter(config=c_no_io )
exporter_no_io.template_file = 'markdown'
nb_no_io, resources_no_io = exporter_no_io.from_filename(self._get_notebook())

assert not resources_no_io['global_content_filter']['include_input']
assert not resources_no_io['global_content_filter']['include_output']

no_code = {
"TemplateExporter":{
"exclude_output": False,
"exclude_input": False,
"exclude_input_prompt": False,
"exclude_output_prompt": False,
"exclude_markdown": False,
"exclude_code_cell": True,
}
}
c_no_code = Config(no_code)
exporter_no_code = TemplateExporter(config=c_no_code )
exporter_no_code.template_file = 'markdown'
nb_no_code, resources_no_code = exporter_no_code.from_filename(self._get_notebook())

assert not resources_no_code['global_content_filter']['include_code']
assert nb_no_io == nb_no_code


def test_exclude_input_prompt(self):
no_input_prompt = {
"TemplateExporter":{
"exclude_output": False,
"exclude_input": False,
"exclude_input_prompt": True,
"exclude_output_prompt": False,
"exclude_markdown": False,
"exclude_code_cell": False,
}
}
c_no_input_prompt = Config(no_input_prompt)
exporter_no_input_prompt = MarkdownExporter(config=c_no_input_prompt)
nb_no_input_prompt, resources_no_input_prompt = exporter_no_input_prompt.from_filename(self._get_notebook())

assert not resources_no_input_prompt['global_content_filter']['include_input_prompt']
assert "# In[" not in nb_no_input_prompt

def test_exclude_markdown(self):

no_md= {
"TemplateExporter":{
"exclude_output": False,
"exclude_input": False,
"exclude_input_prompt": False,
"exclude_output_prompt": False,
"exclude_markdown": True,
"exclude_code_cell": False,
}
}

c_no_md = Config(no_md)
exporter_no_md = TemplateExporter(config=c_no_md)
exporter_no_md.template_file = 'python'
nb_no_md, resources_no_md = exporter_no_md.from_filename(self._get_notebook())

assert not resources_no_md['global_content_filter']['include_markdown']
assert "First import NumPy and Matplotlib" not in nb_no_md

def test_exclude_output_prompt(self):
no_output_prompt = {
"TemplateExporter":{
"exclude_output": False,
"exclude_input": False,
"exclude_input_prompt": False,
"exclude_output_prompt": True,
"exclude_markdown": False,
"exclude_code_cell": False,
}
}
c_no_output_prompt = Config(no_output_prompt)
exporter_no_output_prompt = HTMLExporter(config=c_no_output_prompt)
nb_no_output_prompt, resources_no_output_prompt = exporter_no_output_prompt.from_filename(self._get_notebook())

assert not resources_no_output_prompt['global_content_filter']['include_output_prompt']
assert "Out[" not in nb_no_output_prompt

def _make_exporter(self, config=None):
# Create the exporter instance, make sure to set a template name since
Expand Down
14 changes: 11 additions & 3 deletions nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ def validate(self, obj, value):
{
'NbConvertApp' : {
'use_output_suffix' : False,
'export_format': 'notebook',
'export_format' : 'notebook',
},
'FilesWriter': {'build_directory': ''},
'FilesWriter' : {'build_directory': ''},
},
"""Run nbconvert in place, overwriting the existing notebook (only
relevant when converting to notebook format)"""
)
),
'no-prompt' : (
{'TemplateExporter' : {
'exclude_input_prompt' : True,
'exclude_output_prompt' : True,
}
},
"Exclude input and output prompts from converted document."
),
})


Expand Down
4 changes: 4 additions & 0 deletions nbconvert/templates/asciidoc.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@


{% block input %}
{% if resources.global_content_filter.include_input_prompt %}
{% if cell.execution_count is defined -%}
+*In[{{ cell.execution_count|replace(None, " ") }}]:*+
{% else %}
+*In[]:*+
{%- endif -%}
{%- endif -%}
[source{% if nb.metadata.language_info %}, {{ nb.metadata.language_info.name }}{% endif %}]
----
{{ cell.source}}
----
{% endblock input %}

{% block output_group %}
{% if resources.global_content_filter.include_output_prompt %}
{% if cell.execution_count is defined -%}
+*Out[{{ cell.execution_count|replace(None, " ") }}]:*+
{%- else -%}
+*Out[]:*+
{%- endif -%}
{%- endif %}
----
{{- super() -}}
Expand Down
8 changes: 8 additions & 0 deletions nbconvert/templates/html/basic.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@
{% block in_prompt -%}
<div class="prompt input_prompt">
{%- if cell.execution_count is defined -%}
{%- if resources.global_content_filter.include_input_prompt-%}
In&nbsp;[{{ cell.execution_count|replace(None, "&nbsp;") }}]:
{%- else -%}
In&nbsp;[&nbsp;]:
{%- endif -%}
{%- endif -%}
</div>
{%- endblock in_prompt %}

{% block empty_in_prompt -%}
{%- if resources.global_content_filter.include_input_prompt-%}
<div class="prompt input_prompt">
</div>
{% endif %}
{%- endblock empty_in_prompt %}

{#
Expand All @@ -53,6 +57,7 @@ In&nbsp;[&nbsp;]:
{% block output %}
<div class="output_area">
{% if resources.global_content_filter.include_output_prompt %}
{% block output_area_prompt %}
{%- if output.output_type == 'execute_result' -%}
<div class="prompt output_prompt">
Expand All @@ -66,13 +71,16 @@ In&nbsp;[&nbsp;]:
{%- endif -%}
</div>
{% endblock output_area_prompt %}
{% endif %}
{{ super() }}
</div>
{% endblock output %}
{% block markdowncell scoped %}
<div class="cell border-box-sizing text_cell rendered">
{%- if resources.global_content_filter.include_input_prompt-%}
{{ self.empty_in_prompt() }}
{%- endif -%}
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
{{ cell.source | markdown2html | strip_files_prefix }}
Expand Down
6 changes: 6 additions & 0 deletions nbconvert/templates/html/full.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ div#notebook {
border-top: none;
}
{%- if resources.global_content_filter.no_prompt-%}
div#notebook-container{
padding: 6ex 12ex 8ex 12ex;
}
{%- endif -%}
@media print {
div.cell {
display: block;
Expand Down
Loading

0 comments on commit db30363

Please sign in to comment.