{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook snippets, tips and tricks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**TODO**:\n", "* Read https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/\n", "* Read http://blog.juliusschulz.de/blog/ultimate-ipython-notebook\n", "* howto avoid loosing matplotlib interactive rendering when a document is converted to HTML ?\n", " * https://www.reddit.com/r/IPython/comments/36p360/try_matplotlib_notebook_for_interactive_plots/\n", " * http://stackoverflow.com/questions/36151181/exporting-interactive-jupyter-notebook-to-html\n", " * https://jakevdp.github.io/blog/2013/12/05/static-interactive-widgets/\n", "* table of contents (JS)\n", "* matplotlib / D3.js interaction\n", "* matplotlib animations: how to make it faster\n", "* inspiration\n", " * http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/\n", " * https://github.com/ltiao/notebooks\n", " * https://blog.dominodatalab.com/lesser-known-ways-of-using-notebooks/\n", "* Howto make (personalized) Reveal.js slides from this notebook: https://forum.poppy-project.org/t/utiliser-jupyter-pour-des-presentations-etape-par-etape-use-jupyter-to-present-step-by-step/2271/2\n", "* See https://blog.dominodatalab.com/lesser-known-ways-of-using-notebooks/\n", "\n", "Extension wishlist and todo:\n", "- Table of content\n", "- Hide some blocks in the HTML export\n", " - See https://github.com/jupyter/notebook/issues/534\n", "- Customize CSS in HTML export\n", "- Add disqus in HTML export\n", " - See: https://github.com/jupyter/nbviewer/issues/80\n", " - Example: http://nbviewer.jupyter.org/gist/tonyfast/977184c1243287e7e55e\n", "- Add metadata header/footer (initial publication date, last revision date, author, email, website, license, ...)\n", "- Vim like editor/navigation shortcut keys (search, search+edit, ...)\n", "- Spell checking\n", " - See https://github.com/ipython/ipython/issues/3216#issuecomment-59507673 and http://www.simulkade.com/posts/2015-04-07-spell-checking-in-jupyter-notebooks.html\n", " \n", "Inspiration:\n", "- https://github.com/jupyter/jupyter/wiki/A-gallery-of-interesting-Jupyter-Notebooks" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%html\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%javascript\n", "var toc = document.getElementById(\"toc\");\n", "toc.innerHTML = \"Table of contents:\";\n", "toc.innerHTML += \"
    \"\n", "\n", "var h2_list = document.getElementsByTagName(\"h2\");\n", "for (var i = 0; i < h2_list.length; i++) {\n", " var h2 = h2_list[i];\n", " var h2_str = h2.textContent.slice(0, -1); // \"slice(0, -1)\" remove the last character \n", " toc.innerHTML += \"
  1. \" + h2_str + \"
  2. \";\n", "}\n", "\n", "toc.innerHTML += \"
\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import directives" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib notebook\n", "\n", "# As an alternative, one may use: %pylab notebook\n", "\n", "# For old Matplotlib and Ipython versions, use the non-interactive version:\n", "# %matplotlib inline or %pylab inline\n", "\n", "# To ignore warnings (http://stackoverflow.com/questions/9031783/hide-all-warnings-in-ipython)\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "import math\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "import ipywidgets\n", "from ipywidgets import interact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Useful keyboard shortcuts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Enter **edit mode**: Enter\n", "* Enter **command mode**: Escape\n", "\n", "In **command** mode:\n", "\n", "* Show keyboard shortcuts: h\n", "\n", "\n", "* Find and replace: f\n", "\n", "\n", "* Insert a cell above the selection: a\n", "* Insert a cell below the selection: b\n", "* Switch to Markdown: m\n", "\n", "\n", "* Delete the selected cells: dd (type twice 'd' quickly)\n", "* Undo cell deletion: z\n", "\n", "\n", "* Execute the selected cell: Ctrl + Enter\n", "* Execute the selected cell and select the next cell: Shift + Enter\n", "* Execute the selected cell and insert below: Alt + Enter\n", "\n", "\n", "* Toggle output: o\n", "* Toggle line number: l\n", "\n", "\n", "* Copy selected cells: c\n", "* Paste copied cells below: v\n", "\n", "\n", "* Select the previous cell: k\n", "* Select the next cell: j\n", "\n", "In **edit** mode:\n", "\n", "* Code completion or indent: Tab\n", "* Tooltip: Shift + Tab\n", " * Type \"Shift + Tab\" twice to see the online documentation of the selected element\n", " * Type \"Shift + Tab\" 4 times to the online documentation in a dedicated frame\n", "\n", "\n", "* Indent: ⌘] (on MacOS)\n", "* Dedent: ⌘[ (on MacOS)\n", "\n", "\n", "* Execute the selected cell: Ctrl + Enter\n", "* Execute the selected cell and select the next cell: Shift + Enter\n", "* Execute the selected cell and insert below: Alt + Enter\n", "\n", "\n", "* Cut a cell at the current cursor position: Ctrl + Shift + -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Matplotlib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To plot a figure within a notebook, insert the\n", "```%matplotlib notebook``` (or ```%pylab notebook```)\n", "directive at the begining of the document.\n", "\n", "As an alternative, one may use\n", "```%matplotlib inline``` (or ```%pylab inline```)\n", "for non-interactive plots on old Matplotlib/Ipython versions." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2D plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)\n", "y = np.sin(x)\n", "plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3D plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from mpl_toolkits.mplot3d import axes3d\n", "\n", "# Build datas ###############\n", "\n", "x = np.arange(-5, 5, 0.25)\n", "y = np.arange(-5, 5, 0.25)\n", "\n", "xx,yy = np.meshgrid(x, y)\n", "z = np.sin(np.sqrt(xx**2 + yy**2))\n", "\n", "# Plot data #################\n", "\n", "fig = plt.figure()\n", "ax = axes3d.Axes3D(fig)\n", "ax.plot_wireframe(xx, yy, z)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from matplotlib.animation import FuncAnimation\n", "\n", "# Plots\n", "fig, ax = plt.subplots()\n", "\n", "def update(frame):\n", " x = np.arange(frame/10., frame/10. + 2. * math.pi, 0.1)\n", " ax.clear()\n", " ax.plot(x, np.cos(x))\n", "\n", " # Optional: save plots\n", " filename = \"img_{:03}.png\".format(frame)\n", " plt.savefig(filename)\n", "\n", "# Note: \"interval\" is in ms\n", "anim = FuncAnimation(fig, update, interval=100)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## IPython built-in magic commands" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See http://ipython.readthedocs.io/en/stable/interactive/magics.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Execute an external python script" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%run ./notebook_snippets_run_test.py" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%run ./notebook_snippets_run_mpl_test.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load an external python script" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load the full script" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load ./notebook_snippets_run_mpl_test.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load a specific symbol (funtion, class, ...)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load -s main ./notebook_snippets_run_mpl_test.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load specific lines" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%load -r 22-41 ./notebook_snippets_run_mpl_test.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Time measurement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### %time" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%time\n", "plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### %timeit" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%timeit\n", "plt.hist(np.random.normal(loc=0.0, scale=1.0, size=100000), bins=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ipywidget" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "#help(ipywidgets)\n", "#dir(ipywidgets)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from ipywidgets import IntSlider\n", "from IPython.display import display\n", "\n", "slider = IntSlider(min=1, max=10)\n", "display(slider)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ipywidgets.interact" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Documentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See http://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [], "source": [ "#help(ipywidgets.interact)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using interact as a decorator with named parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To me, this is the best option for single usage functions..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(text=\"IPython Widgets\")\n", "def greeting(text):\n", " print(\"Hello {}\".format(text))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Integer (IntSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(num=5)\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(num=(0, 100))\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(num=(0, 100, 10))\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Float (FloatSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(num=5.)\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(num=(0., 10.))\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "@interact(num=(0., 10., 0.5))\n", "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Boolean (Checkbox)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(upper=False)\n", "def greeting(upper):\n", " text = \"hello\"\n", " if upper:\n", " print(text.upper())\n", " else:\n", " print(text.lower())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(name=[\"John\", \"Bob\", \"Alice\"])\n", "def greeting(name):\n", " print(\"Hello {}\".format(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionnary (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(word={\"One\": \"Un\", \"Two\": \"Deux\", \"Three\": \"Trois\"})\n", "def translate(word):\n", " print(word)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)\n", "\n", "@interact(function={\"Sin\": np.sin, \"Cos\": np.cos})\n", "def plot(function):\n", " y = function(x)\n", " plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using interact as a decorator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def greeting(text=\"World\"):\n", " print(\"Hello {}\".format(text))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Integer (IntSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def square(num=2):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def square(num=(0, 100)):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def square(num=(0, 100, 10)):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Float (FloatSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def square(num=5.):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def square(num=(0., 10.)):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "@interact\n", "def square(num=(0., 10., 0.5)):\n", " print(\"{} squared is {}\".format(num, num*num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Boolean (Checkbox)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def greeting(upper=False):\n", " text = \"hello\"\n", " if upper:\n", " print(text.upper())\n", " else:\n", " print(text.lower())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def greeting(name=[\"John\", \"Bob\", \"Alice\"]):\n", " print(\"Hello {}\".format(name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionnary (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact\n", "def translate(word={\"One\": \"Un\", \"Two\": \"Deux\", \"Three\": \"Trois\"}):\n", " print(word)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)\n", "\n", "@interact\n", "def plot(function={\"Sin\": np.sin, \"Cos\": np.cos}):\n", " y = function(x)\n", " plt.plot(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using interact as a function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To me, this is the best option for multiple usage functions..." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def greeting(text):\n", " print(\"Hello {}\".format(text))\n", " \n", "interact(greeting, text=\"IPython Widgets\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Integer (IntSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=(0, 100))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=(0, 100, 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Float (FloatSlider)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=5.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=(0., 10.))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "def square(num):\n", " print(\"{} squared is {}\".format(num, num*num))\n", "\n", "interact(square, num=(0., 10., 0.5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Boolean (Checkbox)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def greeting(upper):\n", " text = \"hello\"\n", " if upper:\n", " print(text.upper())\n", " else:\n", " print(text.lower())\n", "\n", "interact(greeting, upper=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### List (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def greeting(name):\n", " print(\"Hello {}\".format(name))\n", "\n", "interact(greeting, name=[\"John\", \"Bob\", \"Alice\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Dictionnary (Dropdown)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def translate(word):\n", " print(word)\n", "\n", "interact(translate, word={\"One\": \"Un\", \"Two\": \"Deux\", \"Three\": \"Trois\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)\n", "\n", "def plot(function):\n", " y = function(x)\n", " plt.plot(x, y)\n", "\n", "interact(plot, function={\"Sin\": np.sin, \"Cos\": np.cos})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example of using multiple widgets on one function" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@interact(upper=False, name=[\"john\", \"bob\", \"alice\"])\n", "def greeting(upper, name):\n", " text = \"hello {}\".format(name)\n", " if upper:\n", " print(text.upper())\n", " else:\n", " print(text.lower())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sound player widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "See: https://ipython.org/ipython-doc/dev/api/generated/IPython.display.html#IPython.display.Audio" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from IPython.display import Audio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate a sound" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "framerate = 44100\n", "t = np.linspace(0, 5, framerate*5)\n", "data = np.sin(2*np.pi*220*t) + np.sin(2*np.pi*224*t)\n", "\n", "Audio(data, rate=framerate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate a multi-channel (stereo or more) sound" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_left = np.sin(2 * np.pi * 220 * t)\n", "data_right = np.sin(2 * np.pi * 224 * t)\n", "\n", "Audio([data_left, data_right], rate=framerate)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From URL" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Audio(\"http://www.nch.com.au/acm/8k16bitpcm.wav\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Audio(url=\"http://www.w3schools.com/html/horse.ogg\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Audio('/path/to/sound.wav')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Audio(filename='/path/to/sound.ogg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### From bytes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Audio(b'RAW_WAV_DATA..)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Audio(data=b'RAW_WAV_DATA..)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Youtube widget" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Class for embedding a YouTube Video in an IPython session, based on its video id.\n", "e.g. to embed the video from https://www.youtube.com/watch?v=0HlRtU8clt4 , you would do:\n", "\n", "See https://ipython.org/ipython-doc/dev/api/generated/IPython.display.html#IPython.display.YouTubeVideo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from IPython.display import YouTubeVideo" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "vid = YouTubeVideo(\"0HlRtU8clt4\")\n", "display(vid)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert a Reveal.js presentation written with Markdown to a Jupyter notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a **quick and dirty hack** to have one cell per slide in the notebook; it assumes the string \"---\" is used to separate slides within the markdown file.\n", "\n", "1. copy the markdown document within the Jupyter notebook (in a Markdown cell), save it and close it;\n", "2. to split this cell at each \"---\", open the ipynb notebook with vim and enter the following command and save the file:\n", "\n", "```\n", ":%s/,\\n \"---\\\\n\",/\\r ]\\r },\\r {\\r \"cell_type\": \"markdown\",\\r \"metadata\": {},\\r \"source\": [/gc\n", "```" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }