Python

The Python post-processing step allows you to run custom python scripts for your .

By combining it with a bash setup script you can prepare your environment (install dependencies, create folder structures, …). The script runs in an isolated space inside its own docker container.

Please keep in mind that Python support is an preview stage. So expect to encounter issues! If so, please report them to us, thanks!

The functionality of your own scripts cannot be guaranteed in future versions of Portrait.

Environment

The Python Runtime we provide is a customized version of the official Python image. Specifically, we use python:3-alpine as the base image. This means that the specific Python version is determined for each release of Portrait.

In addition, we added the following packages:

  • SSHD server for running the scripts remotely. (Access meant only internally for Portrait Docker services)

    • There are two SSH keys for the communication pythonRuntime_id_rsa.pem (stored in the jar inside the backend container) and pythonRuntime_id_rsa.pub (stored inside the scripting container)

  • Pyenv

  • Pipenv

  • Venv

  • common packages: vim, nano, git, curl, bash

  • some build dependencies are needed for pyenv, pipenv, venv

The container is connected to the same network as the other portrait services. So you are able to access the Portrait API as well as other APIs or web services.

Configuration

onSubmit with Python supports the following settings:

 

 

 

 

onSubmit.type

Must be Python

onSubmit.scriptName

name of the python file.

onSubmit.setupScriptName

name of a bash setup script that is called on each submit before the Python script. Can be used to install dependencies via pip .

 

There is a runtime limit of 15 seconds for the combination of setup script and python script.
This can be changed by overwriting the config property microservices.pythonRuntime.timeoutMs in the YML

Setup Scripts

The setup script needs to be a shell script, which is called each time the form is submitted. The main purpose is to install additional dependencies through pip. The Portrait server will call this script without any parameters.

While the script can be used for any purpose, we recommend keeping it to a minimum and moving your business logic into the actual python script.

Python Scripts

The python script will contain the actual business logic and will be executed after the setup script execution finished.

Preparation

The script will be called via the command line. You can expect the following inputs:

  • --scriptExecutionID 0620f736-68a1-45e7-b411-f71cb7d6ab8c

    • a UUID identifying this specific execution

  • --files /app/fileparameter/0620f736-68a1-45e7-b411-f71cb7d6ab8c/94141861p1-1670253160678.png

    • List of files that were submitted with the form

    • These files are available under the provided path and will be automatically removed after the script is finished. The files will also be removed if the scripts fails/crashes.

  • --args

    • The form submit information formatted as JSON

    • In addition, this JSON will also contain the portrait user information

    • Example

    • { "responses": [ { "values": [ "IssueText" ], "name": "Name" }, { "values": [ "CREATE" ], "name": "portrait-action" }, { "values": [ "2023-05-16T11:34:23.478Z" ], "name": "portrait-timestamp" }, { "values": [ "xiaoNgRRPsqdclKyAvbZkaYmfGiosEYaSoiFSMkNGXFrSSOJAwUH" ], "name": "PORTRAIT_USER_ID" }, { "values": [ "Linda Jackson" ], "name": "PORTRAIT_USER_NAME" }, { "values": [ "linda.jackson@larsens.portraitapp.co" ], "name": "PORTRAIT_USER_EMAIL" }, { "values": [ "ADMIN" ], "name": "PORTRAIT_USER_ROLE" } ] }

Example call

python /app/scripts/demo.py --scriptExecutionID 0620f736-68a1-45e7-b411-f71cb7d6ab8c --files /app/fileparameter/0620f736-68a1-45e7-b411-f71cb7d6ab8c/94141861p1-1670253160678.png /app/fileparameter/0620f736-68a1-45e7-b411-f71cb7d6ab8c/101454734p0-1670253163775.jpg --args '{"responses":[{"values":["Akku Tausch"],"name":"Name"},{"values":["iPhone XR"],"name":"ModelCode"},{"values":["3 - High"],"name":"Priority"},{"values":["94141861p1-1670253160678.png","101454734p0-1670253163775.jpg"],"name":"Photos"},{"values":["CREATE"],"name":"portrait-action"},{"values":["2022-12-05T15:12:44.581Z"],"name":"portrait-timestamp"}]}'

Your custom script

In the script, you could create a PDF, call a web service, or implement business logic to your needs.

You have access to the input files provided via the command line parameters.
In addition, you can import any python libs that are either manually installed on the system or installed in the setup script.

Output

  • If the script returns an Exit code of 0, Portrait will treat it as finished successfully.

  • If the script writes anything to standard error or a non-zero exit status was received, then Portrait will treat it as finished unsuccessfully.

Cleanup

After the script is called, the /app/fileparameter/0620f736-68a1-45e7-b411-f71cb7d6ab8c folder is deleted.

Simple Example

- id: createServiceCallPython onSubmit: type: Python setupScriptName: installDependencies.sh scriptName: demo.py dialog: |- { ... }

Content of the setup script installDependencies.sh:

Content of the script demo.py:

Output

 

Example Use cases

Generate Invoice PDF

setupGenInvoice.sh:


pythonDependencies Folder contains these files:
- Merchant.ttf
- MerchantWide.ttf


genInvoice.py

Viewing Output in VSCode

Currently there is no separated output folder to transfer files between the scripting container and vscode. As an workaround you can save files in ‘/app/dependencies’

The following mounts are in place

Copyright Treskon GmbH.