{ "metadata": { "name": "Animations_and_Progress" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Simple animations, progress bars, and clearing output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you want to print progress in-place, but don't want\n", "to keep growing the output area. In terminals, there is the carriage-return\n", "(`'\\r'`) for overwriting a single line, but the notebook frontend does not support this\n", "behavior (yet).\n", "\n", "What the notebook *does* support is explicit `clear_output`, and you can use this to replace previous\n", "output (specifying stdout/stderr or the special IPython display outputs)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simple example printing our progress iterating through a list:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "import sys\n", "import time" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import clear_output\n", "for i in range(10):\n", " time.sleep(0.25)\n", " clear_output()\n", " print i\n", " sys.stdout.flush()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The AsyncResult object has a special `wait_interactive()` method, which prints its progress interactively,\n", "so you can watch as your parallel computation completes.\n", "\n", "**This example assumes you have an IPython cluster running, which you can start from the [cluster panel](/#tab2)**" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython import parallel\n", "rc = parallel.Client()\n", "view = rc.load_balanced_view()\n", "\n", "amr = view.map_async(time.sleep, [0.5]*100)\n", "\n", "amr.wait_interactive()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use `clear_output()` to clear figures and plots.\n", "\n", "This time, we need to make sure we are using inline pylab (**requires matplotlib**)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from scipy.special import jn\n", "x = np.linspace(0,5)\n", "f, ax = plt.subplots()\n", "ax.set_title(\"Bessel functions\")\n", "\n", "for n in range(1,10):\n", " time.sleep(1)\n", " ax.plot(x, jn(x,n))\n", " clear_output()\n", " display(f)\n", "\n", "# close the figure at the end, so we don't get a duplicate\n", "# of the last plot\n", "plt.close()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "A Javascript Progress Bar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`clear_output()` is still something of a hack, and if you want to do a progress bar in the notebook\n", "it is better to just use Javascript/HTML if you can.\n", "\n", "Here is a simple progress bar using HTML/Javascript:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import uuid\n", "from IPython.display import HTML, Javascript, display\n", "\n", "divid = str(uuid.uuid4())\n", "\n", "pb = HTML(\n", "\"\"\"\n", "