From 2a06a520ffa6db4f0363308fcc508413c03d450c Mon Sep 17 00:00:00 2001 From: brimoor Date: Mon, 19 Feb 2024 10:55:27 -0800 Subject: [PATCH 1/4] adding a load_default_view operator --- plugins/utils/README.md | 16 ++++++++++++++++ plugins/utils/__init__.py | 22 ++++++++++++++++++++++ plugins/utils/fiftyone.yml | 1 + 3 files changed, 39 insertions(+) diff --git a/plugins/utils/README.md b/plugins/utils/README.md index f12fbff3..e47674d2 100644 --- a/plugins/utils/README.md +++ b/plugins/utils/README.md @@ -294,3 +294,19 @@ delegate( persistent=True, ) ``` + +### load_default_view + +When this operator is enabled, any dataset whose `info` dict contains the name +of a valid saved view in its `default_view` key will load the specified view by +default (rather than the full dataset) whenever that dataset is loaded in the +App. + +```py +# Save a view on the dataset that you wish to be loaded by default +dataset.save_view("your_view", view) + +# Register the saved view's name so the operator knows to load it +dataset.info["default_view"] = view.name +dataset.save() +``` diff --git a/plugins/utils/__init__.py b/plugins/utils/__init__.py index be60dffd..a5205596 100644 --- a/plugins/utils/__init__.py +++ b/plugins/utils/__init__.py @@ -2470,6 +2470,27 @@ def execute(self, ctx): fcn(*args, **kwargs) +class LoadDefaultView(foo.Operator): + @property + def config(self): + return foo.OperatorConfig( + name="load_default_view", + label="Load default view", + on_startup=True, + unlisted=True, + ) + + def execute(self, ctx): + default_view = ctx.dataset.info.get("default_view", None) + if default_view and ctx.dataset.has_saved_view(default_view): + view = ctx.dataset.load_saved_view(default_view) + ctx.trigger("set_view", params={"view": serialize_view(view)}) + + +def serialize_view(view): + return json.loads(json_util.dumps(view._serialize())) + + def register(p): p.register(CreateDataset) p.register(LoadDataset) @@ -2483,3 +2504,4 @@ def register(p): p.register(ComputeMetadata) p.register(GenerateThumbnails) p.register(Delegate) + p.register(LoadDefaultView) diff --git a/plugins/utils/fiftyone.yml b/plugins/utils/fiftyone.yml index 5211ad09..f35f7e4e 100644 --- a/plugins/utils/fiftyone.yml +++ b/plugins/utils/fiftyone.yml @@ -18,3 +18,4 @@ operators: - compute_metadata - generate_thumbnails - delegate + - load_default_view From 868fe8d4f1ce526b8e5d133c2c96cf15fcabcae7 Mon Sep 17 00:00:00 2001 From: brimoor Date: Mon, 19 Feb 2024 10:57:12 -0800 Subject: [PATCH 2/4] allow operator to be triggered manually --- plugins/utils/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/utils/__init__.py b/plugins/utils/__init__.py index a5205596..0fc9eb1c 100644 --- a/plugins/utils/__init__.py +++ b/plugins/utils/__init__.py @@ -2477,7 +2477,6 @@ def config(self): name="load_default_view", label="Load default view", on_startup=True, - unlisted=True, ) def execute(self, ctx): From ddca1f40843dc9fd9fea3700f22e91139963172f Mon Sep 17 00:00:00 2001 From: brimoor Date: Thu, 7 Mar 2024 23:41:17 -0500 Subject: [PATCH 3/4] use on_dataset_open hook --- plugins/utils/README.md | 2 +- plugins/utils/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/utils/README.md b/plugins/utils/README.md index e47674d2..32ddffeb 100644 --- a/plugins/utils/README.md +++ b/plugins/utils/README.md @@ -299,7 +299,7 @@ delegate( When this operator is enabled, any dataset whose `info` dict contains the name of a valid saved view in its `default_view` key will load the specified view by -default (rather than the full dataset) whenever that dataset is loaded in the +default (rather than the full dataset) whenever that dataset is opened in the App. ```py diff --git a/plugins/utils/__init__.py b/plugins/utils/__init__.py index 0fc9eb1c..e3ceaecc 100644 --- a/plugins/utils/__init__.py +++ b/plugins/utils/__init__.py @@ -2476,7 +2476,7 @@ def config(self): return foo.OperatorConfig( name="load_default_view", label="Load default view", - on_startup=True, + on_dataset_open=True, ) def execute(self, ctx): From cc104ee033743835350651e0cfeda109dca44755 Mon Sep 17 00:00:00 2001 From: brimoor Date: Sat, 16 Mar 2024 19:57:51 -0400 Subject: [PATCH 4/4] load saved view by name --- plugins/utils/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/utils/__init__.py b/plugins/utils/__init__.py index e3ceaecc..853a95d6 100644 --- a/plugins/utils/__init__.py +++ b/plugins/utils/__init__.py @@ -2482,8 +2482,11 @@ def config(self): def execute(self, ctx): default_view = ctx.dataset.info.get("default_view", None) if default_view and ctx.dataset.has_saved_view(default_view): - view = ctx.dataset.load_saved_view(default_view) - ctx.trigger("set_view", params={"view": serialize_view(view)}) + if Version(fo.__version__) >= Version("0.23.7"): + ctx.trigger("set_view", params={"name": default_view}) + else: + view = ctx.dataset.load_saved_view(default_view) + ctx.trigger("set_view", params={"view": serialize_view(view)}) def serialize_view(view):