Blobmedia question

I’m trying to store a .pptx file that can be downloaded later. I have a big script I currently use locally that takes a few variables and creates a powerpoint presentation based upon that. I want to make a front end for it so users can do this themselves. I’ve converted the script to a server module but I’m not fully sure how the blobmedia works

On the local version I would end the script with prs.save(“file-location”). I would assume I’d have to something like prs = anvil.Blobmedia('PPTX', 'presentation', name="presentation.pptx") but I’m having difficulty fully understanding how I would need to set this up from the documentation available.

  • What’s the second variable in Blobmedia?
  • Where does it save the item?
  • How would I make the file available for download after this?

Sorry for all the questions, I’m quite new to python.

I’ve shared a copy of the app but I’m currently also having some version/package issue so not sure if I have that fixed on time.

Thanks in advance!

Clone link:
share a copy of your app

I’m going to give you the answer(s) to the question(s) you asked, but it wont really help you to do what I think you want to do.

  1. The second parameter in the constructor for an anvil.Blobmedia object is the byte string of the file you want to pass to it. (The actual content of the file)

The first parameter is the content_type, this is usually a MIME type value, which in your case im guessing is not actually ‘PPTX’ but the mime type of:

'application/vnd.openxmlformats-officedocument.presentationml.presentation'

From the docs:

  1. Saving the item is not going to work the way you are describing, if you would like to save the file for later download, you would typically build the file (on disk or in memory), then convert it to an anvil media object, (like blobmedia) to store it in a data table (db) for later retrieval.

  2. If you are creating a specific, specialized, never-to-be-downloaded-again file for the current user of the client browser, you would do all of the same as above but instead of storing it in a data table, you would return it to the client browser and use a file downloader or attach the medial file to a link for the user to download to their local device. If you were going to store it in a data table, you would do the same but just retrieve the correct cell from the correct row of the data table that contains the anvil.Media object.

1 Like

I cloned your project to look at the code, at the end, where you are doing:

prs = anvil.Blobmedia('PPTX', 'presentation', name="presentation.pptx")

Is overwriting the entire presentation with a BlobMedia object, that contains nothing except the string "presentation" .

After reading the docs for python-pptx, The following code will write out the file directly to a BlobMedia object in memory, but what you do with it afterwards, you are going to have to figure out.

At the top of the server module:

from io import BytesIO

At the end of your code, replacing the part marked # Save File :

file_handler_object = BytesIO(b'')

prs.save(file_handler_object)

file_handler_object.seek(0)

file_as_blob_media = anvil.BlobMedia( 
    'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    file_handler_object.read(),
    name="presentation.pptx"
                                      )
#  Return the media object or store it for later or whatever you want
return file_as_blob_media 

4 Likes

Thank you so much! This definitely helps me in the right direction :).

Regarding the first reaction; I indeed need only need to store it temporary so the user can download it in the current session. Will have a look at how the file downloader works.

From within a client form the .download() method on your media object will download it to the users browser.

Then you probably just want your function to return the media object as a result of the anvil.server.call('your_pptx_function_name') and store it in a variable that you can then call the .download() method on it as a media object.

I also noticed your code is quite long, and I am unfamiliar with how long pptx and bigquery takes to load and execute, so if your function takes longer that 30 seconds, it will time out in the anvil server module.
You would then need to look into using a background task.

Search the forum for how to use background tasks because it is out of scope for this question, but it has been answered a whole bunch of times.

1 Like

Thank you very much! Will have a look at all this when I have some spare time.

This worked perfectly with the return + download function. Thank you!

1 Like