Skip to content

Commit

Permalink
Use dictionary to fix issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
dou-du committed May 23, 2020
1 parent 102a35e commit d7bf982
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
34 changes: 17 additions & 17 deletions examples/introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -26,18 +26,18 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8570bc95bb644a79b80ac79558ecb7a4",
"model_id": "d0d0ae828dbb42ddaac70ab85f7039a1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"PTableWidget(selected_colors=['red', 'green', 'yellow', '#a6cee3', '#b2df8a', '#fdbf6f', '#6a3d9a', '#b15928',"
"PTableWidget(selected_colors=['red', 'green', 'yellow', '#a6cee3', '#b2df8a'], selected_elements={'Be': 0}, st"
]
},
"metadata": {},
Expand All @@ -46,7 +46,7 @@
],
"source": [
"# Show the widget\n",
"widget = PTableWidget(states = 100, selected_elements = {\"Be\":0}, selected_colors = ['red', 'green', 'yellow'], unselected_color='pink')\n",
"widget = PTableWidget(states = 5, selected_elements = {\"Be\":0}, selected_colors = ['red', 'green', 'yellow'], unselected_color='pink')\n",
"widget"
]
},
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -93,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -120,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand All @@ -129,7 +129,7 @@
"['La', 'Nd']"
]
},
"execution_count": 32,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -154,13 +154,13 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2d4a6739f3c447a18de4daf5b06cc8b1",
"model_id": "0899a84824a64e95a8f2849bbbb9b117",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -201,13 +201,13 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "35ecada85d6b4088b8359bc140b4c5ec",
"model_id": "7f7efa91bf1f4b8b85d9eff0d590772a",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -254,13 +254,13 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "682a3d7e344d413a830e85954880f60f",
"model_id": "4144a2a113d246aa9021e1de15a66138",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -296,13 +296,13 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "06fb30933f3e4f61905c2c1e54c2f4bd",
"model_id": "e71d9e807571477faf99bd6d9c9ffc1d",
"version_major": 2,
"version_minor": 0
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "widget-periodictable",
"version": "1.2.0",
"version": "1.3.0",
"description": "A jupyter widget to select chemical elements from the periodic table.",
"keywords": [
"jupyter",
Expand Down
2 changes: 1 addition & 1 deletion widget_periodictable/_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"""

module_name = "widget-periodictable"
module_version = "^1.2.0"
module_version = "^1.3.0"
2 changes: 1 addition & 1 deletion widget_periodictable/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
# Copyright (c) Giovanni Pizzi and Dou Du.
# Distributed under the terms of the Modified BSD License.

version_info = (1, 2, 0)
version_info = (1, 3, 0)
__version__ = ".".join(map(str, version_info))
28 changes: 14 additions & 14 deletions widget_periodictable/periodic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@
from ._frontend import module_name, module_version
from copy import deepcopy

allElements = [
"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg",
"Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe",
"Co","Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y",
"Zr", "Nb", "Mo", "Tc", "Ru", "Rh","Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te",
"I", "Xe", "Cs", "Ba", "Hf", "Ta", "W", "Re", "Os", "Ir","Pt", "Au", "Hg", "Tl",
"Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Rf", "Db", "Sg", "Bh", "Hs",
"Mt","Ds", "Rg", "Cn", "Nh", "Fi", "Mc", "Lv", "Ts", "Og", "La", "Ce", "Pr",
"Nd", "Pm", "Sm", "Eu","Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu","Ac",
"Th", "Pa", "U", "Np", "Pu", "Am","Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr"
];

class PTableWidget(DOMWidget):
"""TODO: Add docstring here
"""
Expand All @@ -41,6 +29,17 @@ class PTableWidget(DOMWidget):
unselected_color = Unicode('pink').tag(sync=True)
states = Int(1).tag(sync=True)
selected_colors = List([]).tag(sync=True)
allElements = List([
"H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg",
"Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe",
"Co","Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y",
"Zr", "Nb", "Mo", "Tc", "Ru", "Rh","Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te",
"I", "Xe", "Cs", "Ba", "Hf", "Ta", "W", "Re", "Os", "Ir","Pt", "Au", "Hg", "Tl",
"Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Rf", "Db", "Sg", "Bh", "Hs",
"Mt","Ds", "Rg", "Cn", "Nh", "Fi", "Mc", "Lv", "Ts", "Og", "La", "Ce", "Pr",
"Nd", "Pm", "Sm", "Eu","Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu","Ac",
"Th", "Pa", "U", "Np", "Pu", "Am","Cm", "Bk", "Cf", "Es", "Fm", "Md", "No", "Lr"
]);

def __init__(self, states = 1, selected_elements = {}, disabled_color = 'gray', unselected_color = 'pink', selected_colors = ["#a6cee3", "#b2df8a", "#fdbf6f", "#6a3d9a", "#b15928", "#e31a1c", "#1f78b4", "#33a02c", "#ff7f00", "#cab2d6", "#ffff99"]):
super(PTableWidget, self).__init__()
Expand All @@ -55,8 +54,9 @@ def __init__(self, states = 1, selected_elements = {}, disabled_color = 'gray',
self.selected_colors = selected_colors + additional_colors * (1 + (states - len(selected_colors)) // len(additional_colors))
self.selected_colors = self.selected_colors[:states]


def set_element_state(self, elementName, state):
if elementName not in allElements:
if elementName not in self.allElements:
raise TraitError('Element not found')
if state not in range(self.states):
raise TraitError('State value is wrong')
Expand All @@ -67,7 +67,7 @@ def set_element_state(self, elementName, state):
@validate('selected_elements')
def _selectedElements_change(self, proposal):
for x, y in proposal['value'].items():
if x not in allElements:
if x not in self.allElements:
raise TraitError('Element not found')
if not isinstance(y, int) or y not in range(self.states):
raise TraitError('State value is wrong')
Expand Down

0 comments on commit d7bf982

Please sign in to comment.