|
21 | 21 |
|
22 | 22 | from zmq import MessageTracker |
23 | 23 |
|
24 | | -from IPython.core.display import clear_output |
| 24 | +from IPython.core.display import clear_output, display |
25 | 25 | from IPython.external.decorator import decorator |
26 | 26 | from IPython.parallel import error |
27 | 27 |
|
@@ -377,6 +377,140 @@ def wait_interactive(self, interval=1., timeout=None): |
377 | 377 | sys.stdout.flush() |
378 | 378 | print |
379 | 379 | print "done" |
| 380 | + |
| 381 | + def _republish_displaypub(self, content, eid): |
| 382 | + """republish individual displaypub content dicts""" |
| 383 | + try: |
| 384 | + ip = get_ipython() |
| 385 | + except NameError: |
| 386 | + # displaypub is meaningless outside IPython |
| 387 | + return |
| 388 | + md = content['metadata'] or {} |
| 389 | + md['engine'] = eid |
| 390 | + ip.display_pub.publish(content['source'], content['data'], md) |
| 391 | + |
| 392 | + |
| 393 | + def _display_single_result(self): |
| 394 | + |
| 395 | + print self.stdout |
| 396 | + print >> sys.stderr, self.stderr |
| 397 | + |
| 398 | + try: |
| 399 | + get_ipython() |
| 400 | + except NameError: |
| 401 | + # displaypub is meaningless outside IPython |
| 402 | + return |
| 403 | + |
| 404 | + for output in self.outputs: |
| 405 | + self._republish_displaypub(output, self.engine_id) |
| 406 | + |
| 407 | + if self.pyout is not None: |
| 408 | + display(self.get()) |
| 409 | + |
| 410 | + @check_ready |
| 411 | + def display_outputs(self, groupby="type"): |
| 412 | + """republish the outputs of the computation |
| 413 | + |
| 414 | + Parameters |
| 415 | + ---------- |
| 416 | + |
| 417 | + groupby : str [default: type] |
| 418 | + if 'type': |
| 419 | + Group outputs by type (show all stdout, then all stderr, etc.): |
| 420 | + |
| 421 | + [stdout:1] foo |
| 422 | + [stdout:2] foo |
| 423 | + [stderr:1] bar |
| 424 | + [stderr:2] bar |
| 425 | + if 'engine': |
| 426 | + Display outputs for each engine before moving on to the next: |
| 427 | + |
| 428 | + [stdout:1] foo |
| 429 | + [stderr:1] bar |
| 430 | + [stdout:2] foo |
| 431 | + [stderr:2] bar |
| 432 | + |
| 433 | + if 'order': |
| 434 | + Like 'type', but further collate individual displaypub |
| 435 | + outputs. This is meant for cases of each command producing |
| 436 | + several plots, and you would like to see all of the first |
| 437 | + plots together, then all of the second plots, and so on. |
| 438 | + """ |
| 439 | + # flush iopub, just in case |
| 440 | + self._client._flush_iopub(self._client._iopub_socket) |
| 441 | + if self._single_result: |
| 442 | + self._display_single_result() |
| 443 | + return |
| 444 | + |
| 445 | + stdouts = [s.rstrip() for s in self.stdout] |
| 446 | + stderrs = [s.rstrip() for s in self.stderr] |
| 447 | + pyouts = [p for p in self.pyout] |
| 448 | + output_lists = self.outputs |
| 449 | + results = self.get() |
| 450 | + |
| 451 | + targets = self.engine_id |
| 452 | + |
| 453 | + if groupby == "engine": |
| 454 | + for eid,stdout,stderr,outputs,r,pyout in zip( |
| 455 | + targets, stdouts, stderrs, output_lists, results, pyouts |
| 456 | + ): |
| 457 | + if stdout: |
| 458 | + print '[stdout:%2i]' % eid, stdout |
| 459 | + if stderr: |
| 460 | + print '[stderr:%2i]' % eid, stderr |
| 461 | + |
| 462 | + try: |
| 463 | + get_ipython() |
| 464 | + except NameError: |
| 465 | + # displaypub is meaningless outside IPython |
| 466 | + return |
| 467 | + |
| 468 | + for output in outputs: |
| 469 | + self._republish_displaypub(output, eid) |
| 470 | + |
| 471 | + if pyout is not None: |
| 472 | + display(r) |
| 473 | + |
| 474 | + elif groupby in ('type', 'order'): |
| 475 | + # republish stdout: |
| 476 | + if any(stdouts): |
| 477 | + for eid,stdout in zip(targets, stdouts): |
| 478 | + print '[stdout:%2i]' % eid, stdout |
| 479 | + |
| 480 | + # republish stderr: |
| 481 | + if any(stderrs): |
| 482 | + for eid,stderr in zip(targets, stderrs): |
| 483 | + print >> sys.stderr, '[stderr:%2i]' % eid, stderr |
| 484 | + |
| 485 | + try: |
| 486 | + get_ipython() |
| 487 | + except NameError: |
| 488 | + # displaypub is meaningless outside IPython |
| 489 | + return |
| 490 | + |
| 491 | + if groupby == 'order': |
| 492 | + output_dict = dict((eid, outputs) for eid,outputs in zip(targets, output_lists)) |
| 493 | + N = max(len(outputs) for outputs in output_lists) |
| 494 | + for i in range(N): |
| 495 | + for eid in targets: |
| 496 | + outputs = output_dict[eid] |
| 497 | + if len(outputs) >= N: |
| 498 | + self._republish_displaypub(outputs[i], eid) |
| 499 | + else: |
| 500 | + # republish displaypub output |
| 501 | + for eid,outputs in zip(targets, output_lists): |
| 502 | + for output in outputs: |
| 503 | + self._republish_displaypub(output, eid) |
| 504 | + |
| 505 | + # finally, add pyout: |
| 506 | + for eid,r,pyout in zip(targets, results, pyouts): |
| 507 | + if pyout is not None: |
| 508 | + display(r) |
| 509 | + |
| 510 | + else: |
| 511 | + raise ValueError("groupby must be one of 'type', 'engine', 'collate', not %r" % groupby) |
| 512 | + |
| 513 | + |
380 | 514 |
|
381 | 515 |
|
382 | 516 | class AsyncMapResult(AsyncResult): |
|
0 commit comments