Created
March 31, 2014 17:21
-
-
Save rdhyee/9897356 to your computer and use it in GitHub Desktop.
This notebook is a slight rewrite of [Cyrille Rossant](https://github.com/rossant)'s [Excel-like data grid editor for Pandas in the IPython notebook](http://nbviewer.ipython.org/gist/rossant/9463955) (which I learned about via Fernando Perez's [tweet](https://twitter.com/fperez_org/status/448636988858454016). I wanted to see whether I could use …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:1f5d4718f0d2a439a0be5894a1f3de0aa35fa8dba52a46ca58ac00a30781a83d" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Do we need to change the IPython profile?" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This notebook is a slight rewrite of [Cyrille Rossant](https://github.com/rossant)'s [Excel-like data grid editor for Pandas in the IPython notebook](http://nbviewer.ipython.org/gist/rossant/9463955) (which I learned about via Fernando Perez's [tweet](https://twitter.com/fperez_org/status/448636988858454016). I wanted to see whether I could use `requirejs` to load `handsontable` without needing to modify my IPython profile. The answer is yes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import IPython\n", | |
"import datetime\n", | |
"\n", | |
"IPython.version_info, datetime.datetime.utcnow()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Load handsontable to show one example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%javascript\n", | |
"\n", | |
"// load css if it's not already there: http://stackoverflow.com/a/4724676/7782\n", | |
"function loadcss(url) {\n", | |
" if (!$(\"link[href='\" + url + \"']\").length)\n", | |
" $('<link href=\"' + url + '\" rel=\"stylesheet\">').appendTo(\"head\");\n", | |
"}\n", | |
"\n", | |
"loadcss(\"http://handsontable.com/dist/jquery.handsontable.full.css\");\n", | |
"loadcss(\"http://handsontable.com/demo/css/samples.css?20140331\");" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%html\n", | |
"\n", | |
"<p style=\"font-size: 20px\"><strong>Handsontable</strong> is a minimalistic Excel-like <span class=\"nobreak\">data grid</span>\n", | |
" editor\n", | |
" for HTML, JavaScript & jQuery</p>\n", | |
"\n", | |
"<div id=\"hs_example\" class=\"handsontable\"></div>" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%javascript\n", | |
"\n", | |
"// https://raw.githubusercontent.com/warpech/jquery-handsontable/master/dist/jquery.handsontable.full.js\n", | |
"// http://handsontable.com/dist/jquery.handsontable.full.js\n", | |
"\n", | |
"require.config({\n", | |
" paths: {\n", | |
" handsontable: \"https://raw.githubusercontent.com/warpech/jquery-handsontable/master/dist/jquery.handsontable\"\n", | |
" }\n", | |
"});\n", | |
"\n", | |
"\n", | |
"require(['handsontable'], function (handsontable){\n", | |
" console.log(\"in require->handsontable\");\n", | |
" \n", | |
" var data = [\n", | |
" [\"\", \"Maserati\", \"Mazda\", \"Mercedes\", \"Mini\", \"Mitsubishi\"],\n", | |
" [\"2009\", 0, 2941, 4303, 354, 5814],\n", | |
" [\"2010\", 5, 2905, 2867, 412, 5284],\n", | |
" [\"2011\", 4, 2517, 4822, 552, 6127],\n", | |
" [\"2012\", 2, 2422, 5399, 776, 4151]\n", | |
" ];\n", | |
" \n", | |
" $('#hs_example').handsontable({\n", | |
" data: data,\n", | |
" minSpareRows: 1,\n", | |
" colHeaders: true,\n", | |
" contextMenu: true\n", | |
" });\n", | |
" \n", | |
" \n", | |
" function bindDumpButton() {\n", | |
" $('body').on('click', 'button[name=dump]', function () {\n", | |
" var dump = $(this).data('dump');\n", | |
" var $container = $(dump);\n", | |
" console.log('data of ' + dump, $container.handsontable('getData'));\n", | |
" });\n", | |
" }\n", | |
" bindDumpButton(); \n", | |
" \n", | |
"});" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Excel-like data grid editor for Pandas in the IPython notebook" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This proof-of-concept brings an **interactive Excel-like data grid editor** in the IPython notebook, compatible with Pandas' *DataFrame*. It means that whenever you have a *DataFrame* in the IPython notebook, you can edit it within an integrated GUI in the notebook, and the corresponding Python object will be automatically updated in real-time." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"This proof-of-concept uses the new widget machinery of IPython 2.0. You need the latest development version of IPython (or v2.0beta, or v2.0 when it's released within the next few weeks). You also need the [Handsontable](http://handsontable.com/) Javascript library. Other data grid editors could probably be used as well." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## How to do it..." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* Let's import a few functions and classes." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from __future__ import print_function # For py 2.7 compat\n", | |
"\n", | |
"from IPython.html import widgets # Widget definitions\n", | |
"from IPython.display import display # Used to display widgets in the notebook\n", | |
"from IPython.utils.traitlets import Unicode # Used to declare attributes of our widget" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* We create a new widget. The `value` trait will contain the JSON representation of the entire table. This trait will be synchronized between Python and Javascript by IPython 2.0's widget machinery." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class HandsonTableWidget(widgets.DOMWidget):\n", | |
" _view_name = Unicode('HandsonTableView', sync=True)\n", | |
" value = Unicode(sync=True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* Now we write the Javascript code for the widget. There is a tiny bit of boilerplate code, but have a look at the three important functions that are responsible for the synchronization:\n", | |
"\n", | |
" * `render` for the widget initialization\n", | |
" * `update` for Python --> JS update\n", | |
" * `handle_table_change` for JS --> Python update\n", | |
"\n", | |
"This is a bit oversimplified, of course. You will find more information on this [tutorial](http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/widgets/Part%206%20-%20Custom%20Widget.ipynb)." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%%javascript\n", | |
"var table_id = 0;\n", | |
"\n", | |
"\n", | |
"require([\"widgets/js/widget\"], function(WidgetManager){ \n", | |
" // Define the HandsonTableView\n", | |
" var HandsonTableView = IPython.DOMWidgetView.extend({\n", | |
" \n", | |
" render: function(){\n", | |
" // CREATION OF THE WIDGET IN THE NOTEBOOK.\n", | |
" \n", | |
" // Add a <div> in the widget area.\n", | |
" this.$table = $('<div />')\n", | |
" .attr('id', 'table_' + (table_id++))\n", | |
" .appendTo(this.$el);\n", | |
" // Create the Handsontable table.\n", | |
" this.$table.handsontable({\n", | |
" });\n", | |
" \n", | |
" },\n", | |
" \n", | |
" update: function() {\n", | |
" // PYTHON --> JS UPDATE.\n", | |
" \n", | |
" // Get the model's JSON string, and parse it.\n", | |
" var data = $.parseJSON(this.model.get('value'));\n", | |
" // Give it to the Handsontable widget.\n", | |
" this.$table.handsontable({data: data});\n", | |
" \n", | |
" // Don't touch this...\n", | |
" return HandsonTableView.__super__.update.apply(this);\n", | |
" },\n", | |
" \n", | |
" // Tell Backbone to listen to the change event of input controls.\n", | |
" events: {\"change\": \"handle_table_change\"},\n", | |
" \n", | |
" handle_table_change: function(event) {\n", | |
" // JS --> PYTHON UPDATE.\n", | |
" \n", | |
" // Get the table instance.\n", | |
" var ht = this.$table.handsontable('getInstance');\n", | |
" // Get the data, and serialize it in JSON.\n", | |
" var json = JSON.stringify(ht.getData());\n", | |
" // Update the model with the JSON string.\n", | |
" this.model.set('value', json);\n", | |
" \n", | |
" // Don't touch this...\n", | |
" this.touch();\n", | |
" },\n", | |
" });\n", | |
" \n", | |
" // Register the HandsonTableView with the widget manager.\n", | |
" WidgetManager.register_widget_view('HandsonTableView', HandsonTableView);\n", | |
"});" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* Now, we have a synchronized table widget that we can already use. But we'd like to integrate it with Pandas. To do this, we create a light wrapper around a `DataFrame` instance. We create two callback functions for synchronizing the Pandas object with the IPython widget. Changes in the GUI will automatically trigger a change in the `DataFrame`, but the converse is not true. You'll need to re-display the widget if you change the `DataFrame` in Python." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import StringIO\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"\n", | |
"class HandsonDataFrame(object):\n", | |
" def __init__(self, df):\n", | |
" self._df = df\n", | |
" self._widget = HandsonTableWidget()\n", | |
" self._widget.on_trait_change(self._on_data_changed, 'value')\n", | |
" self._widget.on_displayed(self._on_displayed)\n", | |
" \n", | |
" def _on_displayed(self, e):\n", | |
" # DataFrame ==> Widget (upon initialization only)\n", | |
" json = self._df.to_json(orient='values')\n", | |
" self._widget.value = json\n", | |
" \n", | |
" def _on_data_changed(self, e, val):\n", | |
" # Widget ==> DataFrame (called every time the user\n", | |
" # changes a value in the graphical widget)\n", | |
" buf = StringIO.StringIO(val)\n", | |
" self._df = pd.read_json(buf, orient='values')\n", | |
" \n", | |
" def to_dataframe(self):\n", | |
" return self._df\n", | |
" \n", | |
" def show(self):\n", | |
" display(self._widget)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* Now, let's test all that! We first create a random `DataFrame`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"data = np.random.randint(size=(3, 5), low=100, high=900)\n", | |
"df = pd.DataFrame(data)\n", | |
"df" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"* We wrap it in a `HandsonDataFrame` and show it." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ht = HandsonDataFrame(df)\n", | |
"ht.show()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"We can now *change* the values interactively, and they will be changed in Python automaticall." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"ht.to_dataframe()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"There are many ways this proof-of-concept could be improved.\n", | |
"\n", | |
"* Synchronize only deltas instead of synchronizing the whole array every time (i.e. the method here would be slow on large tables).\n", | |
"* Also, avoid recreating a new `DataFrame` at very change, but update the same `DataFrame` instance in-place.\n", | |
"* Support for named columns.\n", | |
"* Hide the wrapper, i.e. make it so that the default rich representation of the `DataFrame` in the notebook is the `HandsonDataFrame`.\n", | |
"* Implement everything in an easy-to-use extension.\n", | |
"* etc." | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment