##// END OF EJS Templates
Added an example of how one can create a dialog using widgets.
Jonathan Frederic -
Show More
@@ -0,0 +1,200 b''
1 {
2 "metadata": {
3 "name": ""
4 },
5 "nbformat": 3,
6 "nbformat_minor": 0,
7 "worksheets": [
8 {
9 "cells": [
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "Simple example notebook that shows how one can use widgets to build custom dialogs."
15 ]
16 },
17 {
18 "cell_type": "code",
19 "collapsed": false,
20 "input": [
21 "from IPython.html import widgets\n",
22 "from IPython.display import display"
23 ],
24 "language": "python",
25 "metadata": {},
26 "outputs": [],
27 "prompt_number": 1
28 },
29 {
30 "cell_type": "markdown",
31 "metadata": {},
32 "source": [
33 "By using Bootstrap's modal class and ContainerWidgets, we can build a simple dialog window."
34 ]
35 },
36 {
37 "cell_type": "code",
38 "collapsed": false,
39 "input": [
40 "def scrub_text_html(text):\n",
41 " text = text.replace('&', '&')\n",
42 " text = text.replace(' ', ' ')\n",
43 " text = text.replace('<', '&lt;')\n",
44 " text = text.replace('>', '&gt;')\n",
45 " text = text.replace('\\n', '<br>\\n')\n",
46 " return text\n",
47 "\n",
48 "def create_dialog(title=None, on_hidden=None):\n",
49 " dialog = widgets.ContainerWidget(visible=False)\n",
50 " dialog_header = widgets.ContainerWidget(parent=dialog)\n",
51 " dialog_header_close = widgets.ButtonWidget(parent=dialog_header, description = '&times;')\n",
52 " dialog_header_label = widgets.StringWidget(parent=dialog_header, default_view_name='LabelView')\n",
53 " dialog_body = widgets.ContainerWidget(parent=dialog)\n",
54 " dialog_footer = widgets.ContainerWidget(parent=dialog)\n",
55 " \n",
56 " if title is None or title == '':\n",
57 " title = ' '\n",
58 " dialog_header_label.value = '<h3>%s</h3>' % scrub_text_html(title)\n",
59 " \n",
60 " def handle_close():\n",
61 " dialog.visible = False\n",
62 " if on_hidden is not None:\n",
63 " on_hidden(dialog)\n",
64 " dialog_header_close.on_click(handle_close)\n",
65 " \n",
66 " display(dialog)\n",
67 " \n",
68 " dialog_header.add_class('modal-header')\n",
69 " dialog_body.add_class('modal-body')\n",
70 " dialog_footer.add_class('modal-footer')\n",
71 " dialog.add_class('modal')\n",
72 " \n",
73 " dialog_header_close.remove_class('btn')\n",
74 " dialog_header_close.add_class('close')\n",
75 " \n",
76 " return dialog_body, dialog_footer, dialog\n"
77 ],
78 "language": "python",
79 "metadata": {},
80 "outputs": [],
81 "prompt_number": 2
82 },
83 {
84 "cell_type": "markdown",
85 "metadata": {},
86 "source": [
87 "Using this `show_dialog` method, custom dialogs can be made using widgets. Below is an example of a Yes/No dialog."
88 ]
89 },
90 {
91 "cell_type": "code",
92 "collapsed": false,
93 "input": [
94 "# Since Python has a global thread lock, everything runs on a single thread.\n",
95 "# Because of this, we use an asynronous model (callbacks).\n",
96 "def show_yes_no(prompt, yes_callback=None, no_callback=None, title=None):\n",
97 " \n",
98 " def handle_hidden(dialog_window):\n",
99 " if no_callback is not None:\n",
100 " no_callback()\n",
101 " dialog_window.close()\n",
102 " \n",
103 " (dialog_body, dialog_footer, dialog_window) = create_dialog(title=title, on_hidden=handle_hidden)\n",
104 " \n",
105 " def handle_yes():\n",
106 " dialog_window.visible = False\n",
107 " if yes_callback is not None:\n",
108 " yes_callback()\n",
109 " dialog_window.close()\n",
110 " \n",
111 " def handle_no():\n",
112 " dialog_window.visible = False\n",
113 " handle_hidden(dialog_window)\n",
114 " \n",
115 " yes_button = widgets.ButtonWidget(parent=dialog_footer, description='Yes')\n",
116 " yes_button.on_click(handle_yes)\n",
117 " no_button = widgets.ButtonWidget(parent=dialog_footer, description='No')\n",
118 " no_button.on_click(handle_no)\n",
119 " prompt_label = widgets.StringWidget(parent=dialog_body, value=scrub_text_html(prompt), default_view_name='LabelView')\n",
120 " \n",
121 " display(yes_button)\n",
122 " display(no_button)\n",
123 " display(prompt_label)\n",
124 " \n",
125 " yes_button.add_class('btn-success')\n",
126 " no_button.add_class('btn-danger')\n",
127 " \n",
128 " dialog_window.visible=True\n",
129 " \n",
130 " "
131 ],
132 "language": "python",
133 "metadata": {},
134 "outputs": [],
135 "prompt_number": 3
136 },
137 {
138 "cell_type": "heading",
139 "level": 1,
140 "metadata": {},
141 "source": [
142 "Test"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "collapsed": false,
148 "input": [
149 "def yes():\n",
150 " show_yes_no(\"Do you want to show the dialog again?\", yes, title=\"Self displaying dialog\")\n",
151 "yes()"
152 ],
153 "language": "python",
154 "metadata": {},
155 "outputs": [],
156 "prompt_number": 4
157 },
158 {
159 "cell_type": "markdown",
160 "metadata": {},
161 "source": [
162 "Dialog result handlers can contain nested dialogs. The following example shows how syncronous logic can be simulated using closures."
163 ]
164 },
165 {
166 "cell_type": "code",
167 "collapsed": false,
168 "input": [
169 "title = \"Interactive Story\"\n",
170 "def open_door():\n",
171 " show_yes_no(\"The house is empty. You are tired and decide to sleep. You live to see another day.\")\n",
172 "\n",
173 "def stay_outside():\n",
174 " def fight():\n",
175 " show_yes_no(\"You try to fight the wolves but die in the process.\", title=title)\n",
176 " \n",
177 " def panic():\n",
178 " def flight():\n",
179 " show_yes_no(\"You run as fast as you can. You manage to escape the\\n\" + \\\n",
180 " \"wolves but the cold is beginning to get to you. You\\n\" + \\\n",
181 " \"sit in the snow to take a nap. You freeze and die.\", title=title)\n",
182 " \n",
183 " show_yes_no(\"You panic. Do you enter the cabin now?\", open_door, flight, title=title)\n",
184 " \n",
185 " show_yes_no(\"A pack of wolves approach. Do you want to fight them?\", fight, panic, title=title)\n",
186 "\n",
187 "show_yes_no(\"You are standing outside in a blizzard on a cold winter night.\\n\" + \\\n",
188 " \"A warm cabin with the lights on is in front of you.\\n\\n\" + \\\n",
189 " \"Do you want to enter the cabin?\", open_door, stay_outside, title=title)\n"
190 ],
191 "language": "python",
192 "metadata": {},
193 "outputs": [],
194 "prompt_number": 5
195 }
196 ],
197 "metadata": {}
198 }
199 ]
200 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now