Skip to content

Commit c300ad4

Browse files
authored
Close #29: support returning None in to 'hide' the current view (#30)
1 parent d323be1 commit c300ad4

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

ipyshiny/_ipyshiny.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ def __call__(self) -> object:
157157

158158
async def run(self) -> object:
159159
x = await self._fn()
160+
if x is None:
161+
return None
160162
widget = _as_widget(x)
161163
return {"model_id": widget.model_id} # type: ignore
162164

ipyshiny/static/output.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/output.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,27 @@ class IPyWidgetOutput extends Shiny.OutputBinding {
4040
this.renderError(el, err);
4141
}
4242
renderValue(el: HTMLElement, data): void {
43-
// TODO: allow for null value
43+
44+
// Allow for a None/null value to hide the widget (css inspired by htmlwidgets)
45+
if (!data) {
46+
el.style.visibility = "hidden";
47+
return;
48+
} else {
49+
el.style.visibility = "inherit";
50+
}
51+
4452
const model = manager.get_model(data.model_id);
4553
if (!model) {
4654
throw new Error(`No model found for id ${data.model_id}`);
4755
}
56+
4857
model.then((m) => {
4958
const view = manager.create_view(m, {});
5059
view.then(v => {
5160
manager.display_view(v, {el: el}).then(() => {
5261
// TODO: This is not an ideal way to handle the case where another render
5362
// is requested before the last one is finished displaying the view, but
5463
// it's probably the least unobtrusive way to handle this for now
55-
//
5664
while (el.childNodes.length > 1) {
5765
el.removeChild(el.childNodes[0]);
5866
}

0 commit comments

Comments
 (0)