Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# WidgetHistory\n",
  8. "\n",
  9. "A \"Save State\" button captures the state of every widget on the page. One master widget can be used to update all the other widgets, scrolling through the history of saved states.\n",
  10. "\n",
  11. "The states are persisted in a sqlite file using `historydict`."
  12. ]
  13. },
  14. {
  15. "cell_type": "code",
  16. "execution_count": 1,
  17. "metadata": {
  18. "collapsed": false
  19. },
  20. "outputs": [
  21. {
  22. "name": "stderr",
  23. "output_type": "stream",
  24. "text": [
  25. "/Users/dallan/miniconda/envs/py3/lib/python3.4/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.\n",
  26. " \"`IPython.html.widgets` has moved to `ipywidgets`.\", ShimWarning)\n"
  27. ]
  28. }
  29. ],
  30. "source": [
  31. "import uuid\n",
  32. "import time\n",
  33. "import IPython.html.widgets\n",
  34. "from IPython.display import display\n",
  35. "\n",
  36. "class WidgetHistory:\n",
  37. " \"\"\"\n",
  38. " With a button click, save the state of all other widgets to a file.\n",
  39. " Review past states using a slider.\n",
  40. " \n",
  41. " Parameters\n",
  42. " ----------\n",
  43. " history : history.History\n",
  44. " a dict-like interface to a history file that retains old values\n",
  45. " widgets : list, optional\n",
  46. " list of widgets to capture; if None, use all Widget instances\n",
  47. " \"\"\"\n",
  48. " def __init__(self, history, widgets=None):\n",
  49. " self._history = history\n",
  50. " self._widgets = widgets\n",
  51. " self._state_key = 'WIDGET_STATE'\n",
  52. " \n",
  53. " # History slider\n",
  54. " self.slider = IPython.html.widgets.IntSlider()\n",
  55. " self.slider.max = 0\n",
  56. " self.slider.min = 0\n",
  57. " self.slider.description = 'history'\n",
  58. " self.slider.on_trait_change(self.load_state, 'value')\n",
  59. " self._locate_beginning()\n",
  60. "\n",
  61. " # Save button\n",
  62. " def save_callback(_):\n",
  63. " return self.save_all_widgets()\n",
  64. " \n",
  65. " self.save_button = IPython.html.widgets.Button()\n",
  66. " self.save_button.on_click(save_callback)\n",
  67. " self.save_button.description = 'Save State'\n",
  68. " \n",
  69. " # Playback button\n",
  70. " def play_callback(_):\n",
  71. " return self.playback(1)\n",
  72. " \n",
  73. " self.play_button = IPython.html.widgets.Button()\n",
  74. " self.play_button.on_click(play_callback)\n",
  75. " self.play_button.description = 'Playback'\n",
  76. "\n",
  77. " # boxed group of widgets\n",
  78. " self.box = IPython.html.widgets.HBox([self.slider, self.save_button, self.play_button])\n",
  79. " \n",
  80. " @property\n",
  81. " def widgets(self):\n",
  82. " if self._widgets is not None:\n",
  83. " return self._widgets\n",
  84. " else:\n",
  85. " return IPython.html.widgets.Widget.__dict__['widgets']\n",
  86. " \n",
  87. " @widgets.setter\n",
  88. " def widgets(self, val):\n",
  89. " self._widgets = val\n",
  90. " \n",
  91. " def load_state(self, _):\n",
  92. " state = self._history.past(self._state_key, -self.slider.value)\n",
  93. " for model_id, widget in list(self.widgets.items()):\n",
  94. " if model_id == self.slider.model_id:\n",
  95. " # Don't manipulate the history widget itself, of course.\n",
  96. " continue\n",
  97. " if model_id not in state:\n",
  98. " # No state has been saved for this widget at this point in\n",
  99. " # history.\n",
  100. " continue\n",
  101. " for key, value in state[model_id].items():\n",
  102. " setattr(widget, key, value)\n",
  103. "\n",
  104. " def save_all_widgets(self):\n",
  105. " state = {}\n",
  106. " for key, w in self.widgets.items():\n",
  107. " state[key] = w.get_state()\n",
  108. " if key == self.slider.model_id:\n",
  109. " continue\n",
  110. " self._history[self._state_key] = state\n",
  111. " self._locate_beginning()\n",
  112. " self.slider.value = 0\n",
  113. " \n",
  114. " def playback(self, delay, step=1):\n",
  115. " while not (self.slider.value > -1):\n",
  116. " self.slider.value += step\n",
  117. " time.sleep(delay)\n",
  118. " \n",
  119. " def _locate_beginning(self):\n",
  120. " while True:\n",
  121. " try:\n",
  122. " self._history.past(self._state_key, -self.slider.min + 1)\n",
  123. " except ValueError:\n",
  124. " break\n",
  125. " except KeyError:\n",
  126. " # no value self._state_key\n",
  127. " break\n",
  128. " else:\n",
  129. " self.slider.min -= 1\n",
  130. " self.slider.disabled = self.slider.min == 0\n",
  131. " \n",
  132. " def _repr_html_(self):\n",
  133. " return display(self.box)"
  134. ]
  135. },
  136. {
  137. "cell_type": "markdown",
  138. "metadata": {},
  139. "source": [
  140. "## Instructions\n",
  141. "\n",
  142. "Manipulate the two \"example\" sliders below. Click \"Save State\" and move them again. Repeat a couple times. Then use the \"history\" slider to review the history of saved states.\n",
  143. "\n",
  144. "The \"Playback\" button walks forward through history."
  145. ]
  146. },
  147. {
  148. "cell_type": "code",
  149. "execution_count": 2,
  150. "metadata": {
  151. "collapsed": false,
  152. "scrolled": false
  153. },
  154. "outputs": [
  155. {
  156. "data": {
  157. "text/plain": [
  158. "<__main__.WidgetHistory at 0x104e27630>"
  159. ]
  160. },
  161. "execution_count": 2,
  162. "metadata": {},
  163. "output_type": "execute_result"
  164. }
  165. ],
  166. "source": [
  167. "import historydict\n",
  168. "from IPython.html.widgets import IntSlider\n",
  169. "\n",
  170. "h = historydict.HistoryDict('my_widget_history.db')\n",
  171. "WidgetHistory(h)"
  172. ]
  173. },
  174. {
  175. "cell_type": "code",
  176. "execution_count": 3,
  177. "metadata": {
  178. "collapsed": true
  179. },
  180. "outputs": [],
  181. "source": [
  182. "slider = IntSlider(description='example 1', min=0, max=20, model_id='example1')\n",
  183. "display(slider)\n",
  184. "display(IntSlider(description='example 2', min=0, max=20, model_id='example2'))"
  185. ]
  186. },
  187. {
  188. "cell_type": "code",
  189. "execution_count": null,
  190. "metadata": {
  191. "collapsed": true
  192. },
  193. "outputs": [],
  194. "source": []
  195. }
  196. ],
  197. "metadata": {
  198. "kernelspec": {
  199. "display_name": "Python 3",
  200. "language": "python",
  201. "name": "python3"
  202. },
  203. "language_info": {
  204. "codemirror_mode": {
  205. "name": "ipython",
  206. "version": 3
  207. },
  208. "file_extension": ".py",
  209. "mimetype": "text/x-python",
  210. "name": "python",
  211. "nbconvert_exporter": "python",
  212. "pygments_lexer": "ipython3",
  213. "version": "3.4.3"
  214. },
  215. "widgets_state": {
  216. "18804a3c333f410f9e4cdd6b67661968": {
  217. "model_module": null,
  218. "model_name": "WidgetModel",
  219. "state": {
  220. "_css": [],
  221. "_dom_classes": [],
  222. "_view_module": "",
  223. "_view_name": "FlexBoxView",
  224. "align": "start",
  225. "background_color": "",
  226. "border_color": "",
  227. "border_radius": "",
  228. "border_style": "",
  229. "border_width": "",
  230. "box_style": "",
  231. "children": [
  232. "IPY_MODEL_3e632e91bfd94fb9b751d1095d8c8e32",
  233. "IPY_MODEL_f8e617169bc844cba1b5cdffeeac92c0",
  234. "IPY_MODEL_590fff8075d7496b8967dc26292fd03f"
  235. ],
  236. "color": "",
  237. "flex": 0,
  238. "font_family": "",
  239. "font_size": "",
  240. "font_style": "",
  241. "font_weight": "",
  242. "height": "",
  243. "margin": "",
  244. "msg_throttle": 3,
  245. "orientation": "horizontal",
  246. "overflow_x": "",
  247. "overflow_y": "",
  248. "pack": "start",
  249. "padding": "",
  250. "version": 0,
  251. "visible": true,
  252. "width": ""
  253. },
  254. "views": [
  255. 1
  256. ]
  257. },
  258. "3e632e91bfd94fb9b751d1095d8c8e32": {
  259. "model_module": null,
  260. "model_name": "WidgetModel",
  261. "state": {
  262. "_css": [],
  263. "_dom_classes": [],
  264. "_range": false,
  265. "_view_module": "",
  266. "_view_name": "IntSliderView",
  267. "background_color": "",
  268. "border_color": "",
  269. "border_radius": "",
  270. "border_style": "",
  271. "border_width": "",
  272. "color": "",
  273. "description": "history",
  274. "disabled": false,
  275. "font_family": "",
  276. "font_size": "",
  277. "font_style": "",
  278. "font_weight": "",
  279. "height": "",
  280. "margin": "",
  281. "max": 0,
  282. "min": -21,
  283. "msg_throttle": 3,
  284. "orientation": "horizontal",
  285. "padding": "",
  286. "readout": true,
  287. "slider_color": "",
  288. "step": 1,
  289. "value": 0,
  290. "version": 0,
  291. "visible": true,
  292. "width": ""
  293. },
  294. "views": []
  295. },
  296. "590fff8075d7496b8967dc26292fd03f": {
  297. "model_module": null,
  298. "model_name": "WidgetModel",
  299. "state": {
  300. "_css": [],
  301. "_dom_classes": [],
  302. "_view_module": "",
  303. "_view_name": "ButtonView",
  304. "background_color": "",
  305. "border_color": "",
  306. "border_radius": "",
  307. "border_style": "",
  308. "border_width": "",
  309. "button_style": "",
  310. "color": "",
  311. "description": "Playback",
  312. "disabled": false,
  313. "font_family": "",
  314. "font_size": "",
  315. "font_style": "",
  316. "font_weight": "",
  317. "height": "",
  318. "icon": "",
  319. "margin": "",
  320. "msg_throttle": 3,
  321. "padding": "",
  322. "tooltip": "",
  323. "version": 0,
  324. "visible": true,
  325. "width": ""
  326. },
  327. "views": []
  328. },
  329. "5e503da58c6644079f56cf9884e804e9": {
  330. "model_module": null,
  331. "model_name": "WidgetModel",
  332. "state": {
  333. "_css": [],
  334. "_dom_classes": [],
  335. "_range": false,
  336. "_view_module": "",
  337. "_view_name": "IntSliderView",
  338. "background_color": "",
  339. "border_color": "",
  340. "border_radius": "",
  341. "border_style": "",
  342. "border_width": "",
  343. "color": "",
  344. "description": "example",
  345. "disabled": false,
  346. "font_family": "",
  347. "font_size": "",
  348. "font_style": "",
  349. "font_weight": "",
  350. "height": "",
  351. "margin": "",
  352. "max": 20,
  353. "min": 0,
  354. "msg_throttle": 3,
  355. "orientation": "horizontal",
  356. "padding": "",
  357. "readout": true,
  358. "slider_color": "",
  359. "step": 1,
  360. "value": 0,
  361. "version": 0,
  362. "visible": true,
  363. "width": ""
  364. },
  365. "views": []
  366. },
  367. "example1": {
  368. "model_module": null,
  369. "model_name": "WidgetModel",
  370. "state": {
  371. "_css": [],
  372. "_dom_classes": [],
  373. "_range": false,
  374. "_view_module": "",
  375. "_view_name": "IntSliderView",
  376. "background_color": "",
  377. "border_color": "",
  378. "border_radius": "",
  379. "border_style": "",
  380. "border_width": "",
  381. "color": "",
  382. "description": "example 1",
  383. "disabled": false,
  384. "font_family": "",
  385. "font_size": "",
  386. "font_style": "",
  387. "font_weight": "",
  388. "height": "",
  389. "margin": "",
  390. "max": 20,
  391. "min": 0,
  392. "msg_throttle": 3,
  393. "orientation": "horizontal",
  394. "padding": "",
  395. "readout": true,
  396. "slider_color": "",
  397. "step": 1,
  398. "value": 15,
  399. "version": 0,
  400. "visible": true,
  401. "width": ""
  402. },
  403. "views": [
  404. 2
  405. ]
  406. },
  407. "example2": {
  408. "model_module": null,
  409. "model_name": "WidgetModel",
  410. "state": {
  411. "_css": [],
  412. "_dom_classes": [],
  413. "_range": false,
  414. "_view_module": "",
  415. "_view_name": "IntSliderView",
  416. "background_color": "",
  417. "border_color": "",
  418. "border_radius": "",
  419. "border_style": "",
  420. "border_width": "",
  421. "color": "",
  422. "description": "example 2",
  423. "disabled": false,
  424. "font_family": "",
  425. "font_size": "",
  426. "font_style": "",
  427. "font_weight": "",
  428. "height": "",
  429. "margin": "",
  430. "max": 20,
  431. "min": 0,
  432. "msg_throttle": 3,
  433. "orientation": "horizontal",
  434. "padding": "",
  435. "readout": true,
  436. "slider_color": "",
  437. "step": 1,
  438. "value": 17,
  439. "version": 0,
  440. "visible": true,
  441. "width": ""
  442. },
  443. "views": [
  444. 2
  445. ]
  446. },
  447. "f8e617169bc844cba1b5cdffeeac92c0": {
  448. "model_module": null,
  449. "model_name": "WidgetModel",
  450. "state": {
  451. "_css": [],
  452. "_dom_classes": [],
  453. "_view_module": "",
  454. "_view_name": "ButtonView",
  455. "background_color": "",
  456. "border_color": "",
  457. "border_radius": "",
  458. "border_style": "",
  459. "border_width": "",
  460. "button_style": "",
  461. "color": "",
  462. "description": "Save State",
  463. "disabled": false,
  464. "font_family": "",
  465. "font_size": "",
  466. "font_style": "",
  467. "font_weight": "",
  468. "height": "",
  469. "icon": "",
  470. "margin": "",
  471. "msg_throttle": 3,
  472. "padding": "",
  473. "tooltip": "",
  474. "version": 0,
  475. "visible": true,
  476. "width": ""
  477. },
  478. "views": []
  479. }
  480. }
  481. },
  482. "nbformat": 4,
  483. "nbformat_minor": 0
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement