Show More
@@ -1,194 +1,186 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "name": "" |
|
3 | "name": "" | |
4 | }, |
|
4 | }, | |
5 | "nbformat": 3, |
|
5 | "nbformat": 3, | |
6 | "nbformat_minor": 0, |
|
6 | "nbformat_minor": 0, | |
7 | "worksheets": [ |
|
7 | "worksheets": [ | |
8 | { |
|
8 | { | |
9 | "cells": [ |
|
9 | "cells": [ | |
10 | { |
|
10 | { | |
11 | "cell_type": "code", |
|
11 | "cell_type": "code", | |
12 | "collapsed": false, |
|
12 | "collapsed": false, | |
13 | "input": [ |
|
13 | "input": [ | |
|
14 | "# Widget related imports\n", | |||
14 | "from IPython.html import widgets\n", |
|
15 | "from IPython.html import widgets\n", | |
15 |
"from IPython.display import display, |
|
16 | "from IPython.display import display, clear_output, Javascript\n", | |
|
17 | "from IPython.utils.traitlets import Unicode\n", | |||
|
18 | "\n", | |||
|
19 | "# nbconvert related imports\n", | |||
16 | "from IPython.nbconvert import get_export_names, export_by_name\n", |
|
20 | "from IPython.nbconvert import get_export_names, export_by_name\n", | |
17 | "from IPython.nbconvert.writers import FilesWriter\n", |
|
21 | "from IPython.nbconvert.writers import FilesWriter\n", | |
18 | "from IPython.nbformat import current" |
|
22 | "from IPython.nbformat import current" | |
19 | ], |
|
23 | ], | |
20 | "language": "python", |
|
24 | "language": "python", | |
21 | "metadata": {}, |
|
25 | "metadata": {}, | |
22 | "outputs": [], |
|
26 | "outputs": [], | |
23 | "prompt_number": 1 |
|
27 | "prompt_number": 1 | |
24 | }, |
|
28 | }, | |
25 | { |
|
29 | { | |
26 | "cell_type": "markdown", |
|
30 | "cell_type": "markdown", | |
27 | "metadata": {}, |
|
31 | "metadata": {}, | |
28 | "source": [ |
|
32 | "source": [ | |
29 |
"Create a |
|
33 | "Create a textbox Widget without displaying it. The widget will be used to store the notebook's name which is otherwise only available in the front-end." | |
30 | ] |
|
34 | ] | |
31 | }, |
|
35 | }, | |
32 | { |
|
36 | { | |
33 | "cell_type": "code", |
|
37 | "cell_type": "code", | |
34 | "collapsed": false, |
|
38 | "collapsed": false, | |
35 | "input": [ |
|
39 | "input": [ | |
36 |
"notebook_name = widgets. |
|
40 | "notebook_name = widgets.TextBoxWidget()" | |
37 | "display(notebook_name)" |
|
|||
38 | ], |
|
41 | ], | |
39 | "language": "python", |
|
42 | "language": "python", | |
40 | "metadata": {}, |
|
43 | "metadata": {}, | |
41 | "outputs": [], |
|
44 | "outputs": [], | |
42 | "prompt_number": 2 |
|
45 | "prompt_number": 2 | |
43 | }, |
|
46 | }, | |
44 | { |
|
47 | { | |
45 | "cell_type": "markdown", |
|
48 | "cell_type": "markdown", | |
46 | "metadata": {}, |
|
49 | "metadata": {}, | |
47 | "source": [ |
|
50 | "source": [ | |
48 | "Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget." |
|
51 | "Get the current notebook's name by pushing JavaScript to the browser that sets the notebook name in a string widget." | |
49 | ] |
|
52 | ] | |
50 | }, |
|
53 | }, | |
51 | { |
|
54 | { | |
52 | "cell_type": "code", |
|
55 | "cell_type": "code", | |
53 | "collapsed": false, |
|
56 | "collapsed": false, | |
54 | "input": [ |
|
57 | "input": [ | |
55 |
"js = \"\"\"var model = IPython.notebook.kernel. |
|
58 | "js = \"\"\"var model = IPython.notebook.kernel.widget_manager.get_model('{model_id}');\n", | |
56 | "model.set('value', IPython.notebook.notebook_name);\n", |
|
59 | "model.set('value', IPython.notebook.notebook_name);\n", | |
57 |
"model.save();\"\"\".format( |
|
60 | "model.save();\"\"\".format(model_id=notebook_name.model_id)\n", | |
58 | "display(Javascript(data=js))" |
|
61 | "display(Javascript(data=js))" | |
59 | ], |
|
62 | ], | |
60 | "language": "python", |
|
63 | "language": "python", | |
61 | "metadata": {}, |
|
64 | "metadata": {}, | |
62 | "outputs": [ |
|
65 | "outputs": [ | |
63 | { |
|
66 | { | |
64 | "javascript": [ |
|
67 | "javascript": [ | |
65 | "var model = IPython.notebook.kernel.comm_manager.comms['af0fadc224134034af6141995abf625a'].model;\n", |
|
68 | "var model = IPython.notebook.kernel.widget_manager.get_model('8c6583524eb3422c99491730a3e1ce6c');\n", | |
66 | "model.set('value', IPython.notebook.notebook_name);\n", |
|
69 | "model.set('value', IPython.notebook.notebook_name);\n", | |
67 | "model.save();" |
|
70 | "model.save();" | |
68 | ], |
|
71 | ], | |
69 | "metadata": {}, |
|
72 | "metadata": {}, | |
70 | "output_type": "display_data", |
|
73 | "output_type": "display_data", | |
71 | "text": [ |
|
74 | "text": [ | |
72 |
"<IPython.core.display.Javascript at 0x |
|
75 | "<IPython.core.display.Javascript at 0x164ea50>" | |
73 | ] |
|
76 | ] | |
74 | } |
|
77 | } | |
75 | ], |
|
78 | ], | |
76 | "prompt_number": 3 |
|
79 | "prompt_number": 3 | |
77 | }, |
|
80 | }, | |
78 | { |
|
81 | { | |
79 | "cell_type": "code", |
|
82 | "cell_type": "code", | |
80 | "collapsed": false, |
|
83 | "collapsed": false, | |
81 | "input": [ |
|
84 | "input": [ | |
82 | "filename = notebook_name.value\n", |
|
85 | "filename = notebook_name.value\n", | |
83 | "filename" |
|
86 | "filename" | |
84 | ], |
|
87 | ], | |
85 | "language": "python", |
|
88 | "language": "python", | |
86 | "metadata": {}, |
|
89 | "metadata": {}, | |
87 | "outputs": [ |
|
90 | "outputs": [ | |
88 | { |
|
91 | { | |
89 | "metadata": {}, |
|
92 | "metadata": {}, | |
90 | "output_type": "pyout", |
|
93 | "output_type": "pyout", | |
91 | "prompt_number": 4, |
|
94 | "prompt_number": 4, | |
92 | "text": [ |
|
95 | "text": [ | |
93 | "u'Export As (nbconvert).ipynb'" |
|
96 | "u'Export As (nbconvert).ipynb'" | |
94 | ] |
|
97 | ] | |
95 | } |
|
98 | } | |
96 | ], |
|
99 | ], | |
97 | "prompt_number": 4 |
|
100 | "prompt_number": 4 | |
98 | }, |
|
101 | }, | |
99 | { |
|
102 | { | |
100 | "cell_type": "markdown", |
|
103 | "cell_type": "markdown", | |
101 | "metadata": {}, |
|
104 | "metadata": {}, | |
102 | "source": [ |
|
105 | "source": [ | |
103 | "Create the widget that will allow the user to Export the current notebook." |
|
106 | "Create the widget that will allow the user to Export the current notebook." | |
104 | ] |
|
107 | ] | |
105 | }, |
|
108 | }, | |
106 | { |
|
109 | { | |
107 | "cell_type": "code", |
|
110 | "cell_type": "code", | |
108 | "collapsed": false, |
|
111 | "collapsed": false, | |
109 | "input": [ |
|
112 | "input": [ | |
110 | "container = widgets.ContainerWidget()\n", |
|
113 | "exporter_names = widgets.DropdownWidget(values=get_export_names(), value='html')\n", | |
111 | "container.vbox()\n", |
|
114 | "export_button = widgets.ButtonWidget(description=\"Export\")\n", | |
112 | "container.align_center()\n", |
|
115 | "download_link = widgets.HTMLWidget(visible=False)" | |
113 | "\n", |
|
|||
114 | "options = widgets.ContainerWidget(parent=container)\n", |
|
|||
115 | "options.hbox()\n", |
|
|||
116 | "options.align_center()\n", |
|
|||
117 | "exporter_names = widgets.SelectionWidget(parent=options, values=get_export_names(), value='html')\n", |
|
|||
118 | "export_button = widgets.ButtonWidget(parent=options, description=\"Export\")\n", |
|
|||
119 | "\n", |
|
|||
120 | "download_link = widgets.StringWidget(parent=container, default_view_name=\"HTMLView\", visible=False)" |
|
|||
121 | ], |
|
116 | ], | |
122 | "language": "python", |
|
117 | "language": "python", | |
123 | "metadata": {}, |
|
118 | "metadata": {}, | |
124 | "outputs": [], |
|
119 | "outputs": [], | |
125 | "prompt_number": 5 |
|
120 | "prompt_number": 5 | |
126 | }, |
|
121 | }, | |
127 | { |
|
122 | { | |
128 | "cell_type": "markdown", |
|
123 | "cell_type": "markdown", | |
129 | "metadata": {}, |
|
124 | "metadata": {}, | |
130 | "source": [ |
|
125 | "source": [ | |
131 | "Export the notebook when the export button is clicked." |
|
126 | "Export the notebook when the export button is clicked." | |
132 | ] |
|
127 | ] | |
133 | }, |
|
128 | }, | |
134 | { |
|
129 | { | |
135 | "cell_type": "code", |
|
130 | "cell_type": "code", | |
136 | "collapsed": false, |
|
131 | "collapsed": false, | |
137 | "input": [ |
|
132 | "input": [ | |
138 | "file_writer = FilesWriter()\n", |
|
133 | "file_writer = FilesWriter()\n", | |
139 | "\n", |
|
134 | "\n", | |
140 | "def export(name, nb):\n", |
|
135 | "def export(name, nb):\n", | |
141 | " \n", |
|
136 | " \n", | |
142 | " # Get a unique key for the notebook and set it in the resources object.\n", |
|
137 | " # Get a unique key for the notebook and set it in the resources object.\n", | |
143 | " notebook_name = name[:name.rfind('.')]\n", |
|
138 | " notebook_name = name[:name.rfind('.')]\n", | |
144 | " resources = {}\n", |
|
139 | " resources = {}\n", | |
145 | " resources['unique_key'] = notebook_name\n", |
|
140 | " resources['unique_key'] = notebook_name\n", | |
146 | " resources['output_files_dir'] = '%s_files' % notebook_name\n", |
|
141 | " resources['output_files_dir'] = '%s_files' % notebook_name\n", | |
147 | "\n", |
|
142 | "\n", | |
148 | " # Try to export\n", |
|
143 | " # Try to export\n", | |
149 | " try:\n", |
|
144 | " try:\n", | |
150 | " output, resources = export_by_name(exporter_names.value, nb)\n", |
|
145 | " output, resources = export_by_name(exporter_names.value, nb)\n", | |
151 | " except ConversionException as e:\n", |
|
146 | " except ConversionException as e:\n", | |
152 | " download_link.value = \"<br>Could not export notebook!\"\n", |
|
147 | " download_link.value = \"<br>Could not export notebook!\"\n", | |
153 | " else:\n", |
|
148 | " else:\n", | |
154 | " write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", |
|
149 | " write_results = file_writer.write(output, resources, notebook_name=notebook_name)\n", | |
155 | " \n", |
|
150 | " \n", | |
156 | " download_link.value = \"<br>Results: <a href='files/{filename}'><i>\\\"{filename}\\\"</i></a>\".format(filename=write_results)\n", |
|
151 | " download_link.value = \"<br>Results: <a href='files/{filename}'><i>\\\"{filename}\\\"</i></a>\".format(filename=write_results)\n", | |
157 | " download_link.visible = True" |
|
152 | " download_link.visible = True\n", | |
158 | ], |
|
153 | " \n", | |
159 | "language": "python", |
|
|||
160 | "metadata": {}, |
|
|||
161 | "outputs": [], |
|
|||
162 | "prompt_number": 6 |
|
|||
163 | }, |
|
|||
164 | { |
|
|||
165 | "cell_type": "code", |
|
|||
166 | "collapsed": false, |
|
|||
167 | "input": [ |
|
|||
168 | "def handle_export():\n", |
|
154 | "def handle_export():\n", | |
169 | " with open(filename, 'r') as f:\n", |
|
155 | " with open(filename, 'r') as f:\n", | |
170 | " export(filename, current.read(f, 'json'))\n", |
|
156 | " export(filename, current.read(f, 'json'))\n", | |
171 | "export_button.on_click(handle_export)" |
|
157 | "export_button.on_click(handle_export)" | |
172 | ], |
|
158 | ], | |
173 | "language": "python", |
|
159 | "language": "python", | |
174 | "metadata": {}, |
|
160 | "metadata": {}, | |
175 | "outputs": [], |
|
161 | "outputs": [], | |
176 |
"prompt_number": |
|
162 | "prompt_number": 6 | |
|
163 | }, | |||
|
164 | { | |||
|
165 | "cell_type": "markdown", | |||
|
166 | "metadata": {}, | |||
|
167 | "source": [ | |||
|
168 | "Display the controls." | |||
|
169 | ] | |||
177 | }, |
|
170 | }, | |
178 | { |
|
171 | { | |
179 | "cell_type": "code", |
|
172 | "cell_type": "code", | |
180 | "collapsed": false, |
|
173 | "collapsed": false, | |
181 | "input": [ |
|
174 | "input": [ | |
182 | "download_link.visible = False\n", |
|
175 | "display(exporter_names, export_button, download_link)" | |
183 | "display(container)" |
|
|||
184 | ], |
|
176 | ], | |
185 | "language": "python", |
|
177 | "language": "python", | |
186 | "metadata": {}, |
|
178 | "metadata": {}, | |
187 | "outputs": [], |
|
179 | "outputs": [], | |
188 |
"prompt_number": |
|
180 | "prompt_number": 7 | |
189 | } |
|
181 | } | |
190 | ], |
|
182 | ], | |
191 | "metadata": {} |
|
183 | "metadata": {} | |
192 | } |
|
184 | } | |
193 | ] |
|
185 | ] | |
194 | } No newline at end of file |
|
186 | } |
@@ -1,223 +1,248 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "cell_tags": [ |
|
3 | "cell_tags": [ | |
4 | [ |
|
4 | [ | |
5 | "<None>", |
|
5 | "<None>", | |
6 | null |
|
6 | null | |
7 | ] |
|
7 | ] | |
8 | ], |
|
8 | ], | |
9 | "name": "" |
|
9 | "name": "" | |
10 | }, |
|
10 | }, | |
11 | "nbformat": 3, |
|
11 | "nbformat": 3, | |
12 | "nbformat_minor": 0, |
|
12 | "nbformat_minor": 0, | |
13 | "worksheets": [ |
|
13 | "worksheets": [ | |
14 | { |
|
14 | { | |
15 | "cells": [ |
|
15 | "cells": [ | |
16 | { |
|
16 | { | |
17 | "cell_type": "code", |
|
17 | "cell_type": "code", | |
18 | "collapsed": false, |
|
18 | "collapsed": false, | |
19 | "input": [ |
|
19 | "input": [ | |
20 | "from __future__ import print_function # py 2.7 compat\n", |
|
|||
21 | "\n", |
|
|||
22 | "import base64\n", |
|
20 | "import base64\n", | |
23 | "from IPython.html import widgets # Widget definitions\n", |
|
21 | "from __future__ import print_function # py 2.7 compat.\n", | |
24 | "from IPython.display import display # Used to display widgets in the notebook" |
|
22 | "from IPython.html import widgets # Widget definitions.\n", | |
|
23 | "from IPython.utils.traitlets import Unicode # Traitlet needed to add synced attributes to the widget." | |||
25 | ], |
|
24 | ], | |
26 | "language": "python", |
|
25 | "language": "python", | |
27 | "metadata": {}, |
|
26 | "metadata": {}, | |
28 | "outputs": [], |
|
27 | "outputs": [], | |
29 | "prompt_number": 1 |
|
28 | "prompt_number": 1 | |
30 | }, |
|
29 | }, | |
31 | { |
|
30 | { | |
32 |
"cell_type": " |
|
31 | "cell_type": "markdown", | |
33 | "level": 1, |
|
|||
34 | "metadata": {}, |
|
32 | "metadata": {}, | |
35 | "source": [ |
|
33 | "source": [ | |
36 | "Custom Widget" |
|
34 | "This is a custom widget that allows the user to upload file data to the notebook server. The file data is sent via a statefull `value` attribute of the widget. The widget has an upload failed event that fires in the front-end and is echoed to the back-end using a custom msg." | |
37 | ] |
|
35 | ] | |
38 | }, |
|
36 | }, | |
39 | { |
|
37 | { | |
40 | "cell_type": "code", |
|
38 | "cell_type": "code", | |
41 | "collapsed": false, |
|
39 | "collapsed": false, | |
42 | "input": [ |
|
40 | "input": [ | |
43 | "# Import the base Widget class and the traitlets Unicode class.\n", |
|
41 | "class FileWidget(widgets.DOMWidget):\n", | |
44 | "from IPython.html.widgets import Widget\n", |
|
42 | " _view_name = Unicode('FilePickerView', sync=True)\n", | |
45 | "from IPython.utils.traitlets import Unicode, Int\n", |
|
43 | " value = Unicode(sync=True)\n", | |
46 | "\n", |
|
44 | " filename = Unicode(sync=True)\n", | |
47 | "# Define our FileWidget and its target model and default view.\n", |
|
|||
48 | "class FileWidget(Widget):\n", |
|
|||
49 | " target_name = Unicode('FileWidgetModel')\n", |
|
|||
50 | " default_view_name = Unicode('FilePickerView')\n", |
|
|||
51 | " \n", |
|
45 | " \n", | |
52 | " # Define the custom state properties to sync with the front-end\n", |
|
46 | " def __init__(self, **kwargs):\n", | |
53 | " _keys = ['value', 'filename']\n", |
|
47 | " \"\"\"Constructor\"\"\"\n", | |
54 | " value = Unicode('')\n", |
|
48 | " widgets.DOMWidget.__init__(self, **kwargs) # Call the base.\n", | |
55 |
" |
|
49 | " \n", | |
56 | " on_failed = Int(0)" |
|
50 | " # Allow the user to register error callbacks with the following signatures:\n", | |
|
51 | " # callback()\n", | |||
|
52 | " # callback(sender)\n", | |||
|
53 | " self.errors = widgets.CallbackDispatcher(accepted_nargs=[0, 1])\n", | |||
|
54 | " \n", | |||
|
55 | " # Listen for custom msgs\n", | |||
|
56 | " self.on_msg(self._handle_custom_msg)\n", | |||
|
57 | "\n", | |||
|
58 | " def _handle_custom_msg(self, content):\n", | |||
|
59 | " \"\"\"Handle a msg from the front-end.\n", | |||
|
60 | "\n", | |||
|
61 | " Parameters\n", | |||
|
62 | " ----------\n", | |||
|
63 | " content: dict\n", | |||
|
64 | " Content of the msg.\"\"\"\n", | |||
|
65 | " if 'event' in content and content['event'] == 'error':\n", | |||
|
66 | " self.errors()\n", | |||
|
67 | " self.errors(self)\n", | |||
|
68 | " " | |||
57 | ], |
|
69 | ], | |
58 | "language": "python", |
|
70 | "language": "python", | |
59 | "metadata": {}, |
|
71 | "metadata": {}, | |
60 | "outputs": [], |
|
72 | "outputs": [], | |
61 | "prompt_number": 2 |
|
73 | "prompt_number": 2 | |
62 | }, |
|
74 | }, | |
63 | { |
|
75 | { | |
64 | "cell_type": "code", |
|
76 | "cell_type": "code", | |
65 | "collapsed": false, |
|
77 | "collapsed": false, | |
66 | "input": [ |
|
78 | "input": [ | |
67 | "%%javascript\n", |
|
79 | "%%javascript\n", | |
68 | "\n", |
|
80 | "\n", | |
69 | "require([\"notebook/js/widget\"], function(){\n", |
|
81 | "require([\"notebook/js/widgets/widget\"], function(WidgetManager){\n", | |
70 |
" |
|
82 | "\n", | |
71 | " // Define the FileModel and register it with the widget manager.\n", |
|
|||
72 | " var FileModel = IPython.WidgetModel.extend({});\n", |
|
|||
73 | " IPython.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n", |
|
|||
74 | " \n", |
|
|||
75 | " // Define the FilePickerView\n", |
|
|||
76 | " var FilePickerView = IPython.WidgetView.extend({\n", |
|
83 | " var FilePickerView = IPython.WidgetView.extend({\n", | |
77 | " \n", |
|
|||
78 | " render: function(){\n", |
|
84 | " render: function(){\n", | |
79 |
" |
|
85 | " // Render the view.\n", | |
80 |
" this. |
|
86 | " this.setElement($('<input />')\n", | |
81 | " .attr('type', 'file')\n", |
|
87 | " .attr('type', 'file'));\n", | |
82 | " .change(function(evt){ that.handleFileChange(evt) });\n", |
|
|||
83 | " },\n", |
|
88 | " },\n", | |
84 | " \n", |
|
89 | " \n", | |
85 |
" |
|
90 | " events: {\n", | |
86 | " handleFileChange: function(evt) { \n", |
|
91 | " // List of events and their handlers.\n", | |
|
92 | " 'change': 'handle_file_change',\n", | |||
|
93 | " },\n", | |||
|
94 | " \n", | |||
|
95 | " handle_file_change: function(evt) { \n", | |||
|
96 | " // Handle when the user has changed the file.\n", | |||
87 | " \n", |
|
97 | " \n", | |
88 | " //Retrieve the first (and only!) File from the FileList object\n", |
|
98 | " // Retrieve the first (and only!) File from the FileList object\n", | |
89 |
" var |
|
99 | " var file = evt.target.files[0];\n", | |
90 |
" |
|
100 | " if (file) {\n", | |
91 | " if (f) {\n", |
|
101 | "\n", | |
92 | " var r = new FileReader();\n", |
|
102 | " // Read the file's textual content and set value to those contents.\n", | |
93 |
" |
|
103 | " var that = this;\n", | |
|
104 | " var file_reader = new FileReader();\n", | |||
|
105 | " file_reader.onload = function(e) {\n", | |||
94 | " that.model.set('value', e.target.result);\n", |
|
106 | " that.model.set('value', e.target.result);\n", | |
95 |
" that. |
|
107 | " that.touch();\n", | |
96 | " }\n", |
|
108 | " }\n", | |
97 | " r.readAsText(f);\n", |
|
109 | " file_reader.readAsText(file);\n", | |
98 | " } else {\n", |
|
110 | " } else {\n", | |
99 | " this.model.set('on_failed', this.model.get('on_failed') + 1);\n", |
|
111 | "\n", | |
100 | " this.model.update_other_views(this);\n", |
|
112 | " // The file couldn't be opened. Send an error msg to the\n", | |
|
113 | " // back-end.\n", | |||
|
114 | " this.send({ 'event': 'error' });\n", | |||
101 | " }\n", |
|
115 | " }\n", | |
102 | " this.model.set('filename', f.name);\n", |
|
116 | "\n", | |
103 | " this.model.update_other_views(this);\n", |
|
117 | " // Set the filename of the file.\n", | |
|
118 | " this.model.set('filename', file.name);\n", | |||
|
119 | " this.touch();\n", | |||
104 | " },\n", |
|
120 | " },\n", | |
105 | " });\n", |
|
121 | " });\n", | |
106 | " \n", |
|
122 | " \n", | |
107 | " // Register the DatePickerView with the widget manager.\n", |
|
123 | " // Register the DatePickerView with the widget manager.\n", | |
108 |
" |
|
124 | " WidgetManager.register_widget_view('FilePickerView', FilePickerView);\n", | |
109 | "});" |
|
125 | "});" | |
110 | ], |
|
126 | ], | |
111 | "language": "python", |
|
127 | "language": "python", | |
112 | "metadata": {}, |
|
128 | "metadata": {}, | |
113 | "outputs": [ |
|
129 | "outputs": [ | |
114 | { |
|
130 | { | |
115 | "javascript": [ |
|
131 | "javascript": [ | |
116 | "\n", |
|
132 | "\n", | |
117 | "require([\"notebook/js/widget\"], function(){\n", |
|
133 | "require([\"notebook/js/widgets/widget\"], function(WidgetManager){\n", | |
118 |
" |
|
134 | "\n", | |
119 | " // Define the FileModel and register it with the widget manager.\n", |
|
|||
120 | " var FileModel = IPython.WidgetModel.extend({});\n", |
|
|||
121 | " IPython.widget_manager.register_widget_model('FileWidgetModel', FileModel);\n", |
|
|||
122 | " \n", |
|
|||
123 | " // Define the FilePickerView\n", |
|
|||
124 | " var FilePickerView = IPython.WidgetView.extend({\n", |
|
135 | " var FilePickerView = IPython.WidgetView.extend({\n", | |
125 | " \n", |
|
|||
126 | " render: function(){\n", |
|
136 | " render: function(){\n", | |
127 |
" |
|
137 | " // Render the view.\n", | |
128 |
" this. |
|
138 | " this.setElement($('<input />')\n", | |
129 | " .attr('type', 'file')\n", |
|
139 | " .attr('type', 'file'));\n", | |
130 | " .change(function(evt){ that.handleFileChange(evt) });\n", |
|
|||
131 | " },\n", |
|
140 | " },\n", | |
132 | " \n", |
|
141 | " \n", | |
133 |
" |
|
142 | " events: {\n", | |
134 | " handleFileChange: function(evt) { \n", |
|
143 | " // List of events and their handlers.\n", | |
|
144 | " 'change': 'handle_file_change',\n", | |||
|
145 | " },\n", | |||
|
146 | " \n", | |||
|
147 | " handle_file_change: function(evt) { \n", | |||
|
148 | " // Handle when the user has changed the file.\n", | |||
135 | " \n", |
|
149 | " \n", | |
136 | " //Retrieve the first (and only!) File from the FileList object\n", |
|
150 | " // Retrieve the first (and only!) File from the FileList object\n", | |
137 |
" var |
|
151 | " var file = evt.target.files[0];\n", | |
138 |
" |
|
152 | " if (file) {\n", | |
139 | " if (f) {\n", |
|
153 | "\n", | |
140 | " var r = new FileReader();\n", |
|
154 | " // Read the file's textual content and set value to those contents.\n", | |
141 |
" |
|
155 | " var that = this;\n", | |
|
156 | " var file_reader = new FileReader();\n", | |||
|
157 | " file_reader.onload = function(e) {\n", | |||
142 | " that.model.set('value', e.target.result);\n", |
|
158 | " that.model.set('value', e.target.result);\n", | |
143 |
" that. |
|
159 | " that.touch();\n", | |
144 | " }\n", |
|
160 | " }\n", | |
145 | " r.readAsText(f);\n", |
|
161 | " file_reader.readAsText(file);\n", | |
146 | " } else {\n", |
|
162 | " } else {\n", | |
147 | " this.model.set('on_failed', this.model.get('on_failed') + 1);\n", |
|
163 | "\n", | |
148 | " this.model.update_other_views(this);\n", |
|
164 | " // The file couldn't be opened. Send an error msg to the\n", | |
|
165 | " // back-end.\n", | |||
|
166 | " this.send({ 'event': 'error' });\n", | |||
149 | " }\n", |
|
167 | " }\n", | |
150 | " this.model.set('filename', f.name);\n", |
|
168 | "\n", | |
151 | " this.model.update_other_views(this);\n", |
|
169 | " // Set the filename of the file.\n", | |
|
170 | " this.model.set('filename', file.name);\n", | |||
|
171 | " this.touch();\n", | |||
152 | " },\n", |
|
172 | " },\n", | |
153 | " });\n", |
|
173 | " });\n", | |
154 | " \n", |
|
174 | " \n", | |
155 | " // Register the DatePickerView with the widget manager.\n", |
|
175 | " // Register the DatePickerView with the widget manager.\n", | |
156 |
" |
|
176 | " WidgetManager.register_widget_view('FilePickerView', FilePickerView);\n", | |
157 | "});" |
|
177 | "});" | |
158 | ], |
|
178 | ], | |
159 | "metadata": {}, |
|
179 | "metadata": {}, | |
160 | "output_type": "display_data", |
|
180 | "output_type": "display_data", | |
161 | "text": [ |
|
181 | "text": [ | |
162 |
"<IPython.core.display.Javascript at 0x |
|
182 | "<IPython.core.display.Javascript at 0x22870d0>" | |
163 | ] |
|
183 | ] | |
164 | } |
|
184 | } | |
165 | ], |
|
185 | ], | |
166 | "prompt_number": 3 |
|
186 | "prompt_number": 3 | |
167 | }, |
|
187 | }, | |
168 | { |
|
188 | { | |
169 |
"cell_type": " |
|
189 | "cell_type": "markdown", | |
170 | "level": 1, |
|
|||
171 | "metadata": {}, |
|
190 | "metadata": {}, | |
172 | "source": [ |
|
191 | "source": [ | |
173 | "Usage" |
|
192 | "The following shows how the file widget can be used." | |
174 | ] |
|
193 | ] | |
175 | }, |
|
194 | }, | |
176 | { |
|
195 | { | |
177 | "cell_type": "code", |
|
196 | "cell_type": "code", | |
178 | "collapsed": false, |
|
197 | "collapsed": false, | |
179 | "input": [ |
|
198 | "input": [ | |
180 | "file_widget = FileWidget()\n", |
|
199 | "file_widget = FileWidget()\n", | |
181 | "display(file_widget)\n", |
|
|||
182 | "\n", |
|
200 | "\n", | |
|
201 | "# Register an event to echo the filename when it has been changed.\n", | |||
183 | "def file_loading():\n", |
|
202 | "def file_loading():\n", | |
184 | " print(\"Loading %s\" % file_widget.filename)\n", |
|
203 | " print(\"Loading %s\" % file_widget.filename)\n", | |
|
204 | "file_widget.on_trait_change(file_loading, 'filename')\n", | |||
185 | "\n", |
|
205 | "\n", | |
|
206 | "# Register an event to echo the filename and contents when a file\n", | |||
|
207 | "# has been uploaded.\n", | |||
186 | "def file_loaded():\n", |
|
208 | "def file_loaded():\n", | |
187 | " print(\"Loaded, file contents: %s\" % file_widget.value)\n", |
|
209 | " print(\"Loaded, file contents: %s\" % file_widget.value)\n", | |
|
210 | "file_widget.on_trait_change(file_loaded, 'value')\n", | |||
188 | "\n", |
|
211 | "\n", | |
189 | "def file_failed(name, old_value, new_value):\n", |
|
212 | "# Register an event to print an error message when a file could not\n", | |
190 | " if new_value > old_value:\n", |
|
213 | "# be opened. Since the error messages are not handled through\n", | |
191 | " print(\"Could not load file contents of %s\" % file_widget.filename)\n", |
|
214 | "# traitlets but instead handled through custom msgs, the registration\n", | |
192 | "\n", |
|
215 | "# of the handler is different than the two examples above. Instead\n", | |
|
216 | "# the API provided by the CallbackDispatcher must be used.\n", | |||
|
217 | "def file_failed():\n", | |||
|
218 | " print(\"Could not load file contents of %s\" % file_widget.filename)\n", | |||
|
219 | "file_widget.errors.register_callback(file_failed)\n", | |||
193 | "\n", |
|
220 | "\n", | |
194 | "file_widget.on_trait_change(file_loading, 'filename')\n", |
|
221 | "file_widget" | |
195 | "file_widget.on_trait_change(file_loaded, 'value')\n", |
|
|||
196 | "file_widget.on_trait_change(file_failed, 'on_failed')" |
|
|||
197 | ], |
|
222 | ], | |
198 | "language": "python", |
|
223 | "language": "python", | |
199 | "metadata": {}, |
|
224 | "metadata": {}, | |
200 | "outputs": [ |
|
225 | "outputs": [ | |
201 | { |
|
226 | { | |
202 | "output_type": "stream", |
|
227 | "output_type": "stream", | |
203 | "stream": "stdout", |
|
228 | "stream": "stdout", | |
204 | "text": [ |
|
229 | "text": [ | |
205 | "Loading test.txt\n" |
|
230 | "Loading test.txt\n" | |
206 | ] |
|
231 | ] | |
207 | }, |
|
232 | }, | |
208 | { |
|
233 | { | |
209 | "output_type": "stream", |
|
234 | "output_type": "stream", | |
210 | "stream": "stdout", |
|
235 | "stream": "stdout", | |
211 | "text": [ |
|
236 | "text": [ | |
212 | "Loaded, file contents: Hello World!\n", |
|
237 | "Loaded, file contents: Hello World!\n", | |
213 | "\n" |
|
238 | "\n" | |
214 | ] |
|
239 | ] | |
215 | } |
|
240 | } | |
216 | ], |
|
241 | ], | |
217 | "prompt_number": 4 |
|
242 | "prompt_number": 4 | |
218 | } |
|
243 | } | |
219 | ], |
|
244 | ], | |
220 | "metadata": {} |
|
245 | "metadata": {} | |
221 | } |
|
246 | } | |
222 | ] |
|
247 | ] | |
223 | } No newline at end of file |
|
248 | } |
@@ -1,223 +1,227 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "name": "" |
|
3 | "name": "" | |
4 | }, |
|
4 | }, | |
5 | "nbformat": 3, |
|
5 | "nbformat": 3, | |
6 | "nbformat_minor": 0, |
|
6 | "nbformat_minor": 0, | |
7 | "worksheets": [ |
|
7 | "worksheets": [ | |
8 | { |
|
8 | { | |
9 | "cells": [ |
|
9 | "cells": [ | |
10 | { |
|
10 | { | |
11 | "cell_type": "code", |
|
11 | "cell_type": "code", | |
12 | "collapsed": false, |
|
12 | "collapsed": false, | |
13 | "input": [ |
|
13 | "input": [ | |
|
14 | "# Console related imports.\n", | |||
14 | "from subprocess import Popen, PIPE\n", |
|
15 | "from subprocess import Popen, PIPE\n", | |
15 | "import fcntl\n", |
|
16 | "import fcntl\n", | |
16 | "import os\n", |
|
17 | "import os\n", | |
|
18 | "from IPython.utils.py3compat import bytes_to_str, string_types\n", | |||
17 | "\n", |
|
19 | "\n", | |
|
20 | "# Widget related imports.\n", | |||
18 | "from IPython.html import widgets\n", |
|
21 | "from IPython.html import widgets\n", | |
19 |
"from IPython.display import display |
|
22 | "from IPython.display import display" | |
20 | "from IPython.utils.py3compat import bytes_to_str, string_types" |
|
|||
21 | ], |
|
23 | ], | |
22 | "language": "python", |
|
24 | "language": "python", | |
23 | "metadata": {}, |
|
25 | "metadata": {}, | |
24 | "outputs": [], |
|
26 | "outputs": [], | |
25 |
"prompt_number": |
|
27 | "prompt_number": 6 | |
26 | }, |
|
|||
27 | { |
|
|||
28 | "cell_type": "markdown", |
|
|||
29 | "metadata": {}, |
|
|||
30 | "source": [ |
|
|||
31 | "Create the output, input, and console toggle widgets." |
|
|||
32 | ] |
|
|||
33 | }, |
|
|||
34 | { |
|
|||
35 | "cell_type": "code", |
|
|||
36 | "collapsed": false, |
|
|||
37 | "input": [ |
|
|||
38 | "console_container = widgets.ContainerWidget(visible=False)\n", |
|
|||
39 | "console_container.set_css('padding', '10px')\n", |
|
|||
40 | "\n", |
|
|||
41 | "console_style = {\n", |
|
|||
42 | " 'font-family': 'monospace',\n", |
|
|||
43 | " 'color': '#AAAAAA',\n", |
|
|||
44 | " 'background': 'black',\n", |
|
|||
45 | " 'width': '800px',\n", |
|
|||
46 | "}\n", |
|
|||
47 | "\n", |
|
|||
48 | "output_box = widgets.StringWidget(parent=console_container, default_view_name='TextAreaView')\n", |
|
|||
49 | "output_box.set_css(console_style)\n", |
|
|||
50 | "output_box.set_css('height', '400px')\n", |
|
|||
51 | "\n", |
|
|||
52 | "input_box = widgets.StringWidget(parent=console_container)\n", |
|
|||
53 | "input_box.set_css(console_style)\n", |
|
|||
54 | "\n", |
|
|||
55 | "toggle_button = widgets.ButtonWidget(description=\"Start Console\")\n", |
|
|||
56 | "def toggle_console():\n", |
|
|||
57 | " console_container.visible = not console_container.visible\n", |
|
|||
58 | " if console_container.visible:\n", |
|
|||
59 | " toggle_button.description=\"Stop Console\"\n", |
|
|||
60 | " input_box.disabled = False\n", |
|
|||
61 | " else:\n", |
|
|||
62 | " toggle_button.description=\"Start Console\"\n", |
|
|||
63 | "toggle_button.on_click(toggle_console)\n" |
|
|||
64 | ], |
|
|||
65 | "language": "python", |
|
|||
66 | "metadata": {}, |
|
|||
67 | "outputs": [], |
|
|||
68 | "prompt_number": 2 |
|
|||
69 | }, |
|
28 | }, | |
70 | { |
|
29 | { | |
71 | "cell_type": "markdown", |
|
30 | "cell_type": "markdown", | |
72 | "metadata": {}, |
|
31 | "metadata": {}, | |
73 | "source": [ |
|
32 | "source": [ | |
74 | "Define function to run a process without blocking the input." |
|
33 | "Define function to run a process without blocking the input." | |
75 | ] |
|
34 | ] | |
76 | }, |
|
35 | }, | |
77 | { |
|
36 | { | |
78 | "cell_type": "code", |
|
37 | "cell_type": "code", | |
79 | "collapsed": false, |
|
38 | "collapsed": false, | |
80 | "input": [ |
|
39 | "input": [ | |
81 | "def read_process(process, append_output):\n", |
|
40 | "def read_process(process, append_output):\n", | |
82 | " \"\"\" Try to read the stdout and stderr of a process and render it using \n", |
|
41 | " \"\"\" Try to read the stdout and stderr of a process and render it using \n", | |
83 | " the append_output method provided\n", |
|
42 | " the append_output method provided\n", | |
84 | " \n", |
|
43 | " \n", | |
85 | " Parameters\n", |
|
44 | " Parameters\n", | |
86 | " ----------\n", |
|
45 | " ----------\n", | |
87 | " process: Popen handle\n", |
|
46 | " process: Popen handle\n", | |
88 | " append_output: method handle\n", |
|
47 | " append_output: method handle\n", | |
89 | " Callback to render output. Signature of\n", |
|
48 | " Callback to render output. Signature of\n", | |
90 | " append_output(output, [prefix=])\"\"\"\n", |
|
49 | " append_output(output, [prefix=])\"\"\"\n", | |
91 | " \n", |
|
50 | " \n", | |
92 | " try:\n", |
|
51 | " try:\n", | |
93 | " stdout = process.stdout.read()\n", |
|
52 | " stdout = process.stdout.read()\n", | |
94 | " if stdout is not None and len(stdout) > 0:\n", |
|
53 | " if stdout is not None and len(stdout) > 0:\n", | |
95 | " append_output(stdout, prefix=' ')\n", |
|
54 | " append_output(stdout, prefix=' ')\n", | |
96 | " except:\n", |
|
55 | " except:\n", | |
97 | " pass\n", |
|
56 | " pass\n", | |
98 | " \n", |
|
57 | " \n", | |
99 | " try:\n", |
|
58 | " try:\n", | |
100 | " stderr = process.stderr.read()\n", |
|
59 | " stderr = process.stderr.read()\n", | |
101 | " if stderr is not None and len(stderr) > 0:\n", |
|
60 | " if stderr is not None and len(stderr) > 0:\n", | |
102 | " append_output(stderr, prefix='ERR ')\n", |
|
61 | " append_output(stderr, prefix='ERR ')\n", | |
103 | " except:\n", |
|
62 | " except:\n", | |
104 | " pass\n", |
|
63 | " pass\n", | |
105 | "\n", |
|
64 | "\n", | |
106 | "\n", |
|
65 | "\n", | |
107 | "def set_pipe_nonblocking(pipe):\n", |
|
66 | "def set_pipe_nonblocking(pipe):\n", | |
108 | " \"\"\"Set a pipe as non-blocking\"\"\"\n", |
|
67 | " \"\"\"Set a pipe as non-blocking\"\"\"\n", | |
109 | " fl = fcntl.fcntl(pipe, fcntl.F_GETFL)\n", |
|
68 | " fl = fcntl.fcntl(pipe, fcntl.F_GETFL)\n", | |
110 | " fcntl.fcntl(pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK)\n", |
|
69 | " fcntl.fcntl(pipe, fcntl.F_SETFL, fl | os.O_NONBLOCK)\n", | |
111 | "\n", |
|
70 | "\n", | |
112 | "\n", |
|
71 | "\n", | |
113 | "kernel = get_ipython().kernel\n", |
|
72 | "kernel = get_ipython().kernel\n", | |
114 | "def run_command(command, append_output, has_user_exited=None):\n", |
|
73 | "def run_command(command, append_output, has_user_exited=None):\n", | |
115 | " \"\"\"Run a command asyncronously\n", |
|
74 | " \"\"\"Run a command asyncronously\n", | |
116 | " \n", |
|
75 | " \n", | |
117 | " Parameters\n", |
|
76 | " Parameters\n", | |
118 | " ----------\n", |
|
77 | " ----------\n", | |
119 | " command: str\n", |
|
78 | " command: str\n", | |
120 | " Shell command to launch a process with.\n", |
|
79 | " Shell command to launch a process with.\n", | |
121 | " append_output: method handle\n", |
|
80 | " append_output: method handle\n", | |
122 | " Callback to render output. Signature of\n", |
|
81 | " Callback to render output. Signature of\n", | |
123 | " append_output(output, [prefix=])\n", |
|
82 | " append_output(output, [prefix=])\n", | |
124 | " has_user_exited: method handle\n", |
|
83 | " has_user_exited: method handle\n", | |
125 | " Check to see if the user wants to stop the command.\n", |
|
84 | " Check to see if the user wants to stop the command.\n", | |
126 | " Must return a boolean.\"\"\"\n", |
|
85 | " Must return a boolean.\"\"\"\n", | |
127 | " \n", |
|
86 | " \n", | |
128 | " # Echo input.\n", |
|
87 | " # Echo input.\n", | |
129 | " append_output(command, prefix='>>> ')\n", |
|
88 | " append_output(command, prefix='>>> ')\n", | |
130 | " \n", |
|
89 | " \n", | |
131 | " # Create the process. Make sure the pipes are set as non-blocking.\n", |
|
90 | " # Create the process. Make sure the pipes are set as non-blocking.\n", | |
132 | " process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\n", |
|
91 | " process = Popen(command, shell=True, stdout=PIPE, stderr=PIPE)\n", | |
133 | " set_pipe_nonblocking(process.stdout)\n", |
|
92 | " set_pipe_nonblocking(process.stdout)\n", | |
134 | " set_pipe_nonblocking(process.stderr)\n", |
|
93 | " set_pipe_nonblocking(process.stderr)\n", | |
135 | " \n", |
|
94 | " \n", | |
136 | " # Only continue to read from the command \n", |
|
95 | " # Only continue to read from the command \n", | |
137 | " while (has_user_exited is None or not has_user_exited()) and process.poll() is None:\n", |
|
96 | " while (has_user_exited is None or not has_user_exited()) and process.poll() is None:\n", | |
138 | " read_process(process, append_output)\n", |
|
97 | " read_process(process, append_output)\n", | |
139 | " kernel.do_one_iteration() # Run IPython iteration. This is the code that\n", |
|
98 | " kernel.do_one_iteration() # Run IPython iteration. This is the code that\n", | |
140 | " # makes this operation non-blocking. This will\n", |
|
99 | " # makes this operation non-blocking. This will\n", | |
141 | " # allow widget messages and callbacks to be \n", |
|
100 | " # allow widget messages and callbacks to be \n", | |
142 | " # processed.\n", |
|
101 | " # processed.\n", | |
143 | " \n", |
|
102 | " \n", | |
144 | " # If the process is still running, the user must have exited.\n", |
|
103 | " # If the process is still running, the user must have exited.\n", | |
145 | " if process.poll() is None:\n", |
|
104 | " if process.poll() is None:\n", | |
146 | " process.kill()\n", |
|
105 | " process.kill()\n", | |
147 | " else:\n", |
|
106 | " else:\n", | |
148 | " read_process(process, append_output) # Read remainer\n", |
|
107 | " read_process(process, append_output) # Read remainer\n", | |
149 | " \n", |
|
108 | " \n", | |
150 | " \n", |
|
109 | " \n", | |
151 | " \n", |
|
110 | " \n", | |
152 | " " |
|
111 | " " | |
153 | ], |
|
112 | ], | |
154 | "language": "python", |
|
113 | "language": "python", | |
155 | "metadata": {}, |
|
114 | "metadata": {}, | |
156 | "outputs": [], |
|
115 | "outputs": [], | |
157 |
"prompt_number": |
|
116 | "prompt_number": 7 | |
|
117 | }, | |||
|
118 | { | |||
|
119 | "cell_type": "markdown", | |||
|
120 | "metadata": {}, | |||
|
121 | "source": [ | |||
|
122 | "Create the console widgets without displaying them." | |||
|
123 | ] | |||
|
124 | }, | |||
|
125 | { | |||
|
126 | "cell_type": "code", | |||
|
127 | "collapsed": false, | |||
|
128 | "input": [ | |||
|
129 | "console_container = widgets.ContainerWidget(visible=False)\n", | |||
|
130 | "console_container.set_css('padding', '10px')\n", | |||
|
131 | "\n", | |||
|
132 | "console_style = {\n", | |||
|
133 | " 'font-family': 'monospace',\n", | |||
|
134 | " 'color': '#AAAAAA',\n", | |||
|
135 | " 'background': 'black',\n", | |||
|
136 | " 'width': '800px',\n", | |||
|
137 | "}\n", | |||
|
138 | "\n", | |||
|
139 | "output_box = widgets.TextAreaWidget()\n", | |||
|
140 | "output_box.set_css(console_style)\n", | |||
|
141 | "output_box.set_css('height', '400px')\n", | |||
|
142 | "\n", | |||
|
143 | "input_box = widgets.TextBoxWidget()\n", | |||
|
144 | "input_box.set_css(console_style)\n", | |||
|
145 | "\n", | |||
|
146 | "console_container.children = [output_box, input_box]" | |||
|
147 | ], | |||
|
148 | "language": "python", | |||
|
149 | "metadata": {}, | |||
|
150 | "outputs": [], | |||
|
151 | "prompt_number": 8 | |||
158 | }, |
|
152 | }, | |
159 | { |
|
153 | { | |
160 | "cell_type": "markdown", |
|
154 | "cell_type": "markdown", | |
161 | "metadata": {}, |
|
155 | "metadata": {}, | |
162 | "source": [ |
|
156 | "source": [ | |
163 | "Hook the process execution methods up to our console widgets." |
|
157 | "Hook the process execution methods up to our console widgets." | |
164 | ] |
|
158 | ] | |
165 | }, |
|
159 | }, | |
166 | { |
|
160 | { | |
167 | "cell_type": "code", |
|
161 | "cell_type": "code", | |
168 | "collapsed": false, |
|
162 | "collapsed": false, | |
169 | "input": [ |
|
163 | "input": [ | |
170 | "\n", |
|
164 | "\n", | |
171 | "def append_output(output, prefix):\n", |
|
165 | "def append_output(output, prefix):\n", | |
172 | " if isinstance(output, string_types):\n", |
|
166 | " if isinstance(output, string_types):\n", | |
173 | " output_str = output\n", |
|
167 | " output_str = output\n", | |
174 | " else:\n", |
|
168 | " else:\n", | |
175 | " output_str = bytes_to_str(output)\n", |
|
169 | " output_str = bytes_to_str(output)\n", | |
176 | " output_lines = output_str.split('\\n')\n", |
|
170 | " output_lines = output_str.split('\\n')\n", | |
177 | " formatted_output = '\\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\\n'\n", |
|
171 | " formatted_output = '\\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\\n'\n", | |
178 | " output_box.value += formatted_output\n", |
|
172 | " output_box.value += formatted_output\n", | |
179 | " output_box.scroll_to_bottom()\n", |
|
173 | " output_box.scroll_to_bottom()\n", | |
180 | " \n", |
|
174 | " \n", | |
181 | "def has_user_exited():\n", |
|
175 | "def has_user_exited():\n", | |
182 | " return not console_container.visible\n", |
|
176 | " return not console_container.visible\n", | |
183 | "\n", |
|
177 | "\n", | |
184 | "def handle_input(sender):\n", |
|
178 | "def handle_input(sender):\n", | |
185 | " sender.disabled = True\n", |
|
179 | " sender.disabled = True\n", | |
186 | " try:\n", |
|
180 | " try:\n", | |
187 | " command = sender.value\n", |
|
181 | " command = sender.value\n", | |
188 | " sender.value = ''\n", |
|
182 | " sender.value = ''\n", | |
189 | " run_command(command, append_output=append_output, has_user_exited=has_user_exited)\n", |
|
183 | " run_command(command, append_output=append_output, has_user_exited=has_user_exited)\n", | |
190 | " finally:\n", |
|
184 | " finally:\n", | |
191 | " sender.disabled = False\n", |
|
185 | " sender.disabled = False\n", | |
192 | " \n", |
|
186 | " \n", | |
193 | "input_box.on_submit(handle_input)" |
|
187 | "input_box.on_submit(handle_input)" | |
194 | ], |
|
188 | ], | |
195 | "language": "python", |
|
189 | "language": "python", | |
196 | "metadata": {}, |
|
190 | "metadata": {}, | |
197 | "outputs": [], |
|
191 | "outputs": [], | |
198 |
"prompt_number": |
|
192 | "prompt_number": 9 | |
199 | }, |
|
193 | }, | |
200 | { |
|
194 | { | |
201 | "cell_type": "markdown", |
|
195 | "cell_type": "markdown", | |
202 | "metadata": {}, |
|
196 | "metadata": {}, | |
203 | "source": [ |
|
197 | "source": [ | |
204 | "Show the console" |
|
198 | "Create the button that will be used to display and hide the console. Display both the console container and the new button used to toggle it." | |
205 | ] |
|
199 | ] | |
206 | }, |
|
200 | }, | |
207 | { |
|
201 | { | |
208 | "cell_type": "code", |
|
202 | "cell_type": "code", | |
209 | "collapsed": false, |
|
203 | "collapsed": false, | |
210 | "input": [ |
|
204 | "input": [ | |
|
205 | "toggle_button = widgets.ButtonWidget(description=\"Start Console\")\n", | |||
|
206 | "def toggle_console():\n", | |||
|
207 | " console_container.visible = not console_container.visible\n", | |||
|
208 | " if console_container.visible:\n", | |||
|
209 | " toggle_button.description=\"Stop Console\"\n", | |||
|
210 | " input_box.disabled = False\n", | |||
|
211 | " else:\n", | |||
|
212 | " toggle_button.description=\"Start Console\"\n", | |||
|
213 | "toggle_button.on_click(toggle_console)\n", | |||
|
214 | "\n", | |||
211 | "display(toggle_button)\n", |
|
215 | "display(toggle_button)\n", | |
212 | "display(console_container)" |
|
216 | "display(console_container)" | |
213 | ], |
|
217 | ], | |
214 | "language": "python", |
|
218 | "language": "python", | |
215 | "metadata": {}, |
|
219 | "metadata": {}, | |
216 | "outputs": [], |
|
220 | "outputs": [], | |
217 |
"prompt_number": |
|
221 | "prompt_number": 10 | |
218 | } |
|
222 | } | |
219 | ], |
|
223 | ], | |
220 | "metadata": {} |
|
224 | "metadata": {} | |
221 | } |
|
225 | } | |
222 | ] |
|
226 | ] | |
223 | } No newline at end of file |
|
227 | } |
@@ -1,70 +1,69 | |||||
1 | { |
|
1 | { | |
2 | "metadata": { |
|
2 | "metadata": { | |
3 | "name": "" |
|
3 | "name": "" | |
4 | }, |
|
4 | }, | |
5 | "nbformat": 3, |
|
5 | "nbformat": 3, | |
6 | "nbformat_minor": 0, |
|
6 | "nbformat_minor": 0, | |
7 | "worksheets": [ |
|
7 | "worksheets": [ | |
8 | { |
|
8 | { | |
9 | "cells": [ |
|
9 | "cells": [ | |
10 | { |
|
10 | { | |
11 | "cell_type": "heading", |
|
11 | "cell_type": "heading", | |
12 | "level": 1, |
|
12 | "level": 1, | |
13 | "metadata": {}, |
|
13 | "metadata": {}, | |
14 | "source": [ |
|
14 | "source": [ | |
15 | "Widgets" |
|
15 | "Widgets" | |
16 | ] |
|
16 | ] | |
17 | }, |
|
17 | }, | |
18 | { |
|
18 | { | |
19 | "cell_type": "markdown", |
|
19 | "cell_type": "markdown", | |
20 | "metadata": {}, |
|
20 | "metadata": {}, | |
21 | "source": [ |
|
21 | "source": [ | |
22 | "This directory includes a tutorial and collection of examples related to the IPython notebook widget framework." |
|
22 | "This directory includes a tutorial and collection of examples related to the IPython notebook widget framework." | |
23 | ] |
|
23 | ] | |
24 | }, |
|
24 | }, | |
25 | { |
|
25 | { | |
26 | "cell_type": "heading", |
|
26 | "cell_type": "heading", | |
27 | "level": 2, |
|
27 | "level": 2, | |
28 | "metadata": {}, |
|
28 | "metadata": {}, | |
29 | "source": [ |
|
29 | "source": [ | |
30 | "Tutorial" |
|
30 | "Tutorial" | |
31 | ] |
|
31 | ] | |
32 | }, |
|
32 | }, | |
33 | { |
|
33 | { | |
34 | "cell_type": "markdown", |
|
34 | "cell_type": "markdown", | |
35 | "metadata": {}, |
|
35 | "metadata": {}, | |
36 | "source": [ |
|
36 | "source": [ | |
37 | "- [Part 1 - Basics](Part 1 - Basics.ipynb) \n", |
|
37 | "- [Part 1 - Basics](Part 1 - Basics.ipynb) \n", | |
38 | "- [Part 2 - Events](Part 2 - Events.ipynb) \n", |
|
38 | "- [Part 2 - Events](Part 2 - Events.ipynb) \n", | |
39 | "- [Part 3 - Placement](Part 3 - Placement.ipynb) \n", |
|
39 | "- [Part 3 - Placement](Part 3 - Placement.ipynb) \n", | |
40 | "- [Part 4 - Styles](Part 4 - Styles.ipynb) \n", |
|
40 | "- [Part 4 - Styles](Part 4 - Styles.ipynb) \n", | |
41 | "- [Part 5 - Alignment](Part 5 - Alignment.ipynb) \n", |
|
41 | "- [Part 5 - Alignment](Part 5 - Alignment.ipynb) \n", | |
42 | "- [Part 6 - Custom Widget](Part 6 - Custom Widget.ipynb) " |
|
42 | "- [Part 6 - Custom Widget](Part 6 - Custom Widget.ipynb) " | |
43 | ] |
|
43 | ] | |
44 | }, |
|
44 | }, | |
45 | { |
|
45 | { | |
46 | "cell_type": "heading", |
|
46 | "cell_type": "heading", | |
47 | "level": 2, |
|
47 | "level": 2, | |
48 | "metadata": {}, |
|
48 | "metadata": {}, | |
49 | "source": [ |
|
49 | "source": [ | |
50 | "Examples" |
|
50 | "Examples" | |
51 | ] |
|
51 | ] | |
52 | }, |
|
52 | }, | |
53 | { |
|
53 | { | |
54 | "cell_type": "markdown", |
|
54 | "cell_type": "markdown", | |
55 | "metadata": {}, |
|
55 | "metadata": {}, | |
56 | "source": [ |
|
56 | "source": [ | |
57 | "- [Widget Tester](Widget Tester.ipynb) \n", |
|
57 | "- [Widget Tester](Widget Tester.ipynb) \n", | |
58 | "- [Variable Inspector](Variable Inspector.ipynb) \n", |
|
58 | "- [Variable Inspector](Variable Inspector.ipynb) \n", | |
59 | "- [Export As (nbconvert)](Export As (nbconvert%29.ipynb) \n", |
|
59 | "- [Export As (nbconvert)](Export As (nbconvert%29.ipynb) \n", | |
60 | "- [Nonblocking Console](Nonblocking Console.ipynb) \n", |
|
60 | "- [Nonblocking Console](Nonblocking Console.ipynb) \n", | |
61 | "- [D3](D3.ipynb) \n", |
|
61 | "- [D3](D3.ipynb) \n", | |
62 |
"- [File Upload Widget](File Upload Widget.ipynb) |
|
62 | "- [File Upload Widget](File Upload Widget.ipynb) " | |
63 | "- [Dialogs](Dialogs.ipynb) " |
|
|||
64 | ] |
|
63 | ] | |
65 | } |
|
64 | } | |
66 | ], |
|
65 | ], | |
67 | "metadata": {} |
|
66 | "metadata": {} | |
68 | } |
|
67 | } | |
69 | ] |
|
68 | ] | |
70 | } No newline at end of file |
|
69 | } |
General Comments 0
You need to be logged in to leave comments.
Login now