Why did anvil.media.from_file stop working here?

Continuing the discussion from:

I also see that error now. Maybe there is a bug involved?

Hi and thanks for speedy response :slight_smile:
I’ve also been playing around with xlsxwriter and am bumping into the same problem. I think it has something to do with trying to write the excel file in the same location as the library (eg xlsxwriter or openpyxl) files? I would have thought the from_file method would have worked around this…like you say…maybe a bug.

But in general, it would be handy to know exactly how to make a document or spreadsheet and save it somewhere for later download, download it or mail it perhaps.

1 Like

The only durable place to keep it would be in the app’s database. From there, downloading it and/or emailing it have dozens of examples, in the docs and this very forum, so I’m not going to repeat them.

From the docs on writing to disk:

https://anvil.works/docs/working-with-files/media/files_on_disk#read-and-write-files-as-normal
Files in your filesystem are temporary, and may be removed without warning. We advise you only to access files in the /tmp directory.

The above code works for me on the full python runtime but not in the python 3.10 runtime.

So I’d suggest changing the code

    final = WB.save("/tmp/doc.xlsx")
    app_tables.table_1.add_row(doc=anvil.media.from_file("/tmp/doc.xlsx"))  

I also like this version:

    with anvil.media.TempFile() as f:
        final = WB.save(f)
        app_tables.table_1.add_row(doc=anvil.media.from_file(f, name="doc.xlsx"))   

If you don’t provide a media object to TempFile you just get a temp file location you can use within a with block

1 Like

Hi @stucork

Many thanks, this does indeed solve the problem. Reason I’m replying here is that it bumps into a further problem when I try scale the written excel file. Anything past 15MB throws:
anvil.server.ExecutionTerminatedError: Server code exited unexpectedly: fd30f87720
when closing (xlsxwriter) the file, prior to saving/sending/downloading or whatever.
Is there a file size limit to the tmp folder?

This post already has an answer.
You should create a new post with the correct subject.

2 Likes