Skip to content

Instantly share code, notes, and snippets.

@btel
Created April 11, 2019 15:41
Show Gist options
  • Save btel/e271d8788f674536b9c077520452e344 to your computer and use it in GitHub Desktop.
Save btel/e271d8788f674536b9c077520452e344 to your computer and use it in GitHub Desktop.
draggable_table
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import bqplot.pyplot as plt\n",
"from ipywidgets import Label, GridspecLayout, DropBox, Layout\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def create_table(data):\n",
" n_cols = len(data)\n",
" n_rows = max(len(column) for column in data.values())\n",
" grid = GridspecLayout(n_rows+1, n_cols)\n",
" columnames = list(data.keys())\n",
" for i in range(n_cols):\n",
" column = columnames[i]\n",
" data_json = json.dumps(data[column])\n",
" grid[0, i] = Label(columnames[i], draggable=True)\n",
" grid[0, i].drag_data = {'data/app' : data_json}\n",
" for j in range(n_rows):\n",
" grid[j+1, i] = Label(str(data[column][j]), draggable=True)\n",
" grid[j+1, i].drag_data = {'data/app' : data_json}\n",
" return grid\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fc1341ee8299426e83479e8046602c1a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"GridspecLayout(children=(Label(value='col1', drag_data={'data/app': '[4, 2, 1]'}, draggable=True, layout=Layou…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plot_data = {'col1' : [4, 2, 1],\n",
" 'col2' : [1, 3 ,4],\n",
" 'col3' : [-1, 2, -3]}\n",
"create_table(plot_data)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import DropBox, Box\n",
"box = DropBox(layout=Layout(height='500px', width='800px'))\n",
"def box_ondrop(widget, data):\n",
" fig = plt.figure()\n",
" y = json.loads(data['data/app'])\n",
" plt.plot(y)\n",
" widget.children = (fig, )\n",
"box.on_drop(box_ondrop)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2da4134adaca4fe99a4f2f6624cbc406",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"GridspecLayout(children=(Label(value='col1', drag_data={'data/app': '[4, 2, 1]'}, draggable=True, layout=Layou…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plot_data = {'col1' : [4, 2, 1],\n",
" 'col2' : [1, 3 ,4],\n",
" 'col3' : [-1, 2, -3]}\n",
"create_table(plot_data)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8c35afa98a724505a884bae516f0d869",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"DropBox(layout=Layout(height='500px', width='800px'))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\n",
"box"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment