Skip to content

Instantly share code, notes, and snippets.

@ellisonbg
Created October 31, 2015 19:06
Show Gist options
  • Save ellisonbg/5c54f995bfcd64b90dc2 to your computer and use it in GitHub Desktop.
Save ellisonbg/5c54f995bfcd64b90dc2 to your computer and use it in GitHub Desktop.
QGridWidget.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# QGrid Widget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows how to automatically display a Pandas `DataFrame` or `Series` using qgrid (https://github.com/quantopian/qgrid). This requires master of qgrid and ipywidgets."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"import seaborn.apionly as sns\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load data"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"df = sns.load_dataset('titanic')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"df = df.dropna()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>survived</th>\n",
" <th>pclass</th>\n",
" <th>sex</th>\n",
" <th>age</th>\n",
" <th>sibsp</th>\n",
" <th>parch</th>\n",
" <th>fare</th>\n",
" <th>embarked</th>\n",
" <th>class</th>\n",
" <th>who</th>\n",
" <th>adult_male</th>\n",
" <th>deck</th>\n",
" <th>embark_town</th>\n",
" <th>alive</th>\n",
" <th>alone</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>38</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>71.2833</td>\n",
" <td>C</td>\n",
" <td>First</td>\n",
" <td>woman</td>\n",
" <td>False</td>\n",
" <td>C</td>\n",
" <td>Cherbourg</td>\n",
" <td>yes</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>35</td>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>53.1000</td>\n",
" <td>S</td>\n",
" <td>First</td>\n",
" <td>woman</td>\n",
" <td>False</td>\n",
" <td>C</td>\n",
" <td>Southampton</td>\n",
" <td>yes</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>male</td>\n",
" <td>54</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>51.8625</td>\n",
" <td>S</td>\n",
" <td>First</td>\n",
" <td>man</td>\n",
" <td>True</td>\n",
" <td>E</td>\n",
" <td>Southampton</td>\n",
" <td>no</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1</td>\n",
" <td>3</td>\n",
" <td>female</td>\n",
" <td>4</td>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>16.7000</td>\n",
" <td>S</td>\n",
" <td>Third</td>\n",
" <td>child</td>\n",
" <td>False</td>\n",
" <td>G</td>\n",
" <td>Southampton</td>\n",
" <td>yes</td>\n",
" <td>False</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>1</td>\n",
" <td>1</td>\n",
" <td>female</td>\n",
" <td>58</td>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>26.5500</td>\n",
" <td>S</td>\n",
" <td>First</td>\n",
" <td>woman</td>\n",
" <td>False</td>\n",
" <td>C</td>\n",
" <td>Southampton</td>\n",
" <td>yes</td>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" survived pclass sex age sibsp parch fare embarked class \\\n",
"1 1 1 female 38 1 0 71.2833 C First \n",
"3 1 1 female 35 1 0 53.1000 S First \n",
"6 0 1 male 54 0 0 51.8625 S First \n",
"10 1 3 female 4 1 1 16.7000 S Third \n",
"11 1 1 female 58 0 0 26.5500 S First \n",
"\n",
" who adult_male deck embark_town alive alone \n",
"1 woman False C Cherbourg yes False \n",
"3 woman False C Southampton yes False \n",
"6 man True E Southampton no True \n",
"10 child False G Southampton yes False \n",
"11 woman False C Southampton yes True "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## QGrid initialization"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import qgrid\n",
"qgrid.nbinstall(overwrite=True)\n",
"qgrid.set_defaults(remote_js=True, precision=4)\n",
"from qgrid.grid import QGridWidget, defaults\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def get_qgrid_widget(df):\n",
" remote_js = defaults.remote_js\n",
" precision = defaults.precision\n",
" grid_options = defaults.grid_options\n",
" return QGridWidget(df=df,\n",
" precision=precision,\n",
" grid_options=json.dumps(grid_options),\n",
" remote_js=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Widget"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true,
"scrolled": true
},
"outputs": [],
"source": [
"import ipywidgets as w\n",
"from IPython.display import display, HTML"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a function that will display a `DataFrame` or `Series` as a `QGridWidget`:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def display_dataframe(data):\n",
" if isinstance(data, pd.Series):\n",
" df = pd.DataFrame(data)\n",
" else:\n",
" df = data\n",
" display(get_qgrid_widget(df))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now register that as a IPython display formatter:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ip = get_ipython()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"ip.display_formatter.ipython_display_formatter.for_type_by_name('pandas.core.frame',\n",
" 'DataFrame', \n",
" display_dataframe)\n",
"ip.display_formatter.ipython_display_formatter.for_type_by_name('pandas.core.series', \n",
" 'Series', \n",
" display_dataframe)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now `DataFrame` and `Series` will automatically be displayed as a nice `QGridWidget`:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"s = df['sex']"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment