From 82d6979fb9c1a593b661d8bff362b099bcd80d8d Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sat, 13 Jan 2018 15:25:25 -0800 Subject: [PATCH 1/5] better prompt hiding logic (don't remove all code from latex) --- nbconvert/templates/html/basic.tpl | 42 +++++++++---------- .../templates/latex/style_bw_ipython.tplx | 8 ++++ nbconvert/templates/latex/style_ipython.tplx | 8 ++++ 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/nbconvert/templates/html/basic.tpl b/nbconvert/templates/html/basic.tpl index eaec1f701..380f889b1 100644 --- a/nbconvert/templates/html/basic.tpl +++ b/nbconvert/templates/html/basic.tpl @@ -23,12 +23,12 @@ {% block in_prompt -%}
-{%- if cell.execution_count is defined -%} {%- if resources.global_content_filter.include_input_prompt-%} -In [{{ cell.execution_count|replace(None, " ") }}]: -{%- else -%} -In [ ]: -{%- endif -%} + {%- if cell.execution_count is defined -%} + In [{{ cell.execution_count|replace(None, " ") }}]: + {%- else -%} + In [ ]: + {%- endif -%} {%- endif -%}
{%- endblock in_prompt %} @@ -51,26 +51,26 @@ In [ ]:
{{ cell.source | highlight_code(metadata=cell.metadata) }} -
+
{%- endblock input %} {% block output %}
{% if resources.global_content_filter.include_output_prompt %} -{% block output_area_prompt %} -{%- if output.output_type == 'execute_result' -%} -
-{%- if cell.execution_count is defined -%} - Out[{{ cell.execution_count|replace(None, " ") }}]: -{%- else -%} - Out[ ]: -{%- endif -%} -{%- else -%} -
-{%- endif -%} -
-{% endblock output_area_prompt %} + {% block output_area_prompt %} + {%- if output.output_type == 'execute_result' -%} +
+ {%- if cell.execution_count is defined -%} + Out[{{ cell.execution_count|replace(None, " ") }}]: + {%- else -%} + Out[ ]: + {%- endif -%} + {%- else -%} +
+ {%- endif -%} +
+ {% endblock output_area_prompt %} {% endif %} {{ super() }}
@@ -79,7 +79,7 @@ In [ ]: {% block markdowncell scoped %}
{%- if resources.global_content_filter.include_input_prompt-%} -{{ self.empty_in_prompt() }} + {{ self.empty_in_prompt() }} {%- endif -%}
@@ -97,7 +97,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 %} diff --git a/nbconvert/templates/latex/style_bw_ipython.tplx b/nbconvert/templates/latex/style_bw_ipython.tplx index 9309f9196..43ccc5563 100644 --- a/nbconvert/templates/latex/style_bw_ipython.tplx +++ b/nbconvert/templates/latex/style_bw_ipython.tplx @@ -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 *)) @@ -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 *)) diff --git a/nbconvert/templates/latex/style_ipython.tplx b/nbconvert/templates/latex/style_ipython.tplx index 7a3ab8c3b..10f01a80b 100644 --- a/nbconvert/templates/latex/style_ipython.tplx +++ b/nbconvert/templates/latex/style_ipython.tplx @@ -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 *)) @@ -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 *)) From 4ff26f8df5d9a237fb28a9cdcc58614b7f9082dd Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sat, 13 Jan 2018 15:57:19 -0800 Subject: [PATCH 2/5] add test to latex to make sure input content remains after removing prompts --- nbconvert/exporters/tests/test_latex.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/nbconvert/exporters/tests/test_latex.py b/nbconvert/exporters/tests/test_latex.py index 17a33851c..926c753b8 100644 --- a/nbconvert/exporters/tests/test_latex.py +++ b/nbconvert/exporters/tests/test_latex.py @@ -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 @@ -117,6 +119,26 @@ 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_output": False, + "exclude_input": False, + "exclude_input_prompt": True, + "exclude_output_prompt": True, + "exclude_markdown": False, + "exclude_code_cell": False, + } + } + 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 From 8c71fa6c8249b8f11d54eee6f144d31adb41b6e1 Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sat, 13 Jan 2018 16:14:37 -0800 Subject: [PATCH 3/5] add test to make sure that html properly removes prompts --- nbconvert/exporters/tests/test_html.py | 28 +++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/nbconvert/exporters/tests/test_html.py b/nbconvert/exporters/tests/test_html.py index c7d5b20a5..1d6c3a97c 100644 --- a/nbconvert/exporters/tests/test_html.py +++ b/nbconvert/exporters/tests/test_html.py @@ -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): @@ -59,6 +62,29 @@ 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\[(.*)\]:" + + ins = ["2", "10", " ", " ", "0"] + outs = ["10"] + + assert not re.findall(in_regex, output) + assert not re.findall(out_regex, output) def test_png_metadata(self): """ From b4fc3b567acabf3716b41f071039d2f1a45b0619 Mon Sep 17 00:00:00 2001 From: M Pacer Date: Sat, 13 Jan 2018 22:13:58 -0800 Subject: [PATCH 4/5] add tests for asciidoc; improve tests for html/latex; html template --- nbconvert/exporters/tests/test_asciidoc.py | 28 ++++++++++++++++++- nbconvert/exporters/tests/test_html.py | 3 -- nbconvert/exporters/tests/test_latex.py | 4 --- nbconvert/templates/html/basic.tpl | 32 ++++++++++------------ 4 files changed, 42 insertions(+), 25 deletions(-) diff --git a/nbconvert/exporters/tests/test_asciidoc.py b/nbconvert/exporters/tests/test_asciidoc.py index be08077b4..65075f9e9 100644 --- a/nbconvert/exporters/tests/test_asciidoc.py +++ b/nbconvert/exporters/tests/test_asciidoc.py @@ -12,9 +12,13 @@ # 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 @@ -39,3 +43,25 @@ def test_export(self): """ (output, resources) = ASCIIDocExporter().from_filename(self._get_notebook()) assert len(output) > 0 + + @dec.onlyif_cmds_exist('pandoc') + def test_export(self): + """ + Can a ASCIIDocExporter export something? + """ + 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")) + + in_regex = r"In \[(.*)\]:" + out_regex = r"Out\[(.*)\]:" + + assert not re.findall(in_regex, output) + assert not re.findall(out_regex, output) diff --git a/nbconvert/exporters/tests/test_html.py b/nbconvert/exporters/tests/test_html.py index 1d6c3a97c..1a771e023 100644 --- a/nbconvert/exporters/tests/test_html.py +++ b/nbconvert/exporters/tests/test_html.py @@ -80,9 +80,6 @@ def test_prompt_number(self): in_regex = r"In \[(.*)\]:" out_regex = r"Out\[(.*)\]:" - ins = ["2", "10", " ", " ", "0"] - outs = ["10"] - assert not re.findall(in_regex, output) assert not re.findall(out_regex, output) diff --git a/nbconvert/exporters/tests/test_latex.py b/nbconvert/exporters/tests/test_latex.py index 926c753b8..b58273293 100644 --- a/nbconvert/exporters/tests/test_latex.py +++ b/nbconvert/exporters/tests/test_latex.py @@ -123,12 +123,8 @@ def test_prompt_number_color(self): def test_no_prompt_yes_input(self): no_prompt = { "TemplateExporter":{ - "exclude_output": False, - "exclude_input": False, "exclude_input_prompt": True, "exclude_output_prompt": True, - "exclude_markdown": False, - "exclude_code_cell": False, } } c_no_prompt = Config(no_prompt) diff --git a/nbconvert/templates/html/basic.tpl b/nbconvert/templates/html/basic.tpl index 380f889b1..9159415ab 100644 --- a/nbconvert/templates/html/basic.tpl +++ b/nbconvert/templates/html/basic.tpl @@ -23,21 +23,17 @@ {% block in_prompt -%}
-{%- if resources.global_content_filter.include_input_prompt-%} {%- if cell.execution_count is defined -%} In [{{ cell.execution_count|replace(None, " ") }}]: {%- else -%} In [ ]: {%- endif -%} -{%- endif -%}
{%- endblock in_prompt %} {% block empty_in_prompt -%} -{%- if resources.global_content_filter.include_input_prompt-%}
-{% endif %} {%- endblock empty_in_prompt %} {# @@ -55,22 +51,24 @@
{%- endblock input %} +{% block output_area_prompt %} +{%- if output.output_type == 'execute_result' -%} +
+ {%- if cell.execution_count is defined -%} + Out[{{ cell.execution_count|replace(None, " ") }}]: + {%- else -%} + Out[ ]: + {%- endif -%} +{%- else -%} +
+{%- endif -%} +
+{% endblock output_area_prompt %} + {% block output %}
{% if resources.global_content_filter.include_output_prompt %} - {% block output_area_prompt %} - {%- if output.output_type == 'execute_result' -%} -
- {%- if cell.execution_count is defined -%} - Out[{{ cell.execution_count|replace(None, " ") }}]: - {%- else -%} - Out[ ]: - {%- endif -%} - {%- else -%} -
- {%- endif -%} -
- {% endblock output_area_prompt %} + {{ self.output_area_prompt() }} {% endif %} {{ super() }}
From b2c7cb056591d23f87f515c5b3b761f935fd0fe6 Mon Sep 17 00:00:00 2001 From: M Pacer Date: Tue, 23 Jan 2018 10:01:33 -0800 Subject: [PATCH 5/5] test for prompts by default; update regex to be global for both tests --- nbconvert/exporters/tests/test_asciidoc.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nbconvert/exporters/tests/test_asciidoc.py b/nbconvert/exporters/tests/test_asciidoc.py index 65075f9e9..9fb20be47 100644 --- a/nbconvert/exporters/tests/test_asciidoc.py +++ b/nbconvert/exporters/tests/test_asciidoc.py @@ -23,6 +23,8 @@ #----------------------------------------------------------------------------- # Class #----------------------------------------------------------------------------- +in_regex = r"In\[(.*)\]:" +out_regex = r"Out\[(.*)\]:" class TestASCIIDocExporter(ExportersTestsBase): """Tests for ASCIIDocExporter""" @@ -44,10 +46,13 @@ 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(self): + def test_export_no_prompt(self): """ - Can a ASCIIDocExporter export something? + Can a ASCIIDocExporter export something without prompts? """ no_prompt = { "TemplateExporter":{ @@ -60,8 +65,5 @@ def test_export(self): (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)