Skip to content

Commit

Permalink
feat: Make height reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Jun 29, 2024
1 parent 1295e57 commit 3051008
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type MeshModel = { model_id: string } & AnyModel<{
}>;

export type Model = AnyModel<{
height: number,
_volumes: Array<string>;
_meshes: Array<string>;
_opts: Record<string, unknown>;
Expand Down
5 changes: 4 additions & 1 deletion js/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
async render({ model, el }: { model: Model; el: HTMLElement }) {
const canvas = document.createElement("canvas");
const container = document.createElement("div");
container.style.height = "300px";
container.style.height = `${model.get("height")}px`;
container.appendChild(canvas);
el.appendChild(container);

Expand All @@ -30,6 +30,9 @@ export default {
nv.drawScene();
nv.updateGLVolume();
});
model.on("change:height", () => {
container.style.height = `${model.get("height")}px`;
})

// All the logic for cleaning up the event listeners and the nv object
return () => {
Expand Down
6 changes: 4 additions & 2 deletions src/ipyniivue/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class NiiVue(OptionsMixin, anywidget.AnyWidget):
"""Represents a Niivue instance."""

_esm = pathlib.Path(__file__).parent / "static" / "widget.js"

height = t.Int().tag(sync=True)
_opts = t.Dict({}).tag(sync=True, to_json=serialize_options)
_volumes = t.List(t.Instance(Volume), default_value=[]).tag(
sync=True, **ipywidgets.widget_serialization
Expand All @@ -49,13 +51,13 @@ class NiiVue(OptionsMixin, anywidget.AnyWidget):
sync=True, **ipywidgets.widget_serialization
)

def __init__(self, **options):
def __init__(self, height: int = 300, **options):
# convert to JS camelCase options
_opts = {
_SNAKE_TO_CAMEL_OVERRIDES.get(k, snake_to_camel(k)): v
for k, v in options.items()
}
super().__init__(_opts=_opts, _volumes=[], _meshes=[])
super().__init__(height=height, _opts=_opts, _volumes=[], _meshes=[])

def load_volumes(self, volumes: list):
"""Load a list of volumes into the widget.
Expand Down

0 comments on commit 3051008

Please sign in to comment.