Skip to content

Commit d654bf1

Browse files
ritchbrimoor
authored andcommitted
download file examples
1 parent 247ce20 commit d654bf1

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

plugins/operator-examples/__init__.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,81 @@ def execute(self, ctx):
879879
print(f"Message: {ctx.params['message']}")
880880

881881

882+
class SampleDownloadExample(foo.Operator):
883+
@property
884+
def config(self):
885+
return foo.OperatorConfig(
886+
name="example_sample_download",
887+
label="Examples: Sample Download",
888+
)
889+
890+
def resolve_input(self, ctx):
891+
inputs = types.Object()
892+
inputs.message(
893+
"Sample Download",
894+
label="Select a sample to download its image",
895+
description="This operator allows you to select a sample and download its image file.",
896+
)
897+
return types.Property(inputs)
898+
899+
def execute(self, ctx):
900+
if ctx.current_sample is None:
901+
raise ValueError(
902+
"No sample selected. Please select a sample first."
903+
)
904+
905+
sample = ctx.dataset[ctx.current_sample]
906+
filepath = sample.filepath
907+
908+
# Create download URL for the media file
909+
import fiftyone.core.storage as fos
910+
911+
# TODO make this work in teams
912+
download_url = f"http://localhost:5151/media?filepath={filepath}"
913+
914+
# Extract filename from filepath
915+
import os
916+
917+
filename = os.path.basename(filepath)
918+
919+
return {
920+
"download_url": download_url,
921+
"filename": filename,
922+
"sample_id": ctx.current_sample,
923+
"filepath": filepath,
924+
}
925+
926+
def resolve_output(self, ctx):
927+
outputs = types.Object()
928+
929+
if ctx.params.get("error"):
930+
outputs.str("error", label="Error", view=types.Error())
931+
else:
932+
# Create a download button view
933+
download_url = ctx.results.get("download_url")
934+
filename = ctx.results.get("filename")
935+
print(download_url)
936+
download_button = types.Button(
937+
label="Download Image",
938+
icon="download",
939+
operator="download_file",
940+
params={"url": download_url, "filename": filename},
941+
)
942+
943+
outputs.str("filename", label="Filename")
944+
outputs.str(
945+
"download_url", label="Download URL", view=download_button
946+
)
947+
outputs.str("sample_id", label="Sample ID")
948+
outputs.str("filepath", label="File Path")
949+
950+
return types.Property(outputs)
951+
952+
953+
# an example operator that allows a user to select a sample
954+
# then in the output they can click a button that downloads the image
955+
956+
882957
def register(p):
883958
p.register(MessagesExample)
884959
p.register(SimpleInputExample)
@@ -905,3 +980,4 @@ def register(p):
905980
p.register(TargetViewExample)
906981
p.register(PythonViewExample)
907982
p.register(ExampleComplexExecution)
983+
p.register(SampleDownloadExample)

plugins/operator-examples/fiftyone.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ operators:
3131
- example_lazy_field
3232
- example_python_view
3333
- example_complex_execution
34+
- example_sample_download
3435
secrets:
3536
- FIFTYONE_EXAMPLE_SECRET

0 commit comments

Comments
 (0)