Skip to content

Commit

Permalink
Merge pull request #1822 from minrk/asyncaesthetics
Browse files Browse the repository at this point in the history
aesthetics pass on AsyncResult.display_outputs

adds a few delimiters when there are rich outputs, and cleans up a few cases of extra trailing whitespace on stdout/err
  • Loading branch information
minrk committed Jun 1, 2012
2 parents 7d8e4a4 + f2fab60 commit da8a781
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 86 deletions.
62 changes: 40 additions & 22 deletions IPython/parallel/client/asyncresult.py
Expand Up @@ -15,13 +15,15 @@
# Imports
#-----------------------------------------------------------------------------

from __future__ import print_function

import sys
import time
from datetime import datetime

from zmq import MessageTracker

from IPython.core.display import clear_output, display
from IPython.core.display import clear_output, display, display_pretty
from IPython.external.decorator import decorator
from IPython.parallel import error

Expand All @@ -38,6 +40,9 @@ def _total_seconds(td):
# Python 2.6
return 1e-6 * (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6)

def _raw_text(s):
display_pretty(s, raw=True)

#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -373,10 +378,10 @@ def wait_interactive(self, interval=1., timeout=None):
while not self.ready() and (timeout is None or time.time() - tic <= timeout):
self.wait(interval)
clear_output()
print "%4i/%i tasks finished after %4i s" % (self.progress, N, self.elapsed),
print("%4i/%i tasks finished after %4i s" % (self.progress, N, self.elapsed), end="")
sys.stdout.flush()
print
print "done"
print()
print("done")

def _republish_displaypub(self, content, eid):
"""republish individual displaypub content dicts"""
Expand All @@ -388,20 +393,31 @@ def _republish_displaypub(self, content, eid):
md = content['metadata'] or {}
md['engine'] = eid
ip.display_pub.publish(content['source'], content['data'], md)

def _display_stream(self, text, prefix='', file=None):
if not text:
# nothing to display
return
if file is None:
file = sys.stdout
end = '' if text.endswith('\n') else '\n'

multiline = text.count('\n') > int(text.endswith('\n'))
if prefix and multiline and not text.startswith('\n'):
prefix = prefix + '\n'
print("%s%s" % (prefix, text), file=file, end=end)


def _display_single_result(self):
if self.stdout:
print self.stdout
if self.stderr:
print >> sys.stderr, self.stderr
self._display_stream(self.stdout)
self._display_stream(self.stderr, file=sys.stderr)

try:
get_ipython()
except NameError:
# displaypub is meaningless outside IPython
return

for output in self.outputs:
self._republish_displaypub(output, self.engine_id)

Expand Down Expand Up @@ -443,9 +459,9 @@ def display_outputs(self, groupby="type"):
self._display_single_result()
return

stdouts = [s.rstrip() for s in self.stdout]
stderrs = [s.rstrip() for s in self.stderr]
pyouts = [p for p in self.pyout]
stdouts = self.stdout
stderrs = self.stderr
pyouts = self.pyout
output_lists = self.outputs
results = self.get()

Expand All @@ -455,17 +471,18 @@ def display_outputs(self, groupby="type"):
for eid,stdout,stderr,outputs,r,pyout in zip(
targets, stdouts, stderrs, output_lists, results, pyouts
):
if stdout:
print '[stdout:%i]' % eid, stdout
if stderr:
print >> sys.stderr, '[stderr:%i]' % eid, stderr
self._display_stream(stdout, '[stdout:%i] ' % eid)
self._display_stream(stderr, '[stderr:%i] ' % eid, file=sys.stderr)

try:
get_ipython()
except NameError:
# displaypub is meaningless outside IPython
return

if outputs or pyout is not None:
_raw_text('[output:%i]' % eid)

for output in outputs:
self._republish_displaypub(output, eid)

Expand All @@ -474,14 +491,12 @@ def display_outputs(self, groupby="type"):

elif groupby in ('type', 'order'):
# republish stdout:
if any(stdouts):
for eid,stdout in zip(targets, stdouts):
print '[stdout:%i]' % eid, stdout
for eid,stdout in zip(targets, stdouts):
self._display_stream(stdout, '[stdout:%i] ' % eid)

# republish stderr:
if any(stderrs):
for eid,stderr in zip(targets, stderrs):
print >> sys.stderr, '[stderr:%i]' % eid, stderr
for eid,stderr in zip(targets, stderrs):
self._display_stream(stderr, '[stderr:%i] ' % eid, file=sys.stderr)

try:
get_ipython()
Expand All @@ -496,10 +511,13 @@ def display_outputs(self, groupby="type"):
for eid in targets:
outputs = output_dict[eid]
if len(outputs) >= N:
_raw_text('[output:%i]' % eid)
self._republish_displaypub(outputs[i], eid)
else:
# republish displaypub output
for eid,outputs in zip(targets, output_lists):
if outputs:
_raw_text('[output:%i]' % eid)
for output in outputs:
self._republish_displaypub(output, eid)

Expand Down
10 changes: 7 additions & 3 deletions IPython/parallel/client/client.py
Expand Up @@ -118,10 +118,14 @@ def _repr_pretty_(self, p, cycle):
out = TermColors.Red
normal = TermColors.Normal

if '\n' in text_out and not text_out.startswith('\n'):
# add newline for multiline reprs
text_out = '\n' + text_out

p.text(
u'[%i] ' % self.metadata['engine_id'] +
out + u'Out[%i]: ' % self.execution_count +
normal + text_out
out + u'Out[%i:%i]: ' % (
self.metadata['engine_id'], self.execution_count
) + normal + text_out
)

def _repr_html_(self):
Expand Down
4 changes: 3 additions & 1 deletion IPython/parallel/tests/test_asyncresult.py
Expand Up @@ -212,7 +212,7 @@ def test_display_empty_streams_single(self):
with capture_output() as io:
ar.display_outputs()
self.assertEquals(io.stderr, '')
self.assertTrue('5555' in io.stdout)
self.assertEquals('5555\n', io.stdout)

ar = v.execute("a=5")
ar.get(5)
Expand All @@ -232,6 +232,7 @@ def test_display_empty_streams_type(self):
ar.display_outputs()
self.assertEquals(io.stderr, '')
self.assertEquals(io.stdout.count('5555'), len(v), io.stdout)
self.assertFalse('\n\n' in io.stdout, io.stdout)
self.assertEquals(io.stdout.count('[stdout:'), len(v), io.stdout)

ar = v.execute("a=5")
Expand All @@ -252,6 +253,7 @@ def test_display_empty_streams_engine(self):
ar.display_outputs('engine')
self.assertEquals(io.stderr, '')
self.assertEquals(io.stdout.count('5555'), len(v), io.stdout)
self.assertFalse('\n\n' in io.stdout, io.stdout)
self.assertEquals(io.stdout.count('[stdout:'), len(v), io.stdout)

ar = v.execute("a=5")
Expand Down

0 comments on commit da8a781

Please sign in to comment.