-
-
Save danielballan/63c98b8df34a418774f2 to your computer and use it in GitHub Desktop.
This file contains 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": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
":0: FutureWarning: IPython widgets are experimental and may change in the future.\n" | |
] | |
} | |
], | |
"source": [ | |
"import time\n", | |
"import IPython.html.widgets\n", | |
"\n", | |
"\n", | |
"class WidgetHistory:\n", | |
" \"\"\"\n", | |
" With a button click, save the state of all other widgets to a file.\n", | |
" Review past states using a slider.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" history : history.History\n", | |
" a dictionary-like object that retains the history of each key,\n", | |
" backed by a sqlite db\n", | |
" widgets : list, optional\n", | |
" list of widgets to capture; if None, use all Widget instances\n", | |
" \"\"\"\n", | |
" def __init__(self, history, widgets=None):\n", | |
" self._history = history\n", | |
" self._widgets = widgets\n", | |
" self._state_key = 'WIDGET_STATE'\n", | |
" \n", | |
" # History slider\n", | |
" self.slider = IPython.html.widgets.IntSlider()\n", | |
" self.slider.max = 0\n", | |
" self.slider.min = 0\n", | |
" self.slider.description = 'history'\n", | |
" self.slider.on_trait_change(self.load_state, 'value')\n", | |
" self._locate_beginning()\n", | |
"\n", | |
" # Save button\n", | |
" def save_callback(_):\n", | |
" return self.save_all_widgets()\n", | |
" \n", | |
" self.save_button = IPython.html.widgets.Button()\n", | |
" self.save_button.on_click(save_callback)\n", | |
" self.save_button.description = 'Save State'\n", | |
" \n", | |
" # Playback button\n", | |
" def play_callback(_):\n", | |
" return self.playback(1)\n", | |
" \n", | |
" self.play_button = IPython.html.widgets.Button()\n", | |
" self.play_button.on_click(play_callback)\n", | |
" self.play_button.description = 'Playback'\n", | |
"\n", | |
" # boxed group of widgets\n", | |
" self.box = IPython.html.widgets.HBox([self.slider, self.save_button, self.play_button])\n", | |
" \n", | |
" @property\n", | |
" def widgets(self):\n", | |
" if self._widgets is not None:\n", | |
" return self._widgets\n", | |
" else:\n", | |
" return IPython.html.widgets.Widget.__dict__['widgets']\n", | |
" \n", | |
" @widgets.setter\n", | |
" def widgets(self, val):\n", | |
" self._widgets = val\n", | |
" \n", | |
" def load_state(self, _):\n", | |
" state = self._history.past(self._state_key, -self.slider.value)\n", | |
" for model_id, widget in list(self.widgets.items()):\n", | |
" if model_id == self.slider.model_id:\n", | |
" # Don't manipulate the history widget itself, of course.\n", | |
" continue\n", | |
" if model_id not in state:\n", | |
" # No state has been saved for this widget at this point in\n", | |
" # history.\n", | |
" continue\n", | |
" for key, value in state[model_id].items():\n", | |
" setattr(widget, key, value)\n", | |
"\n", | |
" def save_all_widgets(self):\n", | |
" state = {}\n", | |
" for key, w in self.widgets.items():\n", | |
" state[key] = w.get_state()\n", | |
" if key == self.slider.model_id:\n", | |
" continue\n", | |
" self._history[self._state_key] = state\n", | |
" self._locate_beginning()\n", | |
" self.slider.value = 0\n", | |
" \n", | |
" def playback(self, delay, step=1):\n", | |
" while not (self.slider.value > -1):\n", | |
" self.slider.value += step\n", | |
" time.sleep(delay)\n", | |
" \n", | |
" def _locate_beginning(self):\n", | |
" while True:\n", | |
" try:\n", | |
" self._history.past(self._state_key, -self.slider.min + 1)\n", | |
" except ValueError:\n", | |
" break\n", | |
" except KeyError:\n", | |
" # no value self._state_key\n", | |
" break\n", | |
" else:\n", | |
" self.slider.min -= 1\n", | |
" self.slider.disabled = self.slider.min == 0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import history # github.com/Nikea/history\n", | |
"from IPython.display import display\n", | |
"from IPython.html.widgets import IntSlider\n", | |
"\n", | |
"h = history.History('my_widget_history.db')\n", | |
"wsh = WidgetHistory(h)\n", | |
"wsh.box" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"display(IntSlider(description='example 1', min=0, max=20, model_id='example1'))\n", | |
"display(IntSlider(description='example 2', min=0, max=20, model_id='example2'))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "a" | |
}, | |
"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.4.3" | |
}, | |
"widgets_state": { | |
"18804a3c333f410f9e4cdd6b67661968": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_view_module": "", | |
"_view_name": "FlexBoxView", | |
"align": "start", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"box_style": "", | |
"children": [ | |
"IPY_MODEL_3e632e91bfd94fb9b751d1095d8c8e32", | |
"IPY_MODEL_f8e617169bc844cba1b5cdffeeac92c0", | |
"IPY_MODEL_590fff8075d7496b8967dc26292fd03f" | |
], | |
"color": "", | |
"flex": 0, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"margin": "", | |
"msg_throttle": 3, | |
"orientation": "horizontal", | |
"overflow_x": "", | |
"overflow_y": "", | |
"pack": "start", | |
"padding": "", | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [ | |
1 | |
] | |
}, | |
"3e632e91bfd94fb9b751d1095d8c8e32": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_range": false, | |
"_view_module": "", | |
"_view_name": "IntSliderView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"color": "", | |
"description": "history", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"margin": "", | |
"max": 0, | |
"min": -21, | |
"msg_throttle": 3, | |
"orientation": "horizontal", | |
"padding": "", | |
"readout": true, | |
"slider_color": "", | |
"step": 1, | |
"value": 0, | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [] | |
}, | |
"590fff8075d7496b8967dc26292fd03f": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_view_module": "", | |
"_view_name": "ButtonView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"button_style": "", | |
"color": "", | |
"description": "Playback", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"icon": "", | |
"margin": "", | |
"msg_throttle": 3, | |
"padding": "", | |
"tooltip": "", | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [] | |
}, | |
"5e503da58c6644079f56cf9884e804e9": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_range": false, | |
"_view_module": "", | |
"_view_name": "IntSliderView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"color": "", | |
"description": "example", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"margin": "", | |
"max": 20, | |
"min": 0, | |
"msg_throttle": 3, | |
"orientation": "horizontal", | |
"padding": "", | |
"readout": true, | |
"slider_color": "", | |
"step": 1, | |
"value": 0, | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [] | |
}, | |
"example1": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_range": false, | |
"_view_module": "", | |
"_view_name": "IntSliderView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"color": "", | |
"description": "example 1", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"margin": "", | |
"max": 20, | |
"min": 0, | |
"msg_throttle": 3, | |
"orientation": "horizontal", | |
"padding": "", | |
"readout": true, | |
"slider_color": "", | |
"step": 1, | |
"value": 15, | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [ | |
2 | |
] | |
}, | |
"example2": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_range": false, | |
"_view_module": "", | |
"_view_name": "IntSliderView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"color": "", | |
"description": "example 2", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"margin": "", | |
"max": 20, | |
"min": 0, | |
"msg_throttle": 3, | |
"orientation": "horizontal", | |
"padding": "", | |
"readout": true, | |
"slider_color": "", | |
"step": 1, | |
"value": 17, | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [ | |
2 | |
] | |
}, | |
"f8e617169bc844cba1b5cdffeeac92c0": { | |
"model_module": null, | |
"model_name": "WidgetModel", | |
"state": { | |
"_css": [], | |
"_dom_classes": [], | |
"_view_module": "", | |
"_view_name": "ButtonView", | |
"background_color": "", | |
"border_color": "", | |
"border_radius": "", | |
"border_style": "", | |
"border_width": "", | |
"button_style": "", | |
"color": "", | |
"description": "Save State", | |
"disabled": false, | |
"font_family": "", | |
"font_size": "", | |
"font_style": "", | |
"font_weight": "", | |
"height": "", | |
"icon": "", | |
"margin": "", | |
"msg_throttle": 3, | |
"padding": "", | |
"tooltip": "", | |
"version": 0, | |
"visible": true, | |
"width": "" | |
}, | |
"views": [] | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment