Downloading documents generated by Python

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Downloading documents generated by Python

Postby swingman » Wed Sep 04, 2024 4:55 pm

HI all,

I'm on NG-client 2023.03.x.

I have a Python script that generates Word documents. I would like my users to click a button and get the Word document as a download.
It does not seem to be possible to pass the binary data the script generates using application.executeProgram as this returns Strings.

Have anyone done integrations like this? Where do I get Python to save files so they can be read by the Servoy server?

Thanks,
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1479
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Re: Downloading documents generated by Python

Postby mboegem » Wed Sep 04, 2024 5:26 pm

Hi Christian,

As executeProgram returns a string, would it be possible to let Python return the doc as base64?

Then it would be pretty easy:
Code: Select all
var _aBytes = utils.base64ToBytes(_sPythonB64)
         plugins.file.writeFile('myWordDocument.docx', _aBytes)


Otherwise, you should let Python write the file to a location which can be accessed by the application_server.
Then you can just read the bytes from that file and use the same writeFile command as above.
Marc Boegem
Solutiative / JBS Group, Partner
Servoy Specialist
• Servoy Certified Developer
• Servoy Valued Professional
• Freelance Developer

Image
User avatar
mboegem
 
Posts: 1817
Joined: Sun Oct 14, 2007 1:34 pm
Location: Amsterdam

Re: Downloading documents generated by Python

Postby swingman » Thu Sep 05, 2024 8:13 am

Hi Mark,

Thank you for confirming the approach, I seem to have a problem on the Python side, my data does not look Base64 encoded.
Code: Select all
....GOk\xc6\x05\\\x10U\x91R\xbc\xa7\x9fH\xeb^\xb5\'\xb8H\xa5\x8....
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1479
Joined: Wed Oct 01, 2003 10:20 am
Location: London

Re: Downloading documents generated by Python

Postby swingman » Thu Sep 05, 2024 12:13 pm

Got it working now. It turns out the binary data is padded when it is output from Python. There was a
Code: Select all
b'
at the front and a
Code: Select all
'
at the back.
This caused the base64 conversion on the Servoy side to fail.

Thanks for your help, Marc.
Christian Batchelor
Certified Servoy Developer
Batchelor Associates Ltd, London, UK
http://www.batchelorassociates.co.uk

http://www.postgresql.org - The world's most advanced open source database.
User avatar
swingman
 
Posts: 1479
Joined: Wed Oct 01, 2003 10:20 am
Location: London


Return to Programming with Servoy

Who is online

Users browsing this forum: Bing [Bot] and 11 guests