Created
December 26, 2018 18:47
-
-
Save xavArtley/3f5b0afe31bed7b1eb76a3846900aab4 to your computer and use it in GitHub Desktop.
Example of possible toggles behavior in panel
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# This notebook is used to develop, test and explain how toggle widgets work" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Imports" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import panel as pn\n", | |
"pn.extension()\n", | |
"\n", | |
"from panel.widgets import Select\n", | |
"from collections import OrderedDict\n", | |
"from panel.util import hashable\n", | |
"\n", | |
"from bokeh.models.widgets import (RadioButtonGroup as _BkRadioButtonGroup,\n", | |
" RadioGroup as _BkRadioBoxGroup,\n", | |
" CheckboxButtonGroup as _BkCheckButtonGroup,\n", | |
" CheckboxGroup as _BkCheckboxGroup)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Radio Groups Toggle" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Code " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class AbstractRadioGroup(Select):\n", | |
" \n", | |
" def _process_param_change(self, msg):\n", | |
" msg = super(Select, self)._process_param_change(msg)\n", | |
" mapping = OrderedDict([(hashable(v), k) for k, v in self.options.items()])\n", | |
" if msg.get('value') is not None:\n", | |
" msg['active'] = list(mapping).index(msg.pop('value'))\n", | |
" if 'options' in msg:\n", | |
" msg['labels'] = list(msg.pop('options'))\n", | |
" msg.pop('title', None)\n", | |
" return msg\n", | |
"\n", | |
" def _process_property_change(self, msg):\n", | |
" msg = super(Select, self)._process_property_change(msg)\n", | |
" if 'active' in msg:\n", | |
" msg['value'] = list(self.options.values())[msg.pop('active')]\n", | |
" return msg\n", | |
"\n", | |
"class RadioButtonGroup(AbstractRadioGroup):\n", | |
" _widget_type = _BkRadioButtonGroup\n", | |
"\n", | |
"class RadioBoxGroup(AbstractRadioGroup):\n", | |
" _widget_type = _BkRadioBoxGroup" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"radio_buttons = RadioButtonGroup(options=[1,2,3], value=3)\n", | |
"radio_box = RadioBoxGroup(options=radio_buttons.options, value=radio_buttons.value)\n", | |
"radio_buttons.link(radio_box, value='value')\n", | |
"radio_box.link(radio_buttons, value='value')\n", | |
"pn.Row(radio_buttons, radio_box)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Check Groups Toggle" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Implementation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class AbstractCheckGroup(Select):\n", | |
"\n", | |
" value = param.List(default=[])\n", | |
"\n", | |
" def _process_param_change(self, msg):\n", | |
" msg = super(Select, self)._process_param_change(msg)\n", | |
" mapping = OrderedDict([(hashable(v), k) for k, v in self.options.items()])\n", | |
" if msg.get('value') is not None:\n", | |
" msg['active'] = [list(mapping).index(v) for v in msg.pop('value')]\n", | |
" if 'options' in msg:\n", | |
" msg['labels'] = list(msg.pop('options'))\n", | |
" msg.pop('title', None)\n", | |
" return msg\n", | |
"\n", | |
" def _process_property_change(self, msg):\n", | |
" msg = super(Select, self)._process_property_change(msg)\n", | |
" if 'active' in msg:\n", | |
" msg['value'] = [list(self.options.values())[a] for a in msg.pop('active')]\n", | |
" return msg\n", | |
"\n", | |
"class CheckButtonGroup(AbstractCheckGroup):\n", | |
"\n", | |
" _widget_type = _BkCheckButtonGroup\n", | |
" \n", | |
"class CheckBoxGroup(AbstractCheckGroup):\n", | |
"\n", | |
" _widget_type = _BkCheckboxGroup" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"check_buttons = CheckButtonGroup(options=[1,2,3], value=[3])\n", | |
"check_boxes = CheckBoxGroup(options=check_buttons.options, value=check_buttons.value)\n", | |
"check_buttons.link(check_boxes,value='value')\n", | |
"check_boxes.link(check_buttons,value='value')\n", | |
"pn.Row(check_buttons, check_boxes)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### ToggleGroup Factory" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from functools import wraps\n", | |
"\n", | |
"class ToggleGroup:\n", | |
" _widgets_type = ['button', 'box']\n", | |
" __doc__ = \"\"\" doc on widget type and check value type \"\"\" + Select.__doc__\n", | |
"\n", | |
" def __new__(cls, widget_type='button', **params):\n", | |
" if widget_type not in ToggleGroup._widgets_type:\n", | |
" raise ValueError('widget_type {} is not valid. Valid options are {}'.format(widget_type, ToggleGroup.widget_type))\n", | |
" \n", | |
" if type(params.get('value',[])) is list:\n", | |
" if widget_type == 'button':\n", | |
" return CheckButtonGroup(**params)\n", | |
" else:\n", | |
" return CheckBoxGroup(**params)\n", | |
" else:\n", | |
" if widget_type == 'button':\n", | |
" return RadioButtonGroup(**params)\n", | |
" else:\n", | |
" return RadioBoxGroup(**params)\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"options = [1,2,3]\n", | |
"\n", | |
"pn.Column(\n", | |
" ToggleGroup(options = [1,2,3]),\n", | |
" pn.Row(\n", | |
" ToggleGroup(options=options, value=1, widget_type='button'),\n", | |
" ToggleGroup(options=options, value=1, widget_type='box'),\n", | |
" ),\n", | |
" pn.Row(\n", | |
" ToggleGroup(options=options, value=[1,2], widget_type='button'),\n", | |
" ToggleGroup(options=options, value=[1,2], widget_type='box')\n", | |
" )\n", | |
")" | |
] | |
}, | |
{ | |
"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.6.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment