Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions plugins/operator-examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,81 @@ def execute(self, ctx):
print(f"Message: {ctx.params['message']}")


class SampleDownloadExample(foo.Operator):
@property
def config(self):
return foo.OperatorConfig(
name="example_sample_download",
label="Examples: Sample Download",
)

def resolve_input(self, ctx):
inputs = types.Object()

inputs.md(
"This operator allows you to download a sample's source media"
)

if ctx.current_sample is not None:
label = "Press `Execute` to download the current sample's media"
else:
label = "Please open a sample in the modal first"

inputs.message("Sample Download", label=label)
return types.Property(inputs)

def execute(self, ctx):
import os
import fiftyone.core.storage as fos

if ctx.current_sample is None:
raise ValueError(
"No sample selected. Please select a sample first."
)

sample = ctx.dataset[ctx.current_sample]
filepath = sample.filepath

if fos.is_local(filepath):
download_url = f"http://localhost:5151/media?filepath={filepath}"
else:
download_url = filepath

filename = os.path.basename(filepath)

return {
"download_url": download_url,
"filename": filename,
"sample_id": ctx.current_sample,
"filepath": filepath,
}

def resolve_output(self, ctx):
outputs = types.Object()

if ctx.params.get("error"):
outputs.str("error", label="Error", view=types.Error())
else:
# Create a download button view
download_url = ctx.results.get("download_url")
filename = ctx.results.get("filename")
download_button = types.Button(
label="Download Image",
icon="download",
operator="download_file",
params={"url": download_url, "filename": filename},
)

outputs.str("filename", label="Filename")
outputs.str(
"download_url", label="Download URL", view=download_button
)
outputs.str("sample_id", label="Sample ID")
outputs.str("filepath", label="File Path")

return types.Property(outputs)


def register(p):
p.register(MessagesExample)
p.register(SimpleInputExample)
Expand All @@ -905,3 +980,4 @@ def register(p):
p.register(TargetViewExample)
p.register(PythonViewExample)
p.register(ExampleComplexExecution)
p.register(SampleDownloadExample)
1 change: 1 addition & 0 deletions plugins/operator-examples/fiftyone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ operators:
- example_lazy_field
- example_python_view
- example_complex_execution
- example_sample_download
secrets:
- FIFTYONE_EXAMPLE_SECRET