Skip to content

Instantly share code, notes, and snippets.

@danvk
Created July 13, 2014 23:36
Show Gist options
  • Save danvk/28074b195a3178b29213 to your computer and use it in GitHub Desktop.
Save danvk/28074b195a3178b29213 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:0dc443944d01e70b187afc4d18c342b8e1edae8bd1e03b208ea017e08209bbf5"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pandas as pd\n",
"import numpy as np\n",
"from IPython.display import display,Javascript,HTML"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"df = pd.DataFrame(columns=['x', 'y', 'z'])\n",
"df.x = np.arange(100)\n",
"df.y = np.sin(df.x*2*np.pi/10)\n",
"df.z = np.log(np.abs(df.y))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 30
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dfj = df.to_json()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"html = \"\"\"\n",
"<script src=\"http://dygraphs.com/dygraph-combined.js\"></script>\n",
"<div id=\"dygraph\" style=\"width: 600px; height: 400px;\"></div>\n",
"\n",
"<script type=\"text/javascript\">\n",
"function convertToDataTable(d) {\n",
" var columns = _.keys(d);\n",
" columns.splice(columns.indexOf(\"x\"), 1);\n",
" var out = [];\n",
" for (var k in d['x']) {\n",
" var row = [d['x'][k]];\n",
" columns.forEach(function(col) {\n",
" row.push(d[col][k]);\n",
" });\n",
" out.push(row);\n",
" }\n",
" return {data:out, labels:['x'].concat(columns)};\n",
"}\n",
"\n",
"function handle_output(out) {\n",
" var json = out.content.data['text/plain'];\n",
" var data = JSON.parse(eval(json));\n",
" var tabular = convertToDataTable(data);\n",
" console.log(tabular)\n",
" g = new Dygraph(document.getElementById(\"dygraph\"), tabular.data, {\n",
" legend: 'always',\n",
" labels: tabular.labels\n",
" })\n",
"}\n",
"var kernel = IPython.notebook.kernel;\n",
"var callbacks = { 'iopub' : {'output' : handle_output}};\n",
"kernel.execute(\"dfj\", callbacks, {silent:false});\n",
"</script>\n",
"\"\"\"\n",
"\n",
"HTML(html)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
"<script src=\"http://dygraphs.com/dygraph-combined.js\"></script>\n",
"<div id=\"dygraph\" style=\"width: 600px; height: 400px;\"></div>\n",
"\n",
"<script type=\"text/javascript\">\n",
"function convertToDataTable(d) {\n",
" var columns = _.keys(d);\n",
" columns.splice(columns.indexOf(\"x\"), 1);\n",
" var out = [];\n",
" for (var k in d['x']) {\n",
" var row = [d['x'][k]];\n",
" columns.forEach(function(col) {\n",
" row.push(d[col][k]);\n",
" });\n",
" out.push(row);\n",
" }\n",
" return {data:out, labels:['x'].concat(columns)};\n",
"}\n",
"\n",
"function handle_output(out) {\n",
" var json = out.content.data['text/plain'];\n",
" var data = JSON.parse(eval(json));\n",
" var tabular = convertToDataTable(data);\n",
" console.log(tabular)\n",
" g = new Dygraph(document.getElementById(\"dygraph\"), tabular.data, {\n",
" legend: 'always',\n",
" labels: tabular.labels\n",
" })\n",
"}\n",
"var kernel = IPython.notebook.kernel;\n",
"var callbacks = { 'iopub' : {'output' : handle_output}};\n",
"kernel.execute(\"dfj\", callbacks, {silent:false});\n",
"</script>\n"
],
"metadata": {},
"output_type": "pyout",
"prompt_number": 32,
"text": [
"<IPython.core.display.HTML at 0x1133793d0>"
]
}
],
"prompt_number": 32
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment