Skip to content

Commit 49cb8fa

Browse files
authored
Announce dataset creation with API (#1170)
* Add trigger_reload method. * Add dataset reserve manual upload method. * Add example and add method to dataset class for announce manual upload. * Add trigger reload as method to dataset class and update example. * Remove returns for post requests. * Adapt naming and docstring. * Update example.
1 parent 8bce501 commit 49cb8fa

File tree

6 files changed

+123
-2
lines changed

6 files changed

+123
-2
lines changed

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ nav:
9999
- webknossos-py/examples/remote_datasets.md
100100
- webknossos-py/examples/zarr_and_dask.md
101101
- webknossos-py/examples/convert_4d_tiff.md
102+
- webknossos-py/examples/announce_dataset_upload.md
102103
- Annotation Examples:
103104
- webknossos-py/examples/apply_merger_mode.md
104105
- webknossos-py/examples/learned_segmenter.md
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Announce manual Dataset upload
2+
3+
This example demonstrates the manual process of uploading a Dataset. If users have file-system access to the WEBKNOSSOS datastore, they can upload a dataset by manually copying it to the binary_data folder. To ensure proper database entry creation and access rights, the Dataset can be announced using the WEBKNOSSOS API.
4+
5+
```python
6+
--8<--
7+
webknossos/examples/announce_dataset_upload.py
8+
--8<--
9+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import webknossos as wk
2+
from webknossos.dataset.remote_folder import RemoteFolder
3+
4+
5+
def main() -> None:
6+
# Get the folder id of the upload destination:
7+
folder_id = RemoteFolder.get_by_path("Datasets/").id
8+
9+
# Announce the manual upload of a new dataset:
10+
wk.Dataset.announce_manual_upload(
11+
dataset_name="my_new_dataset_name",
12+
organization="sample_organization",
13+
initial_team_ids=[],
14+
folder_id=folder_id,
15+
)
16+
17+
# Move/Copy the dataset manually to the datastore
18+
19+
# After a few minutes the dataset list is updated
20+
# automatically. The dataset is then visible for all
21+
# users that are part of one team defined in initial_team_ids.
22+
# To trigger a reload of the dataset properties manually, use:
23+
24+
wk.Dataset.trigger_reload_in_datastore(
25+
dataset_name="my_new_dataset_name", organization="sample_organization"
26+
)
27+
28+
29+
if __name__ == "__main__":
30+
main()

webknossos/webknossos/client/api_client/datastore_api_client.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Dict, Optional, Tuple
22

33
from webknossos.client.api_client.models import (
4+
ApiDatasetAnnounceUpload,
45
ApiDatasetUploadInformation,
56
ApiReserveDatasetUploadInformation,
67
)
@@ -34,7 +35,7 @@ def dataset_finish_upload(
3435
retry_count: int,
3536
) -> None:
3637
route = "/datasets/finishUpload"
37-
return self._post_json(
38+
self._post_json(
3839
route,
3940
upload_information,
4041
query={"token": token},
@@ -49,13 +50,32 @@ def dataset_reserve_upload(
4950
retry_count: int,
5051
) -> None:
5152
route = "/datasets/reserveUpload"
52-
return self._post_json(
53+
self._post_json(
5354
route,
5455
reserve_upload_information,
5556
query={"token": token},
5657
retry_count=retry_count,
5758
)
5859

60+
def dataset_trigger_reload(
61+
self,
62+
organization_name: str,
63+
dataset_name: str,
64+
token: Optional[str] = None,
65+
) -> None:
66+
route = f"/triggers/reload/{organization_name}/{dataset_name}"
67+
query: Query = {"token": token}
68+
self._post(route, query=query)
69+
70+
def dataset_reserve_manual_upload(
71+
self,
72+
dataset_announce: ApiDatasetAnnounceUpload,
73+
token: Optional[str],
74+
) -> None:
75+
route = "/datasets/reserveManualUpload"
76+
query: Query = {"token": token}
77+
self._post_json(route, dataset_announce, query)
78+
5979
def dataset_get_raw_data(
6080
self,
6181
organization_name: str,

webknossos/webknossos/client/api_client/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ class ApiDataset:
8686
description: Optional[str] = None
8787

8888

89+
@attr.s(auto_attribs=True)
90+
class ApiDatasetAnnounceUpload:
91+
dataset_name: str
92+
organization: str
93+
initial_team_ids: List[str]
94+
folder_id: str
95+
96+
8997
@attr.s(auto_attribs=True)
9098
class ApiDatasetIsValidNewNameResponse:
9199
is_valid: bool

webknossos/webknossos/dataset/dataset.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,59 @@ def open(cls, dataset_path: Union[str, PathLike]) -> "Dataset":
396396
exist_ok=True,
397397
)
398398

399+
@classmethod
400+
def announce_manual_upload(
401+
cls,
402+
dataset_name: str,
403+
organization: str,
404+
initial_team_ids: List[str],
405+
folder_id: str,
406+
token: Optional[str] = None,
407+
) -> None:
408+
"""
409+
Announces a manual dataset upload to webknossos. This is useful when users with access
410+
to the file system of the datastore want to upload a dataset manually. It creates an entry
411+
in the database and sets the access rights accordingly.
412+
"""
413+
from ..client._upload_dataset import _cached_get_upload_datastore
414+
from ..client.api_client.models import ApiDatasetAnnounceUpload
415+
from ..client.context import _get_context
416+
417+
context = _get_context()
418+
dataset_announce = ApiDatasetAnnounceUpload(
419+
dataset_name=dataset_name,
420+
organization=organization,
421+
initial_team_ids=initial_team_ids,
422+
folder_id=folder_id,
423+
)
424+
token = token or context.token
425+
upload_url = _cached_get_upload_datastore(context)
426+
datastore_api = context.get_datastore_api_client(upload_url)
427+
datastore_api.dataset_reserve_manual_upload(dataset_announce, token=token)
428+
429+
@classmethod
430+
def trigger_reload_in_datastore(
431+
cls,
432+
dataset_name: str,
433+
organization: str,
434+
token: Optional[str] = None,
435+
) -> None:
436+
"""
437+
This method is used for datasets that were uploaded manually
438+
to a webknossos datastore. It can not be used for local datasets.
439+
After a manual upload of a dataset, the datasets properties are
440+
updated automatically after a few minutes. To trigger a reload
441+
of the datasets properties manually, use this method.
442+
"""
443+
from ..client._upload_dataset import _cached_get_upload_datastore
444+
from ..client.context import _get_context
445+
446+
context = _get_context()
447+
token = token or context.token
448+
upload_url = _cached_get_upload_datastore(context)
449+
datastore_api = context.get_datastore_api_client(upload_url)
450+
datastore_api.dataset_trigger_reload(organization, dataset_name, token=token)
451+
399452
@classmethod
400453
def _parse_remote(
401454
cls,

0 commit comments

Comments
 (0)