How to upload folder on Anvil Form

I want to take input folder containing images but it just select 1 image at a time by using file loader but i want entire folder to be uploaded as my input to train images so how we can do that?

You could try uploading a zip file.

3 Likes

The file uploader component can select multiple files at once if the multiple property is set to True.
You should be able to select multiple files at once in the pop up box using shift / control / command + click keys as a normal operating system function. See docs below:

https://anvil.works/docs/client/components/basic#fileloader

Hope that makes sense.

1 Like

Thanks for suggestion :slight_smile: But i have no idea that how we can upload zip file on anvil form . Can you please guide me…

I would kindly suggest that you begin by googling how to create a zip file (if that is the issue), and then also how to unzip it in Python. There will certainly be guides available online.

If that doesn’t help completely, let us know here what you have tried (with code and the errors you receive) and then we can jump in with more specific assistance.

I have successfully read and extract zip file using this code but it takes path of directory where my zip files are located and then extract it in current directory .

So how we can take this directory path as input on anvil form by using File_loader instead of input label.

I would use the file loader to select the zip file, send that file to the Anvil server, and unzip it there.


i got this error when i try to upload zip file using file loader can you please resolve this error.

My python code was successfully reading and extracting my zip file when I gave path of zip file in “file variable” in my python code but now when i try to take zip file input from anvil using file loader it gives an error.

this my python code

You might want to take a closer read of your actual error message (in red) in your prior post. It shows exactly what’s gone wrong, where, and suggests how to fix it.

Your instance of Form1.file_loader_1_change is receiving a Media object (the actual contents of the selected file) as its file parameter. (See FileLoader to see how this works.) It passes that to your Uplink function, zip.

But that function is not expecting the to receive the contents of the file, as a Media Object. It is expecting to receive a folder name (a string).

As a separate issue: Your screen shot has made your current Anvil Key visible to the general public. I strongly recommend that you reset your key immediately, using the Anvil Editor, as documented under Uplink Security.

2 Likes

please can you provide demo app to upload zip file on data tables and then upzip that file coz i have no idea how to unzip file from data tables.

Not an entire app. A code snippet should suffice.

To be fair, there is a little bit of a trick to it. The value you get out of a FileLoader or database column will be a Media Object, but Python’s zipfile library (and its ZipFile class) naturally doesn’t know anything about Media Objects. So a direct connection between the two isn’t possible. There will be an intermediate step, to convert the Media Object into something that ZipFile does understand: Python’s bytes type.

In the code below, report_blob is a Media Object.

    if report_blob.content_type != 'application/zip':
        return { 'err': 'Not a Zip File.' }
    zip_as_in_memory_file = io.BytesIO(report_blob.get_bytes())
    z = zipfile.ZipFile(zip_as_in_memory_file)

At that point, z is a ZipFile instance, which you already know about.

I hope this helps.

1 Like

Thanks Alot i will surely try this :blush:

I successfully uploaded .zip file on data table and now I want to unzip it . But how to get access of data table in jupyter notebook to unzip my .zip file .

Can you please guide me so I can use this code in jupyter notebook which you provided me.

Or is their any alternative to unzip my zip file in data table and read that unzip file on anvil form.

I recommend the following links:
From Anvil’s Docs (the Uplink: Code outside Anvil section):
Connecting to Data Tables
Tutorial: Using the Uplink

These are things you could have readily found for yourself, using the links at the top of the page. I encourage you to use them, especially the Search feature. You can often find answers for yourself in a fraction of the time.

This post appears to sufficiently answer the initial question and has expanded to another topic. I’ll close for now.

Thanks very much @p.colbert for providing such thorough assistance. It is sincerely appreciated.

1 Like