Skip to content

Commit

Permalink
add set_spaces operation (#4381)
Browse files Browse the repository at this point in the history
  • Loading branch information
imanjra authored and benjaminpkane committed May 20, 2024
1 parent 461679d commit ecc0242
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/packages/operators/src/built-in-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import * as fos from "@fiftyone/state";
import * as types from "./types";

import { LOAD_WORKSPACE_OPERATOR } from "@fiftyone/spaces/src/components/Workspaces/constants";
import { toSlug } from "@fiftyone/utilities";
import copyToClipboard from "copy-to-clipboard";
import { useSetRecoilState } from "recoil";
Expand Down Expand Up @@ -771,7 +772,14 @@ class SetSpaces extends Operator {
return { setSessionSpacesState };
}
async execute(ctx: ExecutionContext) {
ctx.hooks.setSessionSpacesState(ctx.params.spaces);
const { name, spaces } = ctx.params || {};
if (spaces) {
ctx.hooks.setSessionSpacesState(spaces);
} else if (name) {
executeOperator(LOAD_WORKSPACE_OPERATOR, { name }, { skipOutput: true });
} else {
throw new Error('Param "spaces" or "name" is required to set a space');
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions fiftyone/operators/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,7 @@ def resolve_input(self, ctx):

def execute(self, ctx):
name = ctx.params.get("name", None)
spaces = ctx.dataset.load_workspace(name)
ctx.trigger("set_spaces", {"spaces": spaces.to_dict()})
ctx.ops.set_spaces(name=name)
return {}


Expand Down
16 changes: 16 additions & 0 deletions fiftyone/operators/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

import json

from bson import json_util
Expand Down Expand Up @@ -316,6 +317,21 @@ def clear_selected_labels(self):
"""Clear the selected labels in the App."""
return self._ctx.trigger("clear_selected_labels")

def set_spaces(self, spaces=None, name=None):
"""Set space in the App by name or :class:`fiftyone.core.odm.workspace.Space`.
Args:
spaces: the spaces (:class:`fiftyone.core.odm.workspace.Space`) to load
name: the name of the workspace to load
"""
params = {}
if spaces is not None:
params["spaces"] = spaces.to_dict()
elif name is not None:
params["spaces"] = self._ctx.dataset.load_workspace(name).to_dict()

return self._ctx.trigger("set_spaces", params=params)


def _serialize_view(view):
return json.loads(json_util.dumps(view._serialize()))

0 comments on commit ecc0242

Please sign in to comment.