diff --git a/IPython/html/static/notebook/js/codecell.js b/IPython/html/static/notebook/js/codecell.js index 73228e8..37df9c3 100644 --- a/IPython/html/static/notebook/js/codecell.js +++ b/IPython/html/static/notebook/js/codecell.js @@ -410,7 +410,7 @@ define([ CodeCell.input_prompt_classical = function (prompt_value, lines_number) { var ns; - if (prompt_value === undefined) { + if (prompt_value === undefined || prompt_value === null) { ns = " "; } else { ns = encodeURIComponent(prompt_value); diff --git a/IPython/html/tests/notebook/prompt_numbers.js b/IPython/html/tests/notebook/prompt_numbers.js new file mode 100644 index 0000000..21e6acd --- /dev/null +++ b/IPython/html/tests/notebook/prompt_numbers.js @@ -0,0 +1,36 @@ + +// Test +casper.notebook_test(function () { + + var that = this; + var set_prompt = function (i, val) { + that.evaluate(function (i, val) { + var cell = IPython.notebook.get_cell(i); + cell.set_input_prompt(val); + }, [i, val]); + }; + + var get_prompt = function (i) { + return that.evaluate(function (i) { + var elem = IPython.notebook.get_cell(i).element; + return elem.find('div.input_prompt').html(); + }, [i]); + }; + + this.then(function () { + var a = 'print("a")'; + var index = this.append_cell(a); + + this.test.assertEquals(get_prompt(index), "In [ ]:", "prompt number is   by default"); + set_prompt(index, 2); + this.test.assertEquals(get_prompt(index), "In [2]:", "prompt number is 2"); + set_prompt(index, 0); + this.test.assertEquals(get_prompt(index), "In [0]:", "prompt number is 0"); + set_prompt(index, "*"); + this.test.assertEquals(get_prompt(index), "In [*]:", "prompt number is *"); + set_prompt(index, undefined); + this.test.assertEquals(get_prompt(index), "In [ ]:", "prompt number is  "); + set_prompt(index, null); + this.test.assertEquals(get_prompt(index), "In [ ]:", "prompt number is  "); + }); +}); diff --git a/IPython/nbconvert/exporters/tests/files/prompt_numbers.ipynb b/IPython/nbconvert/exporters/tests/files/prompt_numbers.ipynb new file mode 100644 index 0000000..2e26355 --- /dev/null +++ b/IPython/nbconvert/exporters/tests/files/prompt_numbers.ipynb @@ -0,0 +1,81 @@ +{ + "metadata": { + "name": "notebook2" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 2 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "evs = np.zeros(100)", + "evs.shape" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 10, + "text": [ + "(100,)" + ] + } + ], + "prompt_number": 10 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": null + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": "*" + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 0 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/IPython/nbconvert/exporters/tests/test_html.py b/IPython/nbconvert/exporters/tests/test_html.py index ca4941a..be1b2b1 100644 --- a/IPython/nbconvert/exporters/tests/test_html.py +++ b/IPython/nbconvert/exporters/tests/test_html.py @@ -15,6 +15,7 @@ from .base import ExportersTestsBase from ..html import HTMLExporter from IPython.testing.decorators import onlyif_any_cmd_exists +import re #----------------------------------------------------------------------------- # Class @@ -59,3 +60,18 @@ class TestHTMLExporter(ExportersTestsBase): (output, resources) = HTMLExporter(template_file='full').from_filename(self._get_notebook()) assert len(output) > 0 + @onlyif_any_cmd_exists('nodejs', 'node', 'pandoc') + def test_prompt_number(self): + """ + Does HTMLExporter properly format input and output prompts? + """ + (output, resources) = HTMLExporter(template_file='full').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 re.findall(in_regex, output) == ins + assert re.findall(out_regex, output) == outs diff --git a/IPython/nbconvert/exporters/tests/test_latex.py b/IPython/nbconvert/exporters/tests/test_latex.py index a6effc9..2b4d2a4 100644 --- a/IPython/nbconvert/exporters/tests/test_latex.py +++ b/IPython/nbconvert/exporters/tests/test_latex.py @@ -5,6 +5,7 @@ import os.path import textwrap +import re from .base import ExportersTestsBase from ..latex import LatexExporter @@ -99,3 +100,19 @@ class TestLatexExporter(ExportersTestsBase): (output, resources) = LatexExporter(template_file='article').from_filename(nbfile) assert len(output) > 0 + + @onlyif_cmds_exist('pandoc') + def test_prompt_number_color(self): + """ + Does LatexExporter properly format input and output prompts in color? + """ + (output, resources) = LatexExporter().from_filename( + self._get_notebook(nb_name="prompt_numbers.ipynb")) + in_regex = r"In \[\{\\color\{incolor\}(.*)\}\]:" + out_regex = r"Out\[\{\\color\{outcolor\}(.*)\}\]:" + + ins = ["2", "10", " ", " ", "*", "0"] + outs = ["10"] + + assert re.findall(in_regex, output) == ins + assert re.findall(out_regex, output) == outs diff --git a/IPython/nbconvert/templates/html/basic.tpl b/IPython/nbconvert/templates/html/basic.tpl index db287f3..c240e90 100644 --- a/IPython/nbconvert/templates/html/basic.tpl +++ b/IPython/nbconvert/templates/html/basic.tpl @@ -23,7 +23,11 @@ {% block in_prompt -%}
-In [{{ cell.prompt_number }}]: +{%- if cell.prompt_number is defined -%} +In [{{ cell.prompt_number|replace(None, " ") }}]: +{%- else -%} +In [ ]: +{%- endif -%}
{%- endblock in_prompt %} @@ -51,7 +55,11 @@ In [{{ cell.prompt_number }}]:
{%- if output.output_type == 'pyout' -%}
- Out[{{ cell.prompt_number }}]: +{%- if cell.prompt_number is defined -%} + Out[{{ cell.prompt_number|replace(None, " ") }}]: +{%- else -%} + Out[ ]: +{%- endif -%} {%- else -%}
{%- endif -%} diff --git a/IPython/nbconvert/templates/latex/style_bw_ipython.tplx b/IPython/nbconvert/templates/latex/style_bw_ipython.tplx index 84859ab..7aa97bf 100644 --- a/IPython/nbconvert/templates/latex/style_bw_ipython.tplx +++ b/IPython/nbconvert/templates/latex/style_bw_ipython.tplx @@ -33,7 +33,11 @@ % Name: draw_prompt % Purpose: Renders an output/input prompt ((* macro add_prompt(text, cell, prompt) -*)) - ((*- set prompt_number = "" ~ cell.prompt_number -*)) + ((*- if cell.prompt_number is defined -*)) + ((*- set prompt_number = "" ~ (cell.prompt_number | replace(None, " ")) -*)) + ((*- else -*)) + ((*- set prompt_number = " " -*)) + ((*- endif -*)) ((*- set indentation = " " * (prompt_number | length + 7) -*)) \begin{verbatim} (((- text | add_prompts(first=prompt ~ '[' ~ prompt_number ~ ']: ', cont=indentation) -))) diff --git a/IPython/nbconvert/templates/latex/style_ipython.tplx b/IPython/nbconvert/templates/latex/style_ipython.tplx index 7799d18..5eb7b9d 100644 --- a/IPython/nbconvert/templates/latex/style_ipython.tplx +++ b/IPython/nbconvert/templates/latex/style_ipython.tplx @@ -46,7 +46,11 @@ % Name: draw_prompt % Purpose: Renders an output/input prompt ((* macro add_prompt(text, cell, prompt, prompt_color) -*)) - ((*- set prompt_number = "" ~ cell.prompt_number -*)) + ((*- if cell.prompt_number is defined -*)) + ((*- set prompt_number = "" ~ (cell.prompt_number | replace(None, " ")) -*)) + ((*- else -*)) + ((*- set prompt_number = " " -*)) + ((*- endif -*)) ((*- set indention = " " * (prompt_number | length + 7) -*)) \begin{Verbatim}[commandchars=\\\{\}] ((( text | add_prompts(first='{\color{' ~ prompt_color ~ '}' ~ prompt ~ '[{\\color{' ~ prompt_color ~ '}' ~ prompt_number ~ '}]:} ', cont=indention) )))