Widget examples Python 3.x compatability.

This commit is contained in:
Jonathan Frederic 2013-11-25 11:43:37 -08:00 committed by Jonathan Frederic
parent e36353c152
commit 2d536f64f5
5 changed files with 95 additions and 171 deletions

View File

@ -17,6 +17,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import print_function # py 2.7 compat\n",
"\n",
"from IPython.html import widgets # Widget definitions\n",
"from IPython.display import display # Used to display widgets in the notebook"
],
@ -156,7 +158,7 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x319fe90>"
"<IPython.core.display.Javascript at 0x7f14f1c78cd0>"
]
}
],
@ -178,14 +180,14 @@
"display(file_widget)\n",
"\n",
"def file_loading():\n",
" print \"Loading %s\" % file_widget.filename\n",
" print(\"Loading %s\" % file_widget.filename)\n",
"\n",
"def file_loaded():\n",
" print \"Loaded, file contents: %s\" % file_widget.value\n",
" print(\"Loaded, file contents: %s\" % file_widget.value)\n",
"\n",
"def file_failed(name, old_value, new_value):\n",
" if new_value > old_value:\n",
" print \"Could not load file contents of %s\" % file_widget.filename\n",
" print(\"Could not load file contents of %s\" % file_widget.filename)\n",
"\n",
"\n",
"file_widget.on_trait_change(file_loading, 'filename')\n",
@ -206,8 +208,8 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"Loaded, file contents: \n",
"hello world!\n"
"Loaded, file contents: Hello World!\n",
"\n"
]
}
],

View File

@ -16,7 +16,8 @@
"import os\n",
"\n",
"from IPython.html import widgets\n",
"from IPython.display import display"
"from IPython.display import display\n",
"from IPython.utils.py3compat import bytes_to_str, string_types"
],
"language": "python",
"metadata": {},
@ -91,7 +92,7 @@
" try:\n",
" stdout = process.stdout.read()\n",
" if stdout is not None and len(stdout) > 0:\n",
" append_output(stdout)\n",
" append_output(stdout, prefix=' ')\n",
" except:\n",
" pass\n",
" \n",
@ -166,8 +167,13 @@
"cell_type": "code",
"collapsed": false,
"input": [
"def append_output(output, prefix=' '):\n",
" output_lines = output.split('\\n')\n",
"\n",
"def append_output(output, prefix):\n",
" if isinstance(output, string_types):\n",
" output_str = output\n",
" else:\n",
" output_str = bytes_to_str(output)\n",
" output_lines = output_str.split('\\n')\n",
" formatted_output = '\\n'.join([prefix + line for line in output_lines if len(line) > 0]) + '\\n'\n",
" output_box.value += formatted_output\n",
" output_box.scroll_to_bottom()\n",

View File

@ -17,6 +17,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import print_function # 2.7 compatability\n",
"\n",
"from IPython.html import widgets # Widget definitions\n",
"from IPython.display import display # Used to display widgets in the notebook"
],
@ -104,7 +106,7 @@
"display(intrange)\n",
"\n",
"def on_value_change(name, value):\n",
" print value\n",
" print(value)\n",
"\n",
"intrange.on_trait_change(on_value_change, 'value')"
],
@ -115,21 +117,21 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"25\n"
"28\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"73\n"
"55\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"99\n"
"94\n"
]
}
],

View File

