Skip to content

Instantly share code, notes, and snippets.

@lbustelo
Created May 20, 2016 14:18
Show Gist options
  • Save lbustelo/a279fdd4b0b999f499da7642e3a011eb to your computer and use it in GitHub Desktop.
Save lbustelo/a279fdd4b0b999f499da7642e3a011eb to your computer and use it in GitHub Desktop.
declarativewidgets/issues/366
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# DeclarativeWidgets DataFrame Sync Test"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"application/javascript": [
"\n",
" window.Urth = window.Urth || {};\n",
" Urth._initialized = Urth._initialized || $.Deferred();\n",
" Urth.whenReady = Urth.whenReady || function(cb) {\n",
" Urth._initialized.then(function() {\n",
" Urth.whenReady(cb);\n",
" });\n",
" };\n",
" Urth.whenReady(function() { console.log(\"Declarative widgets connected.\") });\n",
" "
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from urth import widgets\n",
"\n",
"widgets.init()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"INSTALLED VERSIONS\n",
"------------------\n",
"commit: None\n",
"python: 3.5.1.final.0\n",
"python-bits: 64\n",
"OS: Linux\n",
"OS-release: 4.1.12-boot2docker\n",
"machine: x86_64\n",
"processor: \n",
"byteorder: little\n",
"LC_ALL: en_US.UTF-8\n",
"LANG: en_US.UTF-8\n",
"\n",
"pandas: 0.17.1\n",
"nose: None\n",
"pip: 8.1.1\n",
"setuptools: 20.3\n",
"Cython: None\n",
"numpy: 1.10.4\n",
"scipy: 0.17.0\n",
"statsmodels: None\n",
"IPython: 4.1.2\n",
"sphinx: None\n",
"patsy: None\n",
"dateutil: 2.5.2\n",
"pytz: 2016.3\n",
"blosc: None\n",
"bottleneck: None\n",
"tables: None\n",
"numexpr: 2.5.2\n",
"matplotlib: 1.5.1\n",
"openpyxl: None\n",
"xlrd: None\n",
"xlwt: None\n",
"xlsxwriter: None\n",
"lxml: None\n",
"bs4: None\n",
"html5lib: None\n",
"httplib2: None\n",
"apiclient: None\n",
"sqlalchemy: 1.0.12\n",
"pymysql: None\n",
"psycopg2: None\n",
"Jinja2: None\n"
]
}
],
"source": [
"from IPython.core.display import HTML\n",
"import pandas as pd\n",
"pd.show_versions()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<link rel=\"import\" href=\"urth_components/urth-viz-table/urth-viz-table.html\" is=\"urth-core-import\">"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
"<link rel=\"import\" href=\"urth_components/urth-viz-table/urth-viz-table.html\" is=\"urth-core-import\">"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"df1 = pd.DataFrame([[1,2],[3,4]], columns=['a','b'])\n",
"df1.columns.name = 'df1'\n",
"df2 = pd.DataFrame([[5,6],[7,8]], columns=['c','d'])\n",
"df2.columns.name = 'df2'\n",
"df3 = pd.DataFrame([[9,10],[11,12]], columns=['e','f'])\n",
"df3.columns.name = 'df3'\n",
"df2_orig = df2.copy()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>df1</th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1</td>\n",
" <td>2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>3</td>\n",
" <td>4</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"df1 a b\n",
"0 1 2\n",
"1 3 4"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df1"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>df2</th>\n",
" <th>c</th>\n",
" <th>d</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>5</td>\n",
" <td>6</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>7</td>\n",
" <td>8</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"df2 c d\n",
"0 5 6\n",
"1 7 8"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>df3</th>\n",
" <th>e</th>\n",
" <th>f</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>9</td>\n",
" <td>10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>11</td>\n",
" <td>12</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"df3 e f\n",
"0 9 10\n",
"1 11 12"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df3"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Notice how I'm returning the dataframe\n",
"def get_row(idx:int):\n",
" print(idx)\n",
" if idx > 1:\n",
" print('swap')\n",
" return df3\n",
" else:\n",
" return df2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Using the channel to set a default for df2or3 (Optional depending if you want the 2nd table to display something on init)\n",
"from urth.widgets.widget_channels import channel\n",
"channel().set(\"df2or3\", df2)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<template is=\"urth-core-bind\">\n",
" <urth-core-function ref=\"get_row\" arg-idx=\"[[sel1.0]]\" result=\"{{df2or3}}\" auto>\n",
" </urth-core-function>\n",
" <urth-core-dataframe id='df1' ref=\"df1\" value=\"{{df1}}\" auto></urth-core-dataframe>\n",
" <urth-viz-table datarows=\"{{df1.data}}\" columns=\"{{df1.columns}}\" selection=\"{{sel1}}\" rows-visible=6>\n",
" </urth-viz-table>\n",
"</template>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"swap\n",
"1\n"
]
}
],
"source": [
"%%html\n",
"<template is=\"urth-core-bind\">\n",
" <urth-core-function ref=\"get_row\" arg-idx=\"[[sel1.0]]\" result=\"{{df2or3}}\" auto>\n",
" </urth-core-function>\n",
" <urth-core-dataframe id='df1' ref=\"df1\" value=\"{{df1}}\" auto></urth-core-dataframe>\n",
" <urth-viz-table datarows=\"{{df1.data}}\" columns=\"{{df1.columns}}\" selection=\"{{sel1}}\" rows-visible=6>\n",
" </urth-viz-table>\n",
"</template>"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<template is=\"urth-core-bind\">\n",
" <urth-viz-table datarows=\"{{df2or3.data}}\" columns=\"{{df2or3.columns}}\" selection=\"{{sel2}}\" rows-visible=6>\n",
" </urth-viz-table>\n",
"</template>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%html\n",
"<template is=\"urth-core-bind\">\n",
" <urth-viz-table datarows=\"{{df2or3.data}}\" columns=\"{{df2or3.columns}}\" selection=\"{{sel2}}\" rows-visible=6>\n",
" </urth-viz-table>\n",
"</template>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment