Skip to content

Commit

Permalink
Merge pull request #734 from mpacer/better_content_hiding
Browse files Browse the repository at this point in the history
Better content hiding; template & testing improvements
  • Loading branch information
takluyver authored Jan 23, 2018
2 parents 62646ea + b2c7cb0 commit adf85ce
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 22 deletions.
30 changes: 29 additions & 1 deletion nbconvert/exporters/tests/test_asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
# Imports
#-----------------------------------------------------------------------------

import re

from traitlets.config import Config
from ipython_genutils.testing import decorators as dec

from .base import ExportersTestsBase
from ..asciidoc import ASCIIDocExporter
from ipython_genutils.testing import decorators as dec

#-----------------------------------------------------------------------------
# Class
#-----------------------------------------------------------------------------
in_regex = r"In\[(.*)\]:"
out_regex = r"Out\[(.*)\]:"

class TestASCIIDocExporter(ExportersTestsBase):
"""Tests for ASCIIDocExporter"""
Expand All @@ -39,3 +45,25 @@ def test_export(self):
"""
(output, resources) = ASCIIDocExporter().from_filename(self._get_notebook())
assert len(output) > 0

assert re.findall(in_regex, output)
assert re.findall(out_regex, output)

@dec.onlyif_cmds_exist('pandoc')
def test_export_no_prompt(self):
"""
Can a ASCIIDocExporter export something without prompts?
"""
no_prompt = {
"TemplateExporter":{
"exclude_input_prompt": True,
"exclude_output_prompt": True,
}
}
c_no_prompt = Config(no_prompt)
exporter = ASCIIDocExporter(config=c_no_prompt)
(output, resources) = exporter.from_filename(
self._get_notebook(nb_name="prompt_numbers.ipynb"))

assert not re.findall(in_regex, output)
assert not re.findall(out_regex, output)
25 changes: 24 additions & 1 deletion nbconvert/exporters/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import re

from .base import ExportersTestsBase
from ..html import HTMLExporter

from traitlets.config import Config
from nbformat import v4
import re


class TestHTMLExporter(ExportersTestsBase):
Expand Down Expand Up @@ -59,6 +62,26 @@ def test_prompt_number(self):

assert re.findall(in_regex, output) == ins
assert re.findall(out_regex, output) == outs

def test_prompt_number(self):
"""
Does HTMLExporter properly format input and output prompts?
"""
no_prompt_conf = Config(
{"TemplateExporter":{
"exclude_input_prompt": True,
"exclude_output_prompt": True,
}
}
)
exporter = HTMLExporter(config=no_prompt_conf, template_file='full')
(output, resources) = exporter.from_filename(
self._get_notebook(nb_name="prompt_numbers.ipynb"))
in_regex = r"In \[(.*)\]:"
out_regex = r"Out\[(.*)\]:"

assert not re.findall(in_regex, output)
assert not re.findall(out_regex, output)

def test_png_metadata(self):
"""
Expand Down
18 changes: 18 additions & 0 deletions nbconvert/exporters/tests/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

from .base import ExportersTestsBase
from ..latex import LatexExporter

from traitlets.config import Config
from nbformat import write
from nbformat import v4
from ipython_genutils.testing.decorators import onlyif_cmds_exist
Expand Down Expand Up @@ -117,6 +119,22 @@ def test_prompt_number_color(self):
assert re.findall(in_regex, output) == ins
assert re.findall(out_regex, output) == outs

@onlyif_cmds_exist('pandoc')
def test_no_prompt_yes_input(self):
no_prompt = {
"TemplateExporter":{
"exclude_input_prompt": True,
"exclude_output_prompt": True,
}
}
c_no_prompt = Config(no_prompt)

exporter = LatexExporter(config=c_no_prompt)
(output, resources) = exporter.from_filename(
self._get_notebook(nb_name="prompt_numbers.ipynb"))
assert "shape" in output
assert "evs" in output

def test_in_memory_template_tplx(self):
# Loads in an in memory latex template (.tplx) using jinja2.DictLoader
# creates a class that uses this template with the template_file argument
Expand Down
38 changes: 18 additions & 20 deletions nbconvert/templates/html/basic.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@

{% 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 -%}
{%- if cell.execution_count is defined -%}
In&nbsp;[{{ cell.execution_count|replace(None, "&nbsp;") }}]:
{%- else -%}
In&nbsp;[&nbsp;]:
{%- 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 @@ -51,26 +47,28 @@ In&nbsp;[&nbsp;]:
<div class="inner_cell">
<div class="input_area">
{{ cell.source | highlight_code(metadata=cell.metadata) }}
</div>
</div>
</div>
{%- endblock input %}
{% 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">
{%- if cell.execution_count is defined -%}
Out[{{ cell.execution_count|replace(None, "&nbsp;") }}]:
{%- else -%}
Out[&nbsp;]:
{%- endif -%}
{%- if cell.execution_count is defined -%}
Out[{{ cell.execution_count|replace(None, "&nbsp;") }}]:
{%- else -%}
Out[&nbsp;]:
{%- endif -%}
{%- else -%}
<div class="prompt">
{%- endif -%}
</div>
{% endblock output_area_prompt %}
{% block output %}
<div class="output_area">
{% if resources.global_content_filter.include_output_prompt %}
{{ self.output_area_prompt() }}
{% endif %}
{{ super() }}
</div>
Expand All @@ -79,7 +77,7 @@ In&nbsp;[&nbsp;]:
{% block markdowncell scoped %}
<div class="cell border-box-sizing text_cell rendered">
{%- if resources.global_content_filter.include_input_prompt-%}
{{ self.empty_in_prompt() }}
{{ self.empty_in_prompt() }}
{%- endif -%}
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
Expand All @@ -97,7 +95,7 @@ unknown type {{ cell.type }}
{%- set extra_class="output_execute_result" -%}
{% block data_priority scoped %}
{{ super() }}
{% endblock %}
{% endblock data_priority %}
{%- set extra_class="" -%}
{%- endblock execute_result %}
Expand Down
8 changes: 8 additions & 0 deletions nbconvert/templates/latex/style_bw_ipython.tplx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
((* block input scoped *))
((*- if resources.global_content_filter.include_input_prompt *))
((( add_prompt(cell.source, cell, 'In ') )))
((* else *))
(((cell.source)))
((* endif -*))
((* endblock input *))

Expand All @@ -25,6 +27,12 @@
((*- else -*))
\verb+Out[((( cell.execution_count )))]:+((( super() )))
((*- endif -*))
((*- else -*))
((*- if type in ['text/plain'] *))
((( output.data['text/plain'] )))
((*- else -*))
\verb+((( super() )))
((*- endif -*))
((*- endif -*))
((*- endfor -*))
((* endblock execute_result *))
Expand Down
8 changes: 8 additions & 0 deletions nbconvert/templates/latex/style_ipython.tplx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
((* block input scoped *))
((*- if resources.global_content_filter.include_input_prompt *))
((( add_prompt(cell.source | highlight_code(strip_verbatim=True, metadata=cell.metadata), cell, 'In ', 'incolor') )))
((*- else *))
((( cell.source | highlight_code(strip_verbatim=True, metadata=cell.metadata) )))
((* endif *))
((* endblock input *))

Expand All @@ -38,6 +40,12 @@
((* else -*))
\texttt{\color{outcolor}Out[{\color{outcolor}((( cell.execution_count )))}]:}((( super() )))
((*- endif -*))
((*- else -*))
((*- if type in ['text/plain'] *))
((( output.data['text/plain'] | escape_latex )))
((* else -*))
((( super() )))
((*- endif -*))
((*- endif -*))
((*- endfor -*))
((* endblock execute_result *))
Expand Down

0 comments on commit adf85ce

Please sign in to comment.