{ "metadata": { "name": "ipythonblocks_animation" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Animation with ipythonblocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more on `ipythonblocks` see the home page at [https://github.com/jiffyclub/ipythonblocks](https://github.com/jiffyclub/ipythonblocks)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "from IPython.display import clear_output" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's possible to do animation of sorts using IPython's `clear_output` function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(3, 3)\n", "\n", "previous_block = None\n", "\n", "for block in grid:\n", " clear_output()\n", " block.green = 255\n", " \n", " if previous_block:\n", " previous_block.green = 0\n", " \n", " grid.show()\n", " \n", " previous_block = block\n", " \n", " time.sleep(0.2)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And this is so much fun that it's been added to `ipythonblocks`: one way is to use the `BlockGrid.animate()` method. The `.animate()` method takes\n", "an optional `stop_time` keyword to control the amount of time between loop steps." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(3, 3)\n", "\n", "previous_block = None\n", "\n", "for block in grid.animate():\n", " block.green = 255\n", " \n", " if previous_block:\n", " previous_block.green = 0\n", " \n", " previous_block = block" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And another way, if not iterating over the grid, is to use the `BlockGrid.flash()` method. `.flash()` takes an optional `display_time` keyword that\n", "controls for how long each frame is displayed. The default is 0.2 seconds." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(3, 3)\n", "\n", "previous_block = None\n", "\n", "indices = [[(0, 0), (0, 2), (2, 0), (2, 2)], [(0, 1), (1, 0), (1, 2), (2, 1)]] * 10\n", "\n", "for ind in indices:\n", " for i in ind:\n", " grid[i[0], i[1]].green = 255\n", " \n", " grid.flash(display_time=0.1)\n", " \n", " for i in ind:\n", " grid[i[0], i[1]].green = 0\n", "\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }