In this video, we’ll learn how to access files stored in your Google Drive from an Anvil app.
We’ll build a photo gallery that cycles through all the pictures in a Google Drive folder.
You can do a lot more with Anvil and Google services. Find out more in the Google section of the Anvil reference manual.
We enable the Google Drive Service and connect it to a folder on Google Drive. We make it read-only.
Wire up the components | 1:36 - 7:10
We explain the process of developing the code to flick through the images when the Buttons are clicked. Here it is in brief.
First, we get a reference to the picture folder:
pic_folder = app_files.my_holiday.files
Then, we initialise the image to the 0th picture:
self.pic_num = 0 self.current_pic.source = pic_folder[self.pic_num]
self.pic_num keeps track of which picture we’re looking at right now.
That means we can display the current picture using this method:
def display_pic(self): pic = self.files[self.pic_num] self.current_pic.source = pic
To make the buttons work, we just need to increment or decrement the picture number, and call
def next_btn_click(self, **event_args): """This method is called when the button is clicked""" self.pic_num += 1 self.display_pic() def prev_btn_click(self, **event_args): """This method is called when the button is clicked""" self.pic_num -= 1 self.display_pic()
And finally, we need to disable the Buttons when we’ve reached either end of the picture list:
def display_pic(self): # Disable buttons if we've reached the end of the picture list self.prev_btn.enabled = self.pic_num > 0 self.next_btn.enabled = self.pic_num < len(pic_folder) - 1 # Then display the picture pic = pic_folder[self.pic_num] self.current_pic.source = pic
And that’s all we need. The finished app looks like this:
Click this button to copy the app into your account:
(You’ll need to connect it to a Google Drive folder full of pictures - cloned apps do not preserve Google Drive credentials!)
Watch Ian build a document storage app, storing and displaying multiple documents, with full version history including who uploaded each version. This shows how to combine some of the most commonly used Anvil features to build a useful business application. The full build takes 15 minutes.
If you’d like to learn the basics of Anvil, start with the Hello, World! tutorial.