@ -29,6 +29,8 @@
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import print_function # For py 2.7 compat\n",
"\n",
"from IPython.html import widgets # Widget definitions\n",
"from IPython.display import display # Used to display widgets in the notebook"
],
@ -37,6 +39,25 @@
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The 3 part of this tutorial requires the 3rd party `dateutil` library. https://pypi.python.org/pypi/python-dateutil"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Import the dateutil library to parse date strings.\n",
"from dateutil import parser"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 1,
@ -91,7 +112,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
"prompt_number": 3
},
{
"cell_type": "markdown",
@ -139,11 +160,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21f8f10>"
"<IPython.core.display.Javascript at 0x7f8c679289d0>"
]
}
],
"prompt_number": 3
"prompt_number": 4
},
{
"cell_type": "markdown",
@ -181,11 +202,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21f8ed0>"
"<IPython.core.display.Javascript at 0x7f8c67928510>"
]
}
],
"prompt_number": 4
"prompt_number": 5
},
{
"cell_type": "markdown",
@ -249,11 +270,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21f8cd0>"
"<IPython.core.display.Javascript at 0x7f8c67928a10>"
]
}
],
"prompt_number": 5
"prompt_number": 6
},
{
"cell_type": "heading",
@ -280,7 +301,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
"prompt_number": 7
},
{
"cell_type": "heading",
@ -325,7 +346,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
"prompt_number": 8
},
{
"cell_type": "heading",
@ -405,11 +426,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21fc310>"
"<IPython.core.display.Javascript at 0x7f8c67928590>"
]
}
],
"prompt_number": 8
"prompt_number": 9
},
{
"cell_type": "markdown",
@ -495,11 +516,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21fc290>"
"<IPython.core.display.Javascript at 0x7f8c648ce8d0>"
]
}
],
"prompt_number": 9
"prompt_number": 10
},
{
"cell_type": "markdown",
@ -613,11 +634,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x21fc3d0>"
"<IPython.core.display.Javascript at 0x7f8c648ce150>"
]
}
],
"prompt_number": 10
"prompt_number": 11
},
{
"cell_type": "heading",
@ -644,7 +665,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
"prompt_number": 12
},
{
"cell_type": "markdown",
@ -662,7 +683,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 12
"prompt_number": 13
},
{
"cell_type": "markdown",
@ -683,13 +704,13 @@
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 13,
"prompt_number": 14,
"text": [
"u'2013-11-14'"
"'2013-11-28'"
]
}
],
"prompt_number": 13
"prompt_number": 14
},
{
"cell_type": "markdown",
@ -707,7 +728,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
"prompt_number": 15
},
{
"cell_type": "heading",
@ -765,7 +786,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
"prompt_number": 16
},
{
"cell_type": "markdown",
@ -778,8 +799,6 @@
"cell_type": "code",
"collapsed": false,
"input": [
"# Import the dateutil library to parse date strings.\n",
"from dateutil import parser\n",
"\n",
"# Import the base Widget class and the traitlets Unicode class.\n",
"from IPython.html.widgets import Widget\n",
@ -812,7 +831,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
"prompt_number": 17
},
{
"cell_type": "markdown",
@ -860,7 +879,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
"prompt_number": 18
},
{
"cell_type": "markdown",
@ -938,7 +957,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
"prompt_number": 19
},
{
"cell_type": "heading",
@ -1094,11 +1113,11 @@
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x221a850>"
"<IPython.core.display.Javascript at 0x7f8c679134d0>"
]
}
],
"prompt_number": 19
"prompt_number": 20
},
{
"cell_type": "heading",
@ -1129,7 +1148,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
"prompt_number": 21
},
{
"cell_type": "markdown",
@ -1147,7 +1166,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
"prompt_number": 22
},
{
"cell_type": "markdown",
@ -1170,7 +1189,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
"prompt_number": 23
},
{
"cell_type": "code",
@ -1182,7 +1201,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
"prompt_number": 24
},
{
"cell_type": "code",
@ -1194,7 +1213,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 24
"prompt_number": 25
}
],
"metadata": {}

View File

