public
Last active

  • Download Gist
callbacks.ipynb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
"metadata": {
"name": "callbacks"
},
"nbformat": 3,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"source": [
"*this cell loads the flot jQuery plugin, used for plotting*",
"<script type=\"text/javascript\">",
" // load flot",
" $.getScript(\"http://cdnjs.cloudflare.com/ajax/libs/flot/0.7/jquery.flot.min.js\", function(){});",
"</script>"
]
},
{
"cell_type": "heading",
"level": 1,
"source": [
"Interactive JavaScript plots with kernel callbacks"
]
},
{
"cell_type": "markdown",
"source": [
"This is an example of a plotting widget, entirely in javascript, ",
"which can make callbacks to the Kernel to update data.",
"",
"In this case, there is a user-defined function (`update_plot`) that",
"generates the new data for the plot. The widget has sliders, which",
"trigger calls to this function when they change. There is a javascript",
"callback hooked up, which updates a plot area with the new data when it arrives."
]
},
{
"cell_type": "markdown",
"source": [
"You must run this one code cell to define the function before it is available to the widget."
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
"import json",
"from IPython.core.display import JSON, display",
"from scipy.special import jn",
"from numpy import linspace",
"",
"def update_plot(n, xmax=10, npoints=200):",
" x = linspace(0, xmax, npoints)",
" lines = []",
" for i in range(1,n+1):",
" lines.append(zip(x,jn(x,i)))",
" display(JSON(json.dumps(lines)))"
],
"language": "python",
"outputs": [],
"prompt_number": 314
},
{
"cell_type": "markdown",
"source": [
"**Bessel Functions**",
"",
"<div id=\"theplot\" style=\"width:600px;height:300px;\"></div>",
"",
"<span id=\"n_label\"></span><div id=\"n_slider\"></div>",
"",
"<span id=\"xmax_label\"></span><div id=\"xmax_slider\"></div>",
"",
"<span id=\"npoints_label\"></span><div id=\"npoints_slider\"></div>",
"",
"<script type=\"text/javascript\">",
" var kernel = IPython.notebook.kernel;",
"",
" update_plot = function(msg_type, content){",
" // callback for updating the plot with the output of the request",
" if (msg_type !== 'display_data')",
" return;",
" var lines = content['data']['application/json'];",
" if (lines != undefined){",
" lines = JSON.parse(lines);",
" $.plot($(\"#theplot\"), lines);",
" } else {",
" console.log(\"no lines?!\");",
" console.log(data);",
" }",
" };",
" ",
" request_update = function(){",
" // execute update on the kernel",
" var n = $('div#n_slider').slider(\"value\");",
" $('span#n_label').text(\"n = \" + n);",
" ",
" var xmax = $('div#xmax_slider').slider(\"value\");",
" $('span#xmax_label').text(\"xmax = \" + xmax);",
" ",
" var npoints = $('div#npoints_slider').slider(\"value\");",
" $('span#npoints_label').text(\"npoints = \" + npoints);",
" ",
" var args = n + \", xmax=\" + xmax + \", npoints=\" + npoints;",
" kernel.execute(\"update_plot(\" + args + \")\", {'output': update_plot});",
" };",
" ",
" $('div#n_slider').slider({",
" min : 1,",
" max : 20,",
" value : 4,",
" slide : request_update,",
" change: request_update",
" });",
" $('div#xmax_slider').slider({",
" min : 1,",
" max : 32,",
" step : 0.2,",
" value : 10,",
" slide : request_update,",
" change: request_update",
" });",
" $('div#npoints_slider').slider({",
" min : 2,",
" max : 200,",
" step : 1,",
" value : 100,",
" slide : request_update,",
" change: request_update",
" });",
" request_update();",
"</script>",
""
]
},
{
"cell_type": "code",
"collapsed": true,
"input": [
""
],
"language": "python",
"outputs": []
}
]
}
]
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.