The widget periodic table¶
In [1]:
import ipywidgets as widgets
from widget_periodictable import PTableWidget
Visualize the element grid¶
In [2]:
# Show the widget
widget = PTableWidget(states = 5, selected_elements = {"Be":0}, selected_colors = ['red', 'green', 'yellow'], unselected_color='pink')
widget
Set the states of the elements¶
The periodic table allows users to customize the states of the selected elements. If one do not give the selected element’s state, it will set the state as zero.
Init the selected elements by using a dictionary:
widget.selected_elements = {"La": 0, "Ce": 1, "Pr": 2}
In [3]:
widget.selected_elements = {"La": 0, "Ce": 1, "Pr": 2}
Change or set element state by:
widget.set_element_state("Nd",0)
In [4]:
widget.set_element_state("Nd",0)
However, you cannot use widget.selected_elements[“Nd”] = 1 to set the states of the elements.
Get the elements have the same state:
widget.get_elements_by_state(0)
In [5]:
widget.get_elements_by_state(0)
Out[5]:
['La', 'Nd']
Get the selected values in python¶
Check which elements are currently selected
In [6]:
output = widgets.Output()
def on_get_in_python(event):
output.clear_output()
with output:
print(
"Currently selected values:",
widget.selected_elements)
button2 = widgets.Button(
description="Get the currently selected values",
button_style='success',
layout={'width': '300px'}
)
button2.on_click(on_get_in_python)
vbox = widgets.VBox([button2, output])
vbox
Play with enabling/disabling some elements¶
In [7]:
toggle_disabled = widgets.Checkbox(
value="O" in widget.disabled_elements,
description='Disable oxygen',
disabled=False
)
def on_change_disabled(event):
if toggle_disabled.value:
# It's set, meaning we want to disable oxygen
widget.disabled_elements = ["O"]
else:
widget.disabled_elements = []
toggle_disabled.observe(on_change_disabled, names='value')
def on_change(event):
"""
Update the toggle value if manually changing the disabled_elements list.
"""
toggle_disabled.value = "O" in widget.disabled_elements
widget.observe(on_change, names='disabled_elements', type='change')
toggle_disabled
Set the selected values from python¶
Choose the selected values from python
In [8]:
def on_set_from_python(event):
# NOTE! If you put an element which does not exist, it will stay forever in the list, but it's ignored
widget.selected_elements = {"Li":0, "H":0}
button = widgets.Button(
description="Select only Li and H (from python)",
button_style='success',
layout={'width': '300px'}
)
button.on_click(on_set_from_python)
button
Change the displayed string for some elements¶
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, …
In [9]:
def get_noble_gases_state():
label_deactivate = "Make noble gases bold"
label_activate = "Make noble gases not bold"
def deactivate_noble_gases(event):
widget.display_names_replacements = {}
def activate_noble_gases(event):
widget.display_names_replacements = {
elem_name: "<b>{}</b>".format(elem_name)
for elem_name in ['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og']
}
if 'He' in widget.display_names_replacements:
return {
'is_active': True,
'toggler_function': deactivate_noble_gases,
'toggled_label': label_deactivate,
'current_label': label_activate
}
else:
return {
'is_active': True,
'toggler_function': activate_noble_gases,
'toggled_label': label_activate,
'current_label': label_deactivate
}
button_noble = widgets.Button(
description=get_noble_gases_state()['current_label'],
button_style='success',
layout={'width': '300px'}
)
def on_toggle_noble_gases(event):
"""Toggle the state of the button and of the ."""
state = get_noble_gases_state()
# Change the table
state['toggler_function'](event)
# Change the button description
button_noble.description = state['toggled_label']
button_noble.on_click(on_toggle_noble_gases)
button_noble
This work has been done with the support of the EPFL Open Science Fund OSSCAR.