Skip to content

Commit

Permalink
More changes for issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
dou-du committed May 21, 2020
1 parent c05d00c commit 31ebe91
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 33 deletions.
157 changes: 130 additions & 27 deletions examples/introduction.ipynb
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The widget periodic table"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -14,18 +21,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualize the element grid"
"## Visualize the element grid"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 143,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "86fa0f86c11a4ba790f21039e977d680",
"model_id": "1cfffa4578f84ba58b2a2f9b098ad06f",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -47,37 +54,73 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Set the states of the elements\n",
"## Set the states of the elements\n",
"\n",
"The periodic table allows users to customize the states of the selected elements. \n",
"If one do not give the selected element state, it will set the state as zero."
"If one do not give the selected element's state, it will set the state as zero."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Select the element and set the state:\n",
"\n",
"```python\n",
"widget.set_element_state(\"Ce\", 2)\n",
"widget.set_element_state(\"Am\", 1)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 144,
"metadata": {},
"outputs": [],
"source": [
"widget.selected_elements = [\"La\", \"Ce\", \"Pr\", \"Nd\"]\n",
"widget.set_element_state(\"Ce\", 2)\n",
"widget.set_element_state(\"Am\", 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Only select the elements. If the element has been selected before, it will keep the previous state.\n",
"Ohterwise, the state will be set as zero. \n",
"\n",
"```python\n",
"widget.selected_elements = [\"Ce\", \"Am\", \"Bk\", \"Cf\"]\n",
"widget.get_elements_by_state(0)\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 145,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['Bk', 'Cf']"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"widget.selected_elements = [\"Ce\", \"Am\", \"Bk\", \"Cf\"]"
"widget.selected_elements = [\"Ce\", \"Am\", \"Bk\", \"Cf\"]\n",
"widget.get_elements_by_state(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Get the selected values in python"
"## Get the selected values in python"
]
},
{
Expand All @@ -89,9 +132,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 146,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "025efad6db12462bbf269a6557974515",
"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",
Expand All @@ -116,14 +174,29 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Play with enabling/disabling some elements"
"## Play with enabling/disabling some elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 147,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "89001b02f50b4aabaafc2a86457d337f",
"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",
Expand Down Expand Up @@ -153,17 +226,32 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Set the selected values from python\n",
"## Set the selected values from python\n",
"Choose the selected values from python"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 148,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "e0753276121f49b5a977eb4e96ceafbe",
"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",
"def on_set_from_python(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",
Expand All @@ -172,23 +260,38 @@
" button_style='success',\n",
" layout={'width': '300px'}\n",
")\n",
"button.on_click(on_set_from_ptyhon)\n",
"button.on_click(on_set_from_python)\n",
"button"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Change the displayed string for some elements\n",
"## 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": null,
"execution_count": 149,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "ee04372070ca45e4a9c70d057bf1836d",
"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": [
"def get_noble_gases_state():\n",
" label_deactivate = \"Make noble gases bold\"\n",
Expand All @@ -197,7 +300,7 @@
" widget.display_names_replacements = {}\n",
" def activate_noble_gases(event):\n",
" widget.display_names_replacements = {\n",
" elem_name: \"<strong>{}</strong>\".format(elem_name)\n",
" elem_name: \"<b>{}</b>\".format(elem_name)\n",
" for elem_name in ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og']\n",
" }\n",
"\n",
Expand Down Expand Up @@ -260,7 +363,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
19 changes: 13 additions & 6 deletions widget_periodictable/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from ipywidgets import DOMWidget
from traitlets import Unicode, Int, List, Dict, observe
from traitlets import Unicode, Int, List, Dict, observe, validate, TraitError
from ._frontend import module_name, module_version
from copy import deepcopy

Expand Down Expand Up @@ -44,11 +44,9 @@ def __init__(self, states = 1, disabled_color = 'gray', unselected_color = 'pink
self.selected_colors = selected_colors + additional_colors * (1 + (states - len(selected_colors)) // len(additional_colors))

def get_elements_by_state(self, state):
x = [];
for i, j in enumerate(self.selected_states):
if j == state:
x.append(self.selected_elements[i])
return x
if state >= self.states:
raise ValueError("The state is larger than the maxmum value.")
return [element for element, element_state in zip(self.selected_elements, self.selected_states) if element_state==state]

def set_element_state(self, elementName, state):
if state < self.states:
Expand All @@ -73,3 +71,12 @@ def _selected_elements_changed(self, change):
x.append(i)
y.append(0)
self.selected_states = y

@validate('selected_states')
def _valid_states(self, proposal):
if len(proposal['value']) > len(self.selected_elements):
raise TraitError('The selected_states length is wrong')
elif len(proposal['value']) < len(self.selected_elements):
return proposal['value'] + [0 for i in range(len(self.selected_elements)-len(proposal['value']))]
else:
return proposal['value']

0 comments on commit 31ebe91

Please sign in to comment.