##// END OF EJS Templates
Document _ipython_display_ in custom display logic
MinRK -
Show More
@@ -8546,13 +8546,104 b''
8546 8546 ],
8547 8547 "metadata": {},
8548 8548 "output_type": "pyout",
8549 "prompt_number": 16,
8549 "prompt_number": 17,
8550 8550 "text": [
8551 8551 "Polynomial([-20., 71., -15., 1.], [-1., 1.], [-1., 1.])"
8552 8552 ]
8553 8553 }
8554 8554 ],
8555 "prompt_number": 16
8555 "prompt_number": 17
8556 },
8557 {
8558 "cell_type": "heading",
8559 "level": 2,
8560 "metadata": {},
8561 "source": [
8562 "More complex display with `_ipython_display_`"
8563 ]
8564 },
8565 {
8566 "cell_type": "markdown",
8567 "metadata": {},
8568 "source": [
8569 "Rich reprs can only display one object or mime-type at a time.\n",
8570 "Sometimes this is not enough,\n",
8571 "because you need to get javascript on the page, or you want LaTeX and a PNG.\n",
8572 "This can be done with multiple calls to display easily enough,\n",
8573 "but then users need to know more than they should.\n",
8574 "\n",
8575 "In **IPython 2.0**, we added a special `_ipython_display_` method,\n",
8576 "that allows your objects to take control of displaying.\n",
8577 "If this method is defined, IPython will call it, and make no effort to display the object itself.\n",
8578 "It's a way for you to say \"Back off, IPython, I got this.\""
8579 ]
8580 },
8581 {
8582 "cell_type": "code",
8583 "collapsed": false,
8584 "input": [
8585 "import json\n",
8586 "import uuid\n",
8587 "from IPython.display import display_javascript, display_html, display\n",
8588 "\n",
8589 "class FlotPlot(object):\n",
8590 " def __init__(self, x, y):\n",
8591 " self.x = x\n",
8592 " self.y = y\n",
8593 " self.uuid = str(uuid.uuid4())\n",
8594 " \n",
8595 " def _ipython_display_(self):\n",
8596 " json_data = json.dumps(zip(self.x, self.y))\n",
8597 " display_html('<div id=\"{}\" style=\"height: 300px; width:80%;\"></div>'.format(self.uuid),\n",
8598 " raw=True\n",
8599 " )\n",
8600 " display_javascript(\"\"\"\n",
8601 " require([\"//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js\"], function() {\n",
8602 " var line = JSON.parse(\"%s\");\n",
8603 " console.log(line);\n",
8604 " $.plot(\"#%s\", [line]);\n",
8605 " });\n",
8606 " \"\"\" % (json_data, self.uuid), raw=True)\n"
8607 ],
8608 "language": "python",
8609 "metadata": {},
8610 "outputs": [],
8611 "prompt_number": 17
8612 },
8613 {
8614 "cell_type": "code",
8615 "collapsed": false,
8616 "input": [
8617 "import numpy as np\n",
8618 "x = np.linspace(0,10)\n",
8619 "y = np.sin(x)\n",
8620 "FlotPlot(x, np.sin(x))"
8621 ],
8622 "language": "python",
8623 "metadata": {},
8624 "outputs": [
8625 {
8626 "html": [
8627 "<div id=\"6cc58a29-42e4-43ff-9204-1043632e90f4\" style=\"height: 300px; width:80%;\"></div>"
8628 ],
8629 "metadata": {},
8630 "output_type": "display_data"
8631 },
8632 {
8633 "javascript": [
8634 "\n",
8635 " require([\"//cdnjs.cloudflare.com/ajax/libs/flot/0.8.2/jquery.flot.min.js\"], function() {\n",
8636 " 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",
8637 " console.log(line);\n",
8638 " $.plot(\"#6cc58a29-42e4-43ff-9204-1043632e90f4\", [line]);\n",
8639 " });\n",
8640 " "
8641 ],
8642 "metadata": {},
8643 "output_type": "display_data"
8644 }
8645 ],
8646 "prompt_number": 19
8556 8647 }
8557 8648 ],
8558 8649 "metadata": {}
General Comments 0
You need to be logged in to leave comments. Login now