Creating a zip file from a directory in /tmp

What I’m trying to do:
I want to create a zip file out of a directory inside the /tmp folder.

What I’ve tried and what’s not working:
I have tried using shutil to create a directory, and passing the file from the server to a download button in the user form.

Code Sample:

# this is a formatted code snippet.
Client form:
anvil.media.download(anvil.server.call('get_zip', path.user_path))

Server:
def get_zip(path):
    my_zip = shutil.make_archive('Motor Data', 'zip', f'/tmp/{path}')
    return my_zip
# paste your code between ``` 

The error I get is:

AttributeError: ‘str’ object has no attribute ‘url’

Ah, but where is that error occurring? Which file and which line#? That should be the first place to look.

The solution is:

On the client side form:

import anvil.media

def button_download_click(self, **event_args):
    anvil.media.download(anvil.server.call('make_zip', path'))

On the server side:

import anvil.media
import shutil

@anvil.server.callable
def make_zip(path):
    my_zip = shutil.make_archive('Zip_file_name', 'zip', f'/tmp/{path}')
    tmp_file = anvil.media.from_file(f'{my_zip}', 'text/plain')
    return tmp_file

This should make a zip file of a directory in your /tmp file space and download it. (make sure the directory exists before zipping it)

1 Like