Uploading a zip file from file loader to Google Colab

What I’m trying to do:
Hello! I’m extremely new to Anvil and not the best with technical terminology but I could really use some help.

I’m trying to build a GUI that allows the user to upload a zip file (from the file loader component) and from Google Colab, I wanted to access the zip the user uploaded to Anvil. I want to be able to actually access it and unzip it from Google Colab.

What I’ve tried and what’s not working:
I’ve tried creating a temporary URL from both Anvil and Google Colab. Both have given me the same error “No such method called TempURL in anvil.media”. I have tried doing anvil.server function calls with no success. I’ve looked all over the forums trying to find a solution but I feel like I’ve exhausted all that I can.

If anyone is able to respond, I’d be fully willing to show my code.

Welcome to the Forum!

I’ve zero experience with Google Colab, but I have written Anvil Server-side code to unzip zip files that I’ve stored in one of our Anvil Tables. This statement

isn’t clear to me. Does this mean that you are asking Google Colab to do the unzipping?

2 Likes

Oh thank you so much for responding! Yes! Google Colab would do the unzipping of the file. I was just confused on how the user (from Anvil’s side) would upload their file and I would be able to retrieve it from my server side (Google Colab). Hopefully that makes sense :sweat_smile:. I’ll include my code and pictures if it would be more helpful.

Anvil Client Side:

def upload_dataset(self, file, **event_args):
“”“This method is called when a new file is loaded into this FileLoader”“”
c = self.zipUploader ← this is what I named my uploader component
anvil.server.call(‘download_dataset’, c)

Google Colab (Server Side):
import anvil.server
import anvil.media
import zipfile

anvil.server.connect(‘my_token’)

@anvil.server.callable
def download_dataset(zip_media_object): <–this function is where I want to retrieve the zip file from Anvil
with anvil.media.TempFile(zip_media_object) as zip_file_name:
zip_ref = zipfile.ZipFile(zip_file_name, ‘r’)
zip_ref.extractall(“./”+ zip_file_name)
zip_ref.close()

I have the same problem, but in my code it reads only the first image from the zip both in colab and anvil. How can I handle the problem?

Your code only reads the first image? That’s a lot more progress than me haha. What does your code look like if you don’t mind me asking?

I’m currently getting this error:
AttributeError: 'FileLoader' object has no attribute 'get_bytes'

*** at /usr/local/lib/python3.10/dist-packages/anvil/media.py:21**

It says that the files send by anvil aren’t iterable. I tried to save get_bytes() in one of the labels and it gave the same answer for just one image

Have you tried to send just one file then return that from colab?

I have done that in the following way

in server module I have tis function

def file_loader_1_change(self, file, **event_args):
    """This method is called when a new file is loaded into this FileLoader"""
    if file.name.endswith('.zip'):
        anvil.server.call('download_dataset', file)
    else:
        alert("Please select a ZIP file.")

and this part is in colab, the zip files were apploaded there

import anvil.server
import anvil.media
import zipfile

@anvil.server.callable
def download_dataset(zip_media_object):
with anvil.media.TempFile(zip_media_object) as zip_file_name:
zip_ref = zipfile.ZipFile(zip_file_name, ‘r’)
zip_ref.extractall(“./”+ zip_file_name)
zip_ref.close()