emacs-ipython-notebook/tests/notebook/nbformat3/Animations Using clear_output.ipynb

194 lines
93 KiB
Text
Raw Normal View History

{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Simple animations Using clear_output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sometimes you want to clear the output area in the middle of a calculation. This can be useful for doing simple animations. In terminals, there is the carriage-return (`'\\r'`) for overwriting a single line, but the notebook frontend can clear the whole output area, not just a single line.\n",
"\n",
"To clear output in the Notebook you can use the `clear_output()` function. If you are clearing the output every frame of an animation, calling `clear_output()` will create noticeable flickering. You can use `clear_output(wait=True)` to add the *clear_output* call to a queue. When data becomes available to replace the existing output, the *clear_output* will be called immediately before the new data is added. This avoids the flickering by not rendering the cleared output to the screen."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Simple example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we show our progress iterating through a list:"
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"import sys\n",
"import time"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import display, clear_output\n",
"for i in range(10):\n",
" time.sleep(0.25)\n",
" clear_output(wait=True)\n",
" print(i)\n",
" sys.stdout.flush()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"9\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"AsyncResult.wait_interactive"
]
},
{
"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](/#clusters)**"
]
},
{
"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": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 100/100 tasks finished after 6 s"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"done\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Matplotlib example"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use `clear_output()` to clear figures and plots."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
},
{
"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(wait=True)\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": [
{
"metadata": {},
"output_type": "display_data",
"png": "iVBORw0KGgoAAAANSUhEUgAAAloAAAGKCAYAAADUje9YAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4U+fZx/GvJMt774H3wOABmG32CitANmkzmzRpk6ZN\nSdpmJzTrzWpmm9WRBVklhB32DBsMBoyx8cA23lOesiyd8/5hcEKBBIxtedyf69J1dKxxbksY/fSc\nZ2hUVVURQgghhBAdTmvtAoQQQggheisJWkIIIYQQnUSClhBCCCFEJ5GgJYQQQgjRSSRoCSGEEEJ0\nEglaQgghhBCdRIKWEKJH+Pjjj/nVr371k/epqanh2muvxcXFhZdffrmLKhNCiIuToCWEYOHChWi1\n2raLj48PM2fOZNeuXdYurY1Go/nZ+/ztb3/D09OTsrIyFixY0AVVXZjJZCIuLo4jR45YrQYhRPcg\nQUsIgUaj4dZbb0VRFBRF4cSJE8TFxTF9+nQyMzOtXd4lS01NZf78+Tg4OGBra2u1OmxtbUlLSyMx\nMdFqNQghugcJWkIIVFXlx4tEeHl58dprrxEYGMjKlSutWNnlqaurw97evsuPO3HiRLZv397lxxVC\ndH8StIQQF2U2m3F3d7d2GZfFGquKaTQaqxxXCNH9SdASQpynoqKCRx99FFtbW+bPn3/ObWvXrmXw\n4MHY29sTGRnJe++9d87tn3zyCTExMTg6OpKcnExqaioAFouFP/3pT/j7++Pi4sL8+fOpqqpqe1xJ\nSQk333wzrq6ueHh4cOedd1JZWXlJ9X788cdotVq2bdvGpEmT0Ol0QGtL07Zt286578KFC9s61be0\ntKDVatm9ezdXX301zs7OxMbGsnTp0nMeYzabefHFFwkPD8fe3p5hw4axYcOG846r1Wr59NNPAQgL\nCyM/P7/tOQwGA/fffz++vr44Ojoyfvx49u7d23b7pdbyyiuvEBoaipOTE9OnTycvL++SXiMhhHVI\n0BJCALB48eK2zvC+vr4sWrSIV199FWdn57b7LFmyhEceeYR33nmHmpoaVq9ezdKlS3nppZcAOHr0\nKAsWLOCLL76gsrKSMWPG8PnnnwPw/vvvs337dg4ePEhOTg61tbVtp9sMBgNjx45l/PjxnDp1ipyc\nHOLi4pg0aRJms/lna7/zzjtRFIUJEyawdetWLBYL0NrS9L+d6H+8r9frAbjnnnt44IEHKC8v55ln\nnuH222+nrKys7X633nora9as4ZtvvqGmpobHHnuMF198kdtuu+2c4yqKwu23337ecZqbm0lOTsZk\nMrF3716Kior49a9/zcyZM1m+fPnP1lJeXg7A6tWree+999iwYQPFxcX4+vqyatWqn319hBBWpAoh\n+rxnnnlGve2229r2q6ur1S+//FL18PBQP/nkE1VVVdVisaj9+vVTc3Nzz3lsQUGB6ubmpqqqqi5f\nvlyNiIhQTSbTecd46KGH1FtuueWCx3/00UfVZ5555ryfT506Vf3mm29UVVXVjz76SL3zzjt/8veY\nOHGiunXr1nP2t23bds59Fi5ceM7zaDQa9d133z3nPqNHj1ZXrVqlqqqq7ty5U3V3d1crKysv+biq\nqqphYWFqXl6eqqqq+tprr6mjR48+73EffPCBGhMTc8m1vP322+q4ceMuWocQovuRFi0hBHBu3yZ3\nd3fmz5/P448/3tZalZOTQ2FhIREREedMBRESEkJdXR2FhYVMmTIFHx8fIiIiuO+++1i6dCmKogBw\n9913s2HDBoYMGcKjjz56ztQRO3bs4Nlnnz3nebVaLZs3byY9Pf2yfo9LmQbif40bN+6c/YiIiLbT\nmjt37mT06NF4enq2+7hbt25l7ty55/38mmuu4eTJk20tVher5ewp1BtvvJHi4mJiYmJYsGAB69ev\n/+lfTAhhdRK0hBAXNWDAAHJzc4HWIKHVajEYDG3TQJy9WCwWgoKCcHJyYteuXXzyySd4eHiwYMEC\nbrjhBgAGDhxITk4OTz75JFVVVcycOZO3334bAK1WyzvvvHPB533iiSfajn+5NBrNeaceGxoazruf\no6PjOft6vb4teLbnuBeq41L9VC3+/v6kpaXxxhtvoKoqt956Kw899NAV1yeE6DwStIQQFw0CBw8e\nJDY2FmhtWQkKCmrrc3VWfX09OTk5bfuKojB58mRefPFF9uzZw7Jly6iqqkJRFBwdHbn++uv58MMP\nef/99/nnP/8JwIQJE857XqCtI317eXh4nNdZ/Pvvv7+k4HM23IwZM4Y9e/ac03H/f2k0mrZ+YRcy\nceLEC06TsXz5cmJiYvDx8fnZes7SarXMnj2bN998k+XLl7e9hkKI7kmClhDivKkJysvL+eCDD3j5\n5Zd56qmngNYw8dZbb/GXv/yFRYsWUVtby/Hjx5k7dy6vvPIKAIsWLWL06NHk5OTQ2NjIl19+iZ+f\nH66urvzmN7/h7rvvpqqqivLyclauXElMTAwAf/nLXzh9+jR33303+fn5VFdX88ILLzBjxgxqa2sv\nWOOlGDduHG+88QYZGRnU1tby+OOPk5ube1nPNXr0aKZPn84111zD4c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A0K9YuRfxI//79rV2CuALy/vVc8t71bPL+9X5X1Bm+s1gUlfIGE0W1zRTXtm4La5sprm2mqM6E\nvY2WZrNCpJcDgwKcSQhwJs7XCXu9ztqlCyGEEEK06VZB68l12RTVNlNab8Ld3oZAVzsCXO0IdLVj\nsq8jgS6t+062OowtFtLKGjhaXM+ilBKyK5uI8HQgMcCZxABn4vyccJDgJYQQQggr6lZBa3asN4Gu\ntvi72GFn89NnNe31OoYGuTI0qHXmeaNZIb20gSMl9XxxuISTFU2EediTGODMyBA3Evydu+JXEEII\nIYRo062C1uhQt3Y/1t5Gy5Ag
"text": [
"<matplotlib.figure.Figure at 0x10f01b310>"
]
}
],
"prompt_number": 5
}
],
"metadata": {}
}
]
}