How to transform <anvil.LazyMedia object> into the real contents of this reference


What I’m trying to do
I’m working with the Form Generator example from @movelabs.
This app generates a Form based on a text definition.
Currently the app does not support generating images in the Form, and I want to add that functionallity.

I have a table with 3 text fields and and Image I uploaded directly to the table.

Following the logic of the application, I added the required parts for displaying the image, and apparently in the Form, the element for the image appears, but without contents.
Doing a bit of manual debugging, I get to the point in which I think that what is arriving as the content for the image is <anvil.LazyMedia object>, which is what I get when I call :

self.item = dict(‘get_contents_byId’,self.item_id))

to get the contents of the row of the database.

I’m stuck here because I don’t know how to transform this reference into the real image and pass that to the form_builder function.

Basically in the form_builder function:


input = FormInput(**entry)

Passes the dictionary (entry) with the table row to the FormInput Class
And this is what it is returned:

input.value = initial_data.get(entry[‘key’], None)

For the text fields input.value is the contents of the corresponding database field.
But for the image fields it is <anvil.LazyMedia object>

In the FormInput I again assing the contents for when the type is image:

    elif self.type == "image":
        self.input = Image(source=key)

And again what I’m assigning is <anvil.LazyMedia object>

How can I get the image instead of the reference (or get the image from the reference) and pass that to the Form instead of the reference itself? In a regular Form within the editor I would do this using a data binding like this:


, but here I don’t know how to translate what I have into that.

I’ll appreciate your advice in how to achieve this.

Here is the app for you to check firsthand.

Thank you!!!

The image source should be set to the media object from the database. Anvil should take care of everything else for you from there.

Hi @jshaffstall ,
That is exactly what I’m trying to do, and also exactly where I’m failing.
By the media object from the database you mean this? :

<anvil.LazyMedia object>

That you would extract from the row you get from this?:‘get_contents_byId’,self.item_id)

Like this? :

self.input = Image(source=<anvil.LazyMedia object>)

That server call returns a row from the contents data table. There’s a column in that table named imagen. You should be able to set the image source to row['imagen'] if you captured the result of the server call in a variable named row.

You’ve got enough indirection going on with the form builder, though, that it will take some debugging to see where things are going wrong. For example, just with a cursory look at where you call that server function, it looks like you’re immediately discarding the result from the server function:

      self.item = dict('get_contents_byId',self.item_id))
      if not self.item:
        Notification(f"Supplier not found!", style="danger")
        routing.set_url_hash("contents", replace_current_url=True, load_from_cache=False)
        self.item = {}

When self.item is something (e.g. the server function returns a row) the if not self.item is false, so the else is triggered, which discards the contents of self.item.

1 Like

Yes, I agree that there is a lot of passing things through, and probably this is where I’m loosing the real reference to the media object. At some point it gets a bit confusing :slight_smile:
I’ll take your advice and work on simplifying the process.
Anyway your comments gave a fresh perspective on what I was missing.
Thank you!!