@ -28,103 +28,6 @@
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Custom SidePanel View"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%javascript\n",
"\n",
"require([\"notebook/js/widget\"], function(){\n",
"\n",
" // Define the FilePickerView\n",
" var SidePanelView = IPython.WidgetView.extend({\n",
" \n",
" render: function(){ \n",
" var table_div = $('<div />', {id: 'var_inspect'})\n",
" .addClass('hbox');\n",
" var body_div = $('<div />')\n",
" .css('width', '80%')\n",
" .css('height', '100%')\n",
" .appendTo(table_div);\n",
" this.panel_div = $('<div />')\n",
" .css('width', '20%')\n",
" .css('height', '100%')\n",
" .appendTo(table_div);\n",
" \n",
" var body = $('body');\n",
" var site = body.find('#site');\n",
" site.detach();\n",
" body.find('#var_inspect').remove();\n",
" body.append(table_div);\n",
" site.appendTo(body_div);\n",
" },\n",
"\n",
" display_child: function(view) {\n",
" this.panel_div.append(view.$el);\n",
" },\n",
" });\n",
" \n",
" // Register the DatePickerView with the widget manager.\n",
" IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n",
"});"
],
"language": "python",
"metadata": {},
"outputs": [
{
"javascript": [
"\n",
"require([\"notebook/js/widget\"], function(){\n",
"\n",
" // Define the FilePickerView\n",
" var SidePanelView = IPython.WidgetView.extend({\n",
" \n",
" render: function(){ \n",
" var table_div = $('<div />', {id: 'var_inspect'})\n",
" .addClass('hbox');\n",
" var body_div = $('<div />')\n",
" .css('width', '80%')\n",
" .css('height', '100%')\n",
" .appendTo(table_div);\n",
" this.panel_div = $('<div />')\n",
" .css('width', '20%')\n",
" .css('height', '100%')\n",
" .appendTo(table_div);\n",
" \n",
" var body = $('body');\n",
" var site = body.find('#site');\n",
" site.detach();\n",
" body.find('#var_inspect').remove();\n",
" body.append(table_div);\n",
" site.appendTo(body_div);\n",
" },\n",
"\n",
" display_child: function(view) {\n",
" this.panel_div.append(view.$el);\n",
" },\n",
" });\n",
" \n",
" // Register the DatePickerView with the widget manager.\n",
" IPython.widget_manager.register_widget_view('SidePanelView', SidePanelView);\n",
"});"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.Javascript at 0x7f4e70015050>"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
@ -137,39 +40,31 @@
"cell_type": "code",
"collapsed": false,
"input": [
"_side_panel = widgets.ContainerWidget(default_view_name=\"SidePanelView\")\n",
"_popout = widgets.ContainerWidget(default_view_name=\"ModalView\")\n",
"_popout.description = \"Variable Inspector\"\n",
"_popout.button_text = _popout.description\n",
"_popout.vbox()\n",
"\n",
"_modal_div = widgets.ContainerWidget(parent=_side_panel)\n",
"_modal_div.set_css({'padding-top': '60px',\n",
" 'padding-right': '40px',\n",
" 'padding-left': '10px',})\n",
"\n",
"_modal_header = widgets.ContainerWidget(parent=_modal_div)\n",
"_modal_header_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n",
"_modal_header_label.value = '<h3>Variable Inspector</h3>'\n",
"_modal_header_execs_label = widgets.StringWidget(parent=_modal_header, default_view_name=\"LabelView\")\n",
"_modal_header_execs_label = widgets.StringWidget(parent=_popout, default_view_name=\"LabelView\")\n",
"_modal_header_execs_label.execs = 0\n",
"\n",
"_modal_body = widgets.ContainerWidget(parent=_modal_div)\n",
"_modal_body.vbox()\n",
"\n",
"_modal_body = widgets.ContainerWidget(parent=_popout)\n",
"_modal_body.flex1()\n",
"_modal_body.set_css('overflow-y', 'scroll')\n",
"_modal_body_label = widgets.StringWidget(parent=_modal_body, default_view_name=\"LabelView\")\n",
"_modal_body_label.value = 'Not hooked'\n",
"\n",
"_modal_footer = widgets.ContainerWidget(parent=_modal_div)\n",
"_modal_footer.vbox()\n",
"_modal_footer = widgets.ContainerWidget(parent=_popout)\n",
"_var_filter = widgets.SelectionWidget(values=['Public', 'Private', 'IPython'], parent=_modal_footer, value='Public', default_view_name='ToggleButtonsView')\n",
"\n",
"display(_side_panel)\n",
"display(_popout)\n",
"\n",
"_modal_header.add_class('modal-header')\n",
"_modal_body.add_class('modal-body')\n",
"_modal_footer.add_class('modal-footer')\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
"prompt_number": 2
},
{
"cell_type": "heading",
@ -248,7 +143,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
"prompt_number": 3
},
{
"cell_type": "heading",
@ -282,7 +177,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
"prompt_number": 4
},
{
"cell_type": "heading",
@ -301,7 +196,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
"prompt_number": 5
},
{
"cell_type": "code",
@ -312,7 +207,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
"prompt_number": 6
},
{
"cell_type": "code",
@ -323,7 +218,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
"prompt_number": 7
},
{
"cell_type": "code",
@ -334,7 +229,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
"prompt_number": 8
},
{
"cell_type": "code",
@ -345,7 +240,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
"prompt_number": 9
}
],
"metadata": {}