[FIXED] Possible bug in anvil.BlobMedia (encoding, keywords)?

Hi,

text_file = anvil.BlobMedia(content_type=‘text/plain’, content=‘Hello world’, name=‘hello.txt’)

gave the following error:

ExternalError: TypeError: Cannot read property ‘class’ of undefined

I played around with it. This worked:

text_file = anvil.BlobMedia(‘text/plain’, b’Hello world’)

However, using encode on a string and using the string name with the keyword arguments (often!) did not work, but removing the keywords and using only positional arguments, worked!

I wonder if there is something I do not understand, or perhaps there might be a bug here?

It seems related to Python 2 vs. 3 and string encoding.

Here is a test app:

could you post the clone rather than the link to the live app? go to the gear icon and click share rather than publish to get the clone link.

Yes, a clone is more useful:

https://anvil.works/build#clone:VNTHOGNRJFVNVHIO=3M2BRVCDRUCCZTEDWJPJV5Z6

The docs and auto complete say that the content should be a binary string so I think a plain string is causing the error.

It’s not a particularly helpful error message so anvil team will hopefully pick this up and improve it…

Thank you. Yes, the text has to be binary. When I copied the example from the docs:

file_contents = "Hello, world"

my_media = anvil.BlobMedia(content_type="text/plain", content=file_contents, name="hello.txt")

I got an error. Maybe because the example was based on Python 2.7 when the string was binary and in the example above you would have to write

file_contents = "Hello, world".encode('utf8')

But I wonder also if there is something else going on as well, because if you try:

file_contents = "Hello, world".encode('utf8')

my_media = anvil.BlobMedia(content_type="text/plain", content=file_contents, name="hello.txt")

you get an error, but (strangely?) it works if we use positional arguments:

file_contents = "Hello, world".encode('utf8')

my_media = anvil.BlobMedia("text/plain", file_contents, "hello.txt")

Here is a copy of an app that shows this behaviour.

https://anvil.works/build#clone:VNTHOGNRJFVNVHIO=3M2BRVCDRUCCZTEDWJPJV5Z6

It is not a problem once you know it, but it was a bit strange. Anyway, thank you for having a look at this.

definitely a bug I think it’s a couple of bugs…


for the anvil team:

something to do with the name of the variable - it looks like BlobMedia thinks it should be called contentType rather than content_type

but then I get a new error if I use contentType - I think PyDefUtils.withKwargs is resulting in a typecheck failure… it seems to be converting the kwarg value using remapToJs which causes an error when checking if it is a python string or not…


the error message should probably raise an exception if either content or contentType are undefined… currently it only checks content which is why @hans.melberg got the obscure error message…

Thanks @stucork, @hans.melberg we’re looking into this now. Moved to bug reports.

Thanks folks, this is now sorted. The fix will be released in the next few days. The problem was indeed with the content_type keyword argument - using positional arguments should work just fine in the meantime.

1 Like