Can anyone please help me to solve this error?
TypeError: ‘module’ object is not callable at , line 6 called from [Form1, line 19](javascript:void(0))
This is my Anvil Code:
results = anvil.server.call(‘unzip’,self.file_loader_1.file,self.text_box_1.text)
And this is my Python Code:
import zipfile as ZipFile
import anvil.media
@anvil.server.callable
def unzip(a,b):
with ZipFile(a, ‘r’) as zip_ref:
input = zip_ref.extractall(b)
Hi @fatimamahmood717
In your server code, you import the zipfile module and alias it as ‘ZipFile’
You then call that alias on line 6. As the error says, a module is not callable, so that fails.
I suspect your import should instead be:
from zipfile import ZipFile
But now i got this error:
AttributeError: ‘StreamingMedia’ object has no attribute ‘seek’ at C:/ProgramData/Anaconda3/lib/site-packages/anvil/init .py, line 48 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 259 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 1321 called from C:/ProgramData/Anaconda3/lib/zipfile.py, line 1258 called from , line 6 called from [Form1, line 19](javascript:void(0))
You’ll need to share your code in order for anyone to have much chance of being able to help here.
2 Likes
Perhaps the library you are using needs a random access file, not a stream.
If that’s the case you could get the media object, save it to file (see here for an example), then feed the file to your library.
1 Like
Alternatively, if it’s small enough to fit in memory, you can try this approach:
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, r…
What I’m trying to do:
is to upload a zip file using file loader but getting this error
Here is my python code:
from zipfile import ZipFile
import anvil.media
@anvil.server.callable
def unzip(a):
with ZipFile(a, 'r') as zip:
zip.extractall()
Clone link:
https://anvil.works/build#clone:MOMROK6DF7YCGUSM=HL7UHR3CB6ZYCE3GVOXOWXUL
I moved the above post here as they seem to be related to the same issue.
stucork
February 17, 2021, 3:44am
10
thanks for the clone @ayeshazahid913
Always worth checking the documentation here.
ZipFile expects the file name when used as a context manager (or a ZipInfo object):
Source code: Lib/zipfile/ The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this ...
And you’re passing it a media object.
The anvil docs are helpful here and show you how to convert an anvil media object to a file on disk with a filename.
Anvil Docs | Files on Disk
You can then combine the context managers as follows
from zipfile import ZipFile
import anvil.media
@anvil.server.callable
def unzip(zip_media_object):
with anvil.media.TempFile(zip_media_object) as file_name, ZipFile(file_name, 'r') as myzip:
myzip.extractall()
2 Likes