diff --git a/examples/notebooks/Custom Display Logic.ipynb b/examples/notebooks/Custom Display Logic.ipynb index 2614ba6..08c55a3 100644 --- a/examples/notebooks/Custom Display Logic.ipynb +++ b/examples/notebooks/Custom Display Logic.ipynb @@ -8546,13 +8546,104 @@ ], "metadata": {}, "output_type": "pyout", - "prompt_number": 16, + "prompt_number": 17, "text": [ "Polynomial([-20., 71., -15., 1.], [-1., 1.], [-1., 1.])" ] } ], - "prompt_number": 16 + "prompt_number": 17 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "More complex display with `_ipython_display_`" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Rich reprs can only display one object or mime-type at a time.\n", + "Sometimes this is not enough,\n", + "because you need to get javascript on the page, or you want LaTeX and a PNG.\n", + "This can be done with multiple calls to display easily enough,\n", + "but then users need to know more than they should.\n", + "\n", + "In **IPython 2.0**, we added a special `_ipython_display_` method,\n", + "that allows your objects to take control of displaying.\n", + "If this method is defined, IPython will call it, and make no effort to display the object itself.\n", + "It's a way for you to say \"Back off, IPython, I got this.\"" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import json\n", + "import uuid\n", + "from IPython.display import display_javascript, display_html, display\n", + "\n", + "class FlotPlot(object):\n", + " def __init__(self, x, y):\n", + " self.x = x\n", + " self.y = y\n", + " self.uuid = str(uuid.uuid4())\n", + " \n", + " def _ipython_display_(self):\n", + " json_data = json.dumps(zip(self.x, self.y))\n", + " display_html('
'.format(self.uuid),\n", + " raw=True\n", + " )\n", + " display_javascript(\"\"\"\n", + " require([\"//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js\"], function() {\n", + " var line = JSON.parse(\"%s\");\n", + " console.log(line);\n", + " $.plot(\"#%s\", [line]);\n", + " });\n", + " \"\"\" % (json_data, self.uuid), raw=True)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 17 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "x = np.linspace(0,10)\n", + "y = np.sin(x)\n", + "FlotPlot(x, np.sin(x))" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "html": [ + "
" + ], + "metadata": {}, + "output_type": "display_data" + }, + { + "javascript": [ + "\n", + " require([\"//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js\"], function() {\n", + " var line = JSON.parse(\"[[0.0, 0.0], [0.20408163265306123, 0.20266793654820095], [0.40816326530612246, 0.39692414892492234], [0.61224489795918369, 0.57470604121617908], [0.81632653061224492, 0.72863478346935029], [1.0204081632653061, 0.85232156971961837], [1.2244897959183674, 0.94063278511248671], [1.4285714285714286, 0.98990307637212394], [1.6326530612244898, 0.99808748213471832], [1.8367346938775511, 0.96484630898376322], [2.0408163265306123, 0.89155923041100371], [2.2448979591836737, 0.7812680235262639], [2.4489795918367347, 0.63855032022660208], [2.6530612244897958, 0.46932961277720098], [2.8571428571428572, 0.28062939951435684], [3.0612244897959187, 0.080281674842813497], [3.2653061224489797, -0.12339813736217871], [3.4693877551020407, -0.32195631507261868], [3.6734693877551021, -0.50715170948451438], [3.8775510204081636, -0.67129779355193209], [4.0816326530612246, -0.80758169096833643], [4.2857142857142856, -0.91034694431078278], [4.4897959183673475, -0.97532828606704558], [4.6938775510204085, -0.99982866838408957], [4.8979591836734695, -0.98283120392563061], [5.1020408163265305, -0.92504137173820289], [5.3061224489795915, -0.82885773637304272], [5.5102040816326534, -0.69827239556539955], [5.7142857142857144, -0.53870528838615628], [5.9183673469387754, -0.35677924089893803], [6.1224489795918373, -0.16004508604325057], [6.3265306122448983, 0.043331733368683463], [6.5306122448979593, 0.24491007101197931], [6.7346938775510203, 0.43632342647181932], [6.9387755102040813, 0.6096271964908323], [7.1428571428571432, 0.75762841539272019], [7.3469387755102042, 0.87418429881973347], [7.5510204081632653, 0.95445719973875187], [7.7551020408163271, 0.99511539477766364], [7.9591836734693882, 0.99447136726361685], [8.1632653061224492, 0.95255184753146038], [8.3673469387755102, 0.87109670348232071], [8.5714285714285712, 0.75348672743963763], [8.7755102040816322, 0.60460331650615429], [8.979591836734695, 0.43062587038273736], [9.183673469387756, 0.23877531564403087], [9.387755102040817, 0.037014401485062368], [9.591836734693878, -0.16628279384875641], [9.795918367346939, -0.36267842882654883], [10.0, -0.54402111088936989]]\");\n", + " console.log(line);\n", + " $.plot(\"#6cc58a29-42e4-43ff-9204-1043632e90f4\", [line]);\n", + " });\n", + " " + ], + "metadata": {}, + "output_type": "display_data" + } + ], + "prompt_number": 19 } ], "metadata": {}