Images from Google Drive Displaying on the Client

I am having trouble with posting images from my google drive when a link is clicked from a datatable. I tried following the DOCS and reading through forums but can still use a little help. In a table (elephantsql) I have a list of Universities that have an ID and URL associated with them. The goal is to click a school in the data table and as a result the School info pops up (University Name, URL, and an image of it). I have the School and URL working but I cannot get the image to work.


  def school_name_link_click(self, **event_args):
    get_open_form().page_panel.get_components()[0].column_panel_3.visible = True     
    school_info = anvil.server.call('get_schools_fts',  event_args['sender'].text)
    get_open_form().page_panel.get_components()[0].school_name.text = school_info[0]['school']
    get_open_form().page_panel.get_components()[0].url.text = school_info[0]['url']
    get_open_form().page_panel.get_components()[0].image_1.source = app_files.university(school_info[0]['id'])
 

TypeError: ā€˜ā€™ object is not callable at [SearchUniversityTemplate, line 22]

The google drive folder the image is located is named ā€œUniversityā€ and the image name is ā€œ967.JPGā€
My python identifier under services Google API is app_files.university. The (school_info[0][ā€˜idā€™]) out puts a number that is the same name as the image located in my Google Drive. I figured if I named the image the same Id, I would be able to use the ID to locate the photo as they should never change. Does this make sense to do? I know using an anvil table would be easier and I plan on doing that for the users, but not the school images as they will be static and want to save room. Thank you.

Please can you identify line 22? Iā€™m guessing itā€™s

get_open_form().page_panel.get_components()[0].image_1.source = app_files.university(school_info[0]['id'])

but would be good to be sure.

A google drive file is a media object so you can assign it directly to an image as you are doing, but does app_files.university(school_info[0]['id']) point to the actual file name of the image?

I ask because you say this :

but it implies that itā€™s just the ID and not the file name itself.

Yes that is the line of the error.

And that just displays the ID number, not the file name itself. Say the output of that is 967. The image is saved in the google drive as 967.JPG

Iā€™m going to try it locally in a minute (as I donā€™t use google media myself) but my first guess is you need to append the file extension. Itā€™s worth a try. So turn it into this :

app_files.university(str(school_info[0]['id'])+".JPG")

I donā€™t think any assumptions are made about the file type.

edit
Ok, try this :

get_open_form().page_panel.get_components()[0].image_1.source = app_files.university.get(str(school_info[0]['id'])+".JPG")
2 Likes

I should of asked this sooner. I really appreciate this!! Thank you! Worked perfectly!

1 Like

Welcome. And I learned something new about google files, too :slight_smile:

@UCTechWeb - one further point, you shouldnā€™t need to reference your components like this :

get_open_form().page_panel.get_components()[0].image_1.source  = ...

You should be able to reference them directly, like this :

get_open_form().image_1.source = ...

Each component on a form must have a unique name, even if they are inside a container. So whilst you can fully qualify it like you have done, itā€™s not necessary and could cause problems if you rearrange your forms.

(thanks to @alcampopiano who pointed that out on another thread)

EDIT - there are exceptions to this, however, as @stefano.menci pointed out below. If the components you are referencing are inside another form thatā€™s been added to the open form, either by dragging & dropping or via add_component(), you may need to qualify the path further. There are probably other exceptions as well.

1 Like

Is this true even if image_1 is inside a form that has been added with add_component?

1 Like

I made too many assumptions; you are correct.

Updating my answer.

I guess you could just add ā€œā€¦in the IDEā€. :slight_smile:

Actually, it would be nice if it were possible to specify the componentā€™s name when adding them with add_component. This way you could still reference them by name if needed. Perhaps there are some complexities with Skulpt that make that difficult.

2 Likes

Brings me onto being able to give unique ids (eg DIV id=ā€¦) to a component, but thatā€™s another topic :slight_smile:

2 Likes

@alcampopiano I love the idea!

The first example on the documentation of add_component would be xyp.add_component(lbl, x=10, y=10, name='hello_label') and it could be reached with xyp.hello_label.

FR added.

2 Likes