How to download a file created and saved by the script?

I need to enable the User to download an Excel file that my code saved in the app folder as nom_excel + ‘.xlsx’. (nom_excel being a string).

Before publishing the app, it worked on my machine with the following code :

database_to_dl = File.from_path((Path(__file__).parent / nom_excel).with_suffix('.xlsx'))

return DownloadResult(database_to_dl, 'Synthese ' + params.input_1 + '.xlsx')

where the file at (Path(file).parent / nom_excel).with_suffix(‘.xlsx’) was created just before by a call to pd.ExcelWriter.

I suspect that once published, saving the file and then creating a File object from it doesn’t work anymore ?

Hi @SolalSPC ,

Welcome to the community! And thank you for posting your question here. You are correct in saying that saving a file inside a folder does not always work in production. Depending on whether you want to use the file directly in a single calculation, or store for a subsequent calculation, the recommended way would be to either convert the file directly from memory to the format that is allowed to be passed through to the DownloadResult, or you could pass the file to storage, more info here:

Let me know if this helps.

1 Like

I think this could also be a valuable resource to read @SolalSPC: Managing files | VIKTOR Documentation


@matthijs @mslootweg
Thanks guys, after reading your recommendations, i found a way to make it work by writing my Excel file to a BytesIO object that i then fed to render_spreadsheet without filling any cells.

1 Like