Repository: ipython/ipython

Issue:
Eliminating display of intermediate stages in progress bars
Migrated from https://github.com/ipython/nbconvert/issues/185
Original author @fonnesbeck

---

When working with notebooks that use code that generates visual outputs such as progress bars, displaying them either in nbviewer or as an export format (e.g. latex) results in the intermediate outputs from such components are printed, taking up a huge amount of document space. These need to be manually deleted in order for the document to be usable.

Here is what it looks like:

![example](http://f.cl.ly/items/2d0d1N021V1w35402e3o/Screen%20Shot%202013-06-18%20at%202.26.44%20PM.png)

[Here's an example notebook with this behavior](http://nbviewer.ipython.org/urls/raw.github.com/pymc-devs/pymc/glm/pymc/examples/GLM.ipynb)



Discussion:
The example provided in the description is out of date, please use [this example](http://nbviewer.ipython.org/gist/fonnesbeck/5214637) provided by @fonnesbeck yesterday.

Does something like this would be good enough to clean `\b` without messing up with `\n` ?

``` python
a = '''abc\rdef\nhij\rklm\nnop'''
print(a)

lines = a.split('\n')
cleaned_lines = [l.split('\r')[-1] for l in lines]
'\n'.join(cleaned_lines)
print(a) # should display identically 
```

> Does something like this would be good enough to clean \b without messing up with \n ?

I think so.  

In nbconvert we have a preprocessor that is supposed to coalesce the output stream.  It was there that I ended up adding some logic to parse the '\r' characters in #5387.


Resolving patch:
diff --git a/IPython/nbconvert/preprocessors/coalescestreams.py b/IPython/nbconvert/preprocessors/coalescestreams.py
--- a/IPython/nbconvert/preprocessors/coalescestreams.py
+++ b/IPython/nbconvert/preprocessors/coalescestreams.py
@@ -11,9 +11,13 @@
 #-----------------------------------------------------------------------------
 
 #-----------------------------------------------------------------------------
-# Functions
+# Imports
 #-----------------------------------------------------------------------------
+import re
 
+#-----------------------------------------------------------------------------
+# Functions
+#-----------------------------------------------------------------------------
 def cell_preprocessor(function):
     """
     Wrap a function to be executed on all cells of a notebook
@@ -67,6 +71,10 @@ def coalesce_streams(cell, resources, index):
             last.stream == output.stream
         ):
             last.text += output.text
+
+            # Respect \r characters.
+            cr_pat = re.compile(r'.*\r(?=[^\n])')
+            last.text = cr_pat.sub('', last.text)
         else:
             new_outputs.append(output)
             last = output


Test patch:
diff --git a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py
--- a/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py
+++ b/IPython/nbconvert/preprocessors/tests/test_coalescestreams.py
@@ -13,7 +13,6 @@
 #-----------------------------------------------------------------------------
 # Imports
 #-----------------------------------------------------------------------------
-
 from IPython.nbformat import current as nbformat
 
 from .base import PreprocessorTestsBase
@@ -23,7 +22,6 @@
 #-----------------------------------------------------------------------------
 # Class
 #-----------------------------------------------------------------------------
-
 class TestCoalesceStreams(PreprocessorTestsBase):
     """Contains test functions for coalescestreams.py"""
 
@@ -38,10 +36,8 @@ def test_coalesce_streams(self):
         self.assertEqual(outputs[2].text, "cd")
         self.assertEqual(outputs[3].text, "ef")
 
-
     def test_coalesce_sequenced_streams(self):
         """Can the coalesce streams preprocessor merge a sequence of streams?"""
-
         outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="0"),
                    nbformat.new_output(output_type="stream", stream="stdout", output_text="1"),
                    nbformat.new_output(output_type="stream", stream="stdout", output_text="2"),
@@ -58,3 +54,20 @@ def test_coalesce_sequenced_streams(self):
         nb, res = coalesce_streams(nb, res)
         outputs = nb.worksheets[0].cells[0].outputs
         self.assertEqual(outputs[0].text, u'01234567')
+
+    def test_coalesce_replace_streams(self):
+        """Are \\r characters handled?"""
+        outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="z"),
+                   nbformat.new_output(output_type="stream", stream="stdout", output_text="\ra"),
+                   nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz\rb"),
+                   nbformat.new_output(output_type="stream", stream="stdout", output_text="\nz"),
+                   nbformat.new_output(output_type="stream", stream="stdout", output_text="\rc\n"),
+                   nbformat.new_output(output_type="stream", stream="stdout", output_text="z\rz\rd")]
+        cells=[nbformat.new_code_cell(input="# None", prompt_number=1,outputs=outputs)]
+        worksheets = [nbformat.new_worksheet(name="worksheet1", cells=cells)]
+
+        nb = nbformat.new_notebook(name="notebook1", worksheets=worksheets)
+        res = self.build_resources()
+        nb, res = coalesce_streams(nb, res)
+        outputs = nb.worksheets[0].cells[0].outputs
+        self.assertEqual(outputs[0].text, u'a\nb\nc\nd')