Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export current view #93

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions qgrid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def show_grid(data_frame, show_toolbar=None, remote_js=None, precision=None, gri
else:
display(grid)

#jchuahtacc: return the grid so client can call export_view, etc.
return grid


class QGridWidget(widgets.DOMWidget):
_view_module = Unicode("nbextensions/qgridjs/qgrid.widget", sync=True)
Expand All @@ -232,6 +235,7 @@ class QGridWidget(widgets.DOMWidget):
_cdn_base_url = Unicode(LOCAL_URL, sync=True)
_multi_index = Bool(False)
_selected_rows = List()
_export_callback = None

df = Instance(pd.DataFrame)
precision = Integer(6)
Expand Down Expand Up @@ -359,6 +363,10 @@ def _handle_qgrid_msg(self, widget, content, buffers=None):
elif content['type'] == 'selection_change':
self._selected_rows = content['rows']

elif content['type'] == 'export_data':
if self._export_callback:
self._export_callback(pd.DataFrame(content['data']))

def get_selected_rows(self):
"""Get the currently selected rows"""
return self._selected_rows
Expand All @@ -385,3 +393,15 @@ def export(self, value=None):

display_html(raw_html, raw=True)
display_javascript(raw_js, raw=True)

# jchuahtacc:
# export_view messages qgrid.widget.js to start returning visible rows
# when the export is complete, the callback will be invoked with
# either a dataframe containing the data, or None if an error occurred
def export_view(self, callback):
self._export_callback = callback
self.send({ 'type' : 'export_view' })

def export_all(self, callback):
self._export_callback = callback
self.send({ 'type' : 'export_all' })
25 changes: 25 additions & 0 deletions qgrid/qgridjs/qgrid.widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,34 @@ define([path], function(widget) {
} else if (msg.type === 'draw_table') {
this.drawTable();
this.updateSize();
}
// jchuahtacc: handle export_view message
else if (msg.type === 'export_view') {
this.exportView();
} else if (msg.type === 'export_all') {
this.exportView(true);
}
},

/**
* jchuahtacc: retrieve "included" items from grid.data_view and send them back as messages
*/
exportView: function(all) {
results = [ ];
for (var i in grid.row_data) {
var row = grid.row_data[i];
if (all || row.include) {
var data = { };
for (var j in grid.columns) {
var field = grid.columns[j].field;
data[field] = row[field];
}
results.push(data);
}
}
this.send({ 'type': 'export_data', 'data' : results });
},

/**
* Update the size of the dataframe.
*/
Expand Down
141 changes: 141 additions & 0 deletions qgrid_export_view_demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import qgrid\n",
"\n",
"df = pd.DataFrame(np.random.randn(6,4),\n",
" index=list('abcdef'),\n",
" columns=list('ABCD'))\n",
"\n",
"grid = qgrid.show_grid(df, show_toolbar=True)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"filtered = None\n",
"def callback(df):\n",
" global filtered\n",
" filtered = df\n",
" \n",
"grid.export_view(callback)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>A</th>\n",
" <th>B</th>\n",
" <th>C</th>\n",
" <th>D</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.47940</td>\n",
" <td>-0.94475</td>\n",
" <td>-1.20880</td>\n",
" <td>0.95058</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1.97422</td>\n",
" <td>1.40290</td>\n",
" <td>-1.40673</td>\n",
" <td>0.53463</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" A B C D\n",
"0 0.47940 -0.94475 -1.20880 0.95058\n",
"1 1.97422 1.40290 -1.40673 0.53463"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filtered"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
},
"widgets": {
"state": {
"4fb1b18b2a55454eb6302229de167090": {
"views": [
{
"cell_index": 0
}
]
},
"9c7bbc47a63a4201ae3a3312405a489b": {
"views": [
{
"cell_index": 0
}
]
},
"bce66d801410466eb2a74f557d1c1150": {
"views": [
{
"cell_index": 0
}
]
}
},
"version": "1.2.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}