Skip to content

Commit 31cc09f

Browse files
committed
observe cell traitlet
1 parent 1dc64ca commit 31cc09f

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

example/example.ipynb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@
4141
"display(bz)"
4242
]
4343
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {},
48+
"outputs": [],
49+
"source": [
50+
"# update cell:\n",
51+
"bz.cell = [\n",
52+
" [5.0, 0.0, 0.0],\n",
53+
" [0.0, 1.0, 0.0],\n",
54+
" [0.0, 0.0, 1.0],\n",
55+
"]"
56+
]
57+
},
4458
{
4559
"cell_type": "code",
4660
"execution_count": null,

src/widget_bzvisualizer/__init__.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pathlib
33

44
import anywidget
5-
import traitlets
5+
import traitlets as tl
66

77
from . import utils
88

@@ -16,17 +16,26 @@ class BZVisualizer(anywidget.AnyWidget):
1616
_esm = pathlib.Path(__file__).parent / "static" / "widget.js"
1717
_css = pathlib.Path(__file__).parent / "static" / "widget.css"
1818

19-
seekpath_data = traitlets.Dict({}).tag(sync=True)
19+
# primary input parameters, not directly used in the JS app
20+
cell = tl.List().tag(sync=True)
21+
rel_coords = tl.List().tag(sync=True)
22+
atom_numbers = tl.List().tag(sync=True)
2023

21-
# parameters passed to the js BZVisualizer
22-
show_axes = traitlets.Bool(True).tag(sync=True)
23-
show_bvectors = traitlets.Bool(True).tag(sync=True)
24-
show_pathpoints = traitlets.Bool(False).tag(sync=True)
25-
disable_interact_overlay = traitlets.Bool(False).tag(sync=True)
24+
# auxiliary traitlet to easily manage the previous ones
25+
system = tl.Dict({}).tag(sync=True)
2626

27-
# Parameters to control the size of the div-container
28-
width = traitlets.Unicode("100%").tag(sync=True)
29-
height = traitlets.Unicode("400px").tag(sync=True)
27+
# Data used in the JS app
28+
seekpath_data = tl.Dict({}).tag(sync=True)
29+
30+
# optional parameters passed to the JS BZVisualizer
31+
show_axes = tl.Bool(True).tag(sync=True)
32+
show_bvectors = tl.Bool(True).tag(sync=True)
33+
show_pathpoints = tl.Bool(False).tag(sync=True)
34+
disable_interact_overlay = tl.Bool(False).tag(sync=True)
35+
36+
# parameters to control the size of the div-container
37+
width = tl.Unicode("100%").tag(sync=True)
38+
height = tl.Unicode("400px").tag(sync=True)
3039

3140
def __init__(
3241
self,
@@ -40,6 +49,14 @@ def __init__(
4049
The traitlets defined above can be set as a kwargs.
4150
"""
4251
super().__init__(**kwargs)
43-
self.seekpath_data = utils.get_seekpath_data_for_visualizer(
44-
cell, rel_coords, atom_numbers
45-
)
52+
self.system = {
53+
"cell": cell,
54+
"rel_coords": rel_coords,
55+
"atom_numbers": atom_numbers,
56+
}
57+
self.seekpath_data = utils.get_seekpath_data_for_visualizer(self.system)
58+
59+
@tl.observe("cell")
60+
def _cell_changed(self, change):
61+
self.system[change["name"]] = change["new"]
62+
self.seekpath_data = utils.get_seekpath_data_for_visualizer(self.system)

src/widget_bzvisualizer/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
from seekpath.brillouinzone.brillouinzone import get_BZ
44

55

6-
def get_seekpath_data_for_visualizer(cell, relcoords, atomic_numbers):
7-
system = (np.array(cell), np.array(relcoords), np.array(atomic_numbers))
6+
def get_seekpath_data_for_visualizer(system):
7+
system = (
8+
np.array(system["cell"]),
9+
np.array(system["rel_coords"]),
10+
np.array(system["atom_numbers"]),
11+
)
812
res = seekpath.get_explicit_k_path(system, with_time_reversal=False)
913

1014
b1, b2, b3 = res["reciprocal_primitive_lattice"]

0 commit comments

Comments
 (0)