Skip to content

Commit

Permalink
make it working for new cookiecutter
Browse files Browse the repository at this point in the history
  • Loading branch information
dou-du committed Mar 9, 2020
1 parent 9445c02 commit 603f5d7
Show file tree
Hide file tree
Showing 2 changed files with 243 additions and 13 deletions.
254 changes: 242 additions & 12 deletions examples/introduction.ipynb
Original file line number Diff line number Diff line change
@@ -1,37 +1,267 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from widget_periodictable import PTableWidget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction"
"# Visualize the element grid"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8f63d7a55d1d4cd2b2b3c3f8cae6cbc7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"PTableWidget()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Show the widget\n",
"widget = PTableWidget()\n",
"widget"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get the selected values in python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"import widget_periodictable"
"Check which elements are currently selected"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "48788e1c42414f42b24a1aef2dbea529",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(Button(button_style='success', description='Get the currently selected values', layout=Layout(w…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"output = widgets.Output()\n",
"\n",
"def on_get_in_python(event):\n",
" output.clear_output()\n",
" with output:\n",
" print(\n",
" \"Currently selected values:\", \n",
" widget.selected_elements)\n",
"\n",
"button2 = widgets.Button(\n",
" description=\"Get the currently selected values\", \n",
" button_style='success',\n",
" layout={'width': '300px'}\n",
")\n",
"button2.on_click(on_get_in_python)\n",
"vbox = widgets.VBox([button2, output])\n",
"vbox"
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"w = widget_periodictable.ExampleWidget()"
"# Play with enabling/disabling some elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5b4247f84a3b46e2a784cb12951b05dc",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Checkbox(value=False, description='Disable oxygen')"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"toggle_disabled = widgets.Checkbox(\n",
" value=\"O\" in widget.disabled_elements,\n",
" description='Disable oxygen',\n",
" disabled=False\n",
")\n",
"\n",
"def on_change_disabled(event):\n",
" if toggle_disabled.value:\n",
" # It's set, meaning we want to disable oxygen\n",
" widget.disabled_elements = [\"O\"]\n",
" else:\n",
" widget.disabled_elements = []\n",
"toggle_disabled.observe(on_change_disabled, names='value')\n",
"\n",
"def on_change(event):\n",
" \"\"\"\n",
" Update the toggle value if manually changing the disabled_elements list.\n",
" \"\"\"\n",
" toggle_disabled.value = \"O\" in widget.disabled_elements\n",
"widget.observe(on_change, names='disabled_elements', type='change') \n",
" \n",
"toggle_disabled"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Set the selected values from python\n",
"Choose the selected values from python"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "19555bf8512342cb974838d16c806eea",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(button_style='success', description='Select only Li and H (from python)', layout=Layout(width='300px'),…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def on_set_from_ptyhon(event):\n",
" # NOTE! If you put an element which does not exist, it will stay forever in the list, but it's ignored\n",
" widget.selected_elements = ['Li', 'H']\n",
"\n",
"button = widgets.Button(\n",
" description=\"Select only Li and H (from python)\", \n",
" button_style='success',\n",
" layout={'width': '300px'}\n",
")\n",
"button.on_click(on_set_from_ptyhon)\n",
"button"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Change the displayed string for some elements\n",
"Note that you should pass valid HTML strings, as they will not be escaped. On the other hand this allows to use HTML to change the class, color, ..."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7bc9c3a8545544519729a10742acdbd2",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Button(button_style='success', description='Make noble gases bold', layout=Layout(width='300px'), style=Button…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"assert w.value == 'Hello World'"
"def get_noble_gases_state():\n",
" label_deactivate = \"Make noble gases bold\"\n",
" label_activate = \"Make noble gases not bold\"\n",
" def deactivate_noble_gases(event):\n",
" widget.display_names_replacements = {}\n",
" def activate_noble_gases(event):\n",
" widget.display_names_replacements = {\n",
" elem_name: \"<strong>{}</strong>\".format(elem_name)\n",
" for elem_name in ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og']\n",
" }\n",
"\n",
" if 'He' in widget.display_names_replacements:\n",
" return {\n",
" 'is_active': True,\n",
" 'toggler_function': deactivate_noble_gases,\n",
" 'toggled_label': label_deactivate,\n",
" 'current_label': label_activate\n",
" }\n",
" else:\n",
" return {\n",
" 'is_active': True,\n",
" 'toggler_function': activate_noble_gases,\n",
" 'toggled_label': label_activate,\n",
" 'current_label': label_deactivate\n",
" }\n",
"\n",
"button_noble = widgets.Button(\n",
" description=get_noble_gases_state()['current_label'], \n",
" button_style='success',\n",
" layout={'width': '300px'}\n",
")\n",
" \n",
"def on_toggle_noble_gases(event):\n",
" \"\"\"Toggle the state of the button and of the .\"\"\"\n",
" state = get_noble_gases_state() \n",
" # Change the table\n",
" state['toggler_function'](event)\n",
" # Change the button description\n",
" button_noble.description = state['toggled_label']\n",
" \n",
"button_noble.on_click(on_toggle_noble_gases)\n",
"button_noble"
]
}
],
Expand All @@ -51,9 +281,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class MCPTableView extends DOMWidgetView {
return {"click .periodic-table-entry": "toggleElement"};
}

toggleElement(event) {
toggleElement(event: any) {
let classNames: object = _.map(event.target.classList, function(a){return a});
let tempName: string = _.chain(classNames)
.last()
Expand Down

0 comments on commit 603f5d7

Please sign in to comment.