-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dou Du
committed
Mar 10, 2024
1 parent
83595ed
commit 500cc19
Showing
9 changed files
with
1,029 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules | ||
.venv | ||
dist | ||
|
||
# Python | ||
__pycache__ | ||
.ipynb_checkpoints | ||
|
||
src/widget_periodictable/static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# widget_periodictable | ||
|
||
```sh | ||
pip install widget_periodictable | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,339 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# The widget periodic table" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import ipywidgets as widgets\n", | ||
"from widget_periodictable import PTableWidget\n", | ||
"from ipywidgets import Layout" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Visualize the element grid" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Show the widget\n", | ||
"widget = PTableWidget(states = 3, selected_elements = {\"C\": 0, \"Si\": 1, \"Ge\": 2}, \n", | ||
" selected_colors = ['red', 'green', 'cyan'], \n", | ||
" disabled_elements = ['B', 'Al', 'Ga'],\n", | ||
" unselected_color='blue', border_color = 'white', width = '40px')\n", | ||
"display(widget)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 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's state, it will set the state as zero." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Init the selected elements by using a dictionary:\n", | ||
"\n", | ||
"```python\n", | ||
"widget.selected_elements = {\"La\": 0, \"Ce\": 1, \"Pr\": 2}\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"widget.selected_elements = {\"La\": 0, \"Ce\": 1, \"Pr\": 2}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Change or set element state by:\n", | ||
"\n", | ||
"```python\n", | ||
"widget.set_element_state(\"Nd\",0)\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"widget.set_element_state(\"Nd\",0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"However, you cannot use widget.selected_elements[\"Nd\"] = 1 to set the states of the elements." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Get the elements have the same state:\n", | ||
"\n", | ||
"```python\n", | ||
"widget.get_elements_by_state(0)\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"widget.get_elements_by_state(0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Get the selected values in python" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Check which elements are currently selected" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"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": {}, | ||
"source": [ | ||
"## Play with enabling/disabling some elements" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"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": [ | ||
"## Freeze (disable clicking events) the whole periodic table " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"toggle_freeze = widgets.Checkbox(\n", | ||
" value= False,\n", | ||
" description='Freeze periodic table:',\n", | ||
" disabled=False\n", | ||
")\n", | ||
"\n", | ||
"def on_freeze_change(event):\n", | ||
" widget.disabled = toggle_freeze.value\n", | ||
" \n", | ||
"toggle_freeze.observe(on_freeze_change, names='value')\n", | ||
"\n", | ||
"toggle_freeze" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Set the selected values from python\n", | ||
"Choose the selected values from python" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"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\":0, \"H\":0}\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_python)\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": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"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: \"<b>{}</b>\".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" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.7" | ||
}, | ||
"voila": { | ||
"authors": "Dou Du, Casper Welzel Andersen and Giovanni Pizzi" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.