Hi,
I have several UI components created by code. This works fine.
Among those I have a fileloader and an image.
On the fileloader_change event I want to change the image in the image component by the one loaded in the file loader.
Here is the code for the event handler: (you will see many comment, mostly for debuggin purposes, and some for failed attempts to achieve the goal)
def handle_fileloader_change(self, sender, key, file, files, event_name,image_name, **event_args):
print("These are properties of the fileloader")
#print("Self Input - Image Loader : ----", self.inputs)
print("File:- ----", file) # This should be the current file loaded when the event is triggered
print("File URL:- ----", file.url) # Apparently this file element does not have url
print("Sender :-------", sender) # This is the fileloader component
print("Sender Text: -----", sender.text) # This is the key of the image to which to assign the fileloader loaded image
print("Key:----", key) # This is the key of the fileloader
print("Files: ----", files)
loaded_image = None
for element in self.inputs:
if element.type == "fileLoader" and element.key == key:
print("Element:----", element)
print("Element key:---", element.key)
print("Parameter Key:----",key)
print("Element type: ----", element.type)
loaded_image = element.value
print("Loaded Image:-----", loaded_image)
if event_name == "change" and file is not None:
# Get file url to later assign to the image source
loaded_image_url = anvil.server.call('get_file_url', sender.value)
print("File URL:----", loaded_image_url)
# Iterate through the form_inputs to find the corresponding Image
for form_input in self.inputs:
if form_input.type == "image" and form_input.key == sender.text:
print("All the following are properties of the image")
print("Image found")
print("FormInput:-----", form_input) #This is the image component
print("Content Type:----",form_input.value.content_type)
print("Content URL:----",form_input.value.url)
print("Type: ---", form_input.type) # This is the component type
print("Value before:-----", form_input.value)
#print("Source :-----", form_input.value.source) #Appaarently no such argument source
# Failed attempts to assign the new image
## ------------------------------------------------
#form_input.source = file # Assign the file to the source of the image
#form_input.value.source = loaded_image_url
#form_input.source = image_name
form_input.value.source = image_name
self.refresh_data_bindings()
print("Value after:-----", form_input.value)
#print("ImageComponent: ------", image_component)
Here is the output of the print statements:
File uploaded in the fileloader_loaded_image event handler:----- <anvil.FileMedia object>
File name:---- Cine.jpg
File URL:---- None
These are properties of the fileloader (in the event handler above)
File:- ---- <anvil.FileMedia object
File URL:- ---- None
Sender :------- <anvil.FileLoader object>
Sender Text: ----- imagen
Key:---- fileloader1
Files: ---- [<anvil.FileMedia object>]
Element:---- FormInput object - key: fileloader1, type: fileLoader, label: <anvil.Label object>, value: None, boundImage:imagen
Element key:— fileloader1
Parameter Key:---- fileloader1
Element type: ---- fileLoader
Loaded Image:----- None
File URL:---- URL not generatedAll the following are properties of the image
Image found
FormInput:----- FormInput object - key: imagen, type: image, label: <anvil.Label object>, value: <anvil.LazyMedia object>, boundImage:
Content Type:---- image/jpeg
Content URL:---- https://toka2562xuixmvid.anvil.app/debug/MWPTP4OCJQJFVQC66ZNP4UQMO5AXQBD3%3DDPBGHZBPG5YQXS6HOCPXYP7C/_/lm/table-media/168c861d86cc8a085f90b023bab76aa0/273571984/alaska.jpg?s=XUWCNP2WDHGGGLDJIKSM2UL7OFZLGK5L=wQEWhlUMjJU9AaY4lYfuaOA_2Jfq
Type: — image
Value before:----- <anvil.LazyMedia object>
Value after:----- <anvil.LazyMedia object>
I can’t get to assign the value of the new file to the current image object.
I’ve tried all the variants you can see commented in the code, but they don’t seem to work.
I’m probably missing some conceptual fact that might be the culprit of this code not working.
Here is the app in case you want to check it directly.
Just chose the menu option “Contenido” and when the list of items appears click on any of the descriptions, (the first column), and then go to the fileuploader called “imagen”
I would really appreciate any help in spotting the problem!!
Thank you very much!