@@ -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+
882957def 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 )
0 commit comments