From d7bf982cb6c295bc9bbc46f0e2dcdaee0ded688c Mon Sep 17 00:00:00 2001 From: Dou Date: Sat, 23 May 2020 16:39:32 +0200 Subject: [PATCH] Use dictionary to fix issue #4 --- examples/introduction.ipynb | 34 +++++++++++++------------- package.json | 2 +- widget_periodictable/_frontend.py | 2 +- widget_periodictable/_version.py | 2 +- widget_periodictable/periodic_table.py | 28 ++++++++++----------- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/examples/introduction.ipynb b/examples/introduction.ipynb index e619830..a4aa665 100644 --- a/examples/introduction.ipynb +++ b/examples/introduction.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -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": {}, @@ -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" ] }, @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -93,7 +93,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -120,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -129,7 +129,7 @@ "['La', 'Nd']" ] }, - "execution_count": 32, + "execution_count": 34, "metadata": {}, "output_type": "execute_result" } @@ -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 }, @@ -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 }, @@ -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 }, @@ -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 }, diff --git a/package.json b/package.json index d31982c..f9c03fc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/widget_periodictable/_frontend.py b/widget_periodictable/_frontend.py index eb2c4a4..4e415d4 100644 --- a/widget_periodictable/_frontend.py +++ b/widget_periodictable/_frontend.py @@ -9,4 +9,4 @@ """ module_name = "widget-periodictable" -module_version = "^1.2.0" +module_version = "^1.3.0" diff --git a/widget_periodictable/_version.py b/widget_periodictable/_version.py index 52484e7..e9561a8 100644 --- a/widget_periodictable/_version.py +++ b/widget_periodictable/_version.py @@ -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)) diff --git a/widget_periodictable/periodic_table.py b/widget_periodictable/periodic_table.py index 9f630ef..c57db58 100644 --- a/widget_periodictable/periodic_table.py +++ b/widget_periodictable/periodic_table.py @@ -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 """ @@ -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__() @@ -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') @@ -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')