Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us at Qlik Connect 2026 in Orlando, April 13–15: Register Here!
cancel
Showing results for 
Search instead for 
Did you mean: 
olaoyesunday1
Contributor III
Contributor III

Custome Code

Is it possible to run python code in custom block of the Application Automation? 

 I wrote a custom code to send a visualization  in pdf to more than 100 people in python after reading the visualization from Qlik Cloud, I want to know if I can place the same code in the Application Automation block and run it?

Labels (3)
5 Replies
olaoyesunday1
Contributor III
Contributor III
Author

Thanks for this, before reading all the documentation, what I am trying to achieve is this I want to print a long table of 211 rows that span more than 4 pdf pages but Qlik can only take the snapshot of a visible sheet. I have done it on my desktop with the code below but I want to know how  Ican  place the code inside a custom block. Here is the code:

   

import pytoqlik
import numpy as np
import seaborn        # Seaborn provides us with some sample datasets
import pandas as pd   # We will need pandas to manipulate the extracted DataFrame
import matplotlib.pyplot as plt

from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter, landscape
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table,TableStyle,colors
#from my_table_data import my_data # import the data

key='eyJhbGciOiJFUzM4' # api key
ID='d7a3cf6d-1254-4d41-ad12-e7ca63c59348' #app id

p2q = pytoqlik.Pytoqlik(api_key=key, tenant=url, appId=ID)

# Get data from Qlik Sense chart
table_data=p2q.toPy('FrzkR') #object id
 # Display n rows
pd.set_option('display.max_rows', 211)
table_data
 # Save the dataframe as pdf using reportLab.
pdf = SimpleDocTemplate("long3.pdf", pagesize=landscape(letter))
    #pdf = SimpleDocTemplate("long3.pdf", pagesize=letter)

# Extract column names and data
column_headers = list(table_data.columns)
data_rows = [list(row) for _, row in table_data.iterrows()]
   


# Create the table
table_style = ([
    ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
    ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
    ('FONTSIZE', (0, 0), (-1, 0), 11),
    ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
    ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
    ('TEXTCOLOR', (0, 1), (-1, -1), colors.black),
    ('ALIGN', (0, 1), (-1, -1), 'CENTER'),
    ('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
    ('FONTSIZE', (0, 1), (-1, -1), 12),
    ('BOTTOMPADDING', (0, 1), (-1, -1), 8),
])
c_width = [1*inch, 1*inch,1*inch,1.5*inch,1.5*inch,1.5*inch,1.5*inch,1.5*inch]
table = Table([column_headers] + data_rows, rowHeights=25,repeatRows=1, colWidths=c_width, style=table_style)

# Adding the table to the pdf file
pdf_table = [table]

pdf.build(pdf_table).
 
import win32com.client
ol=win32com.client.Dispatch("office365")
olmailitem=0x0 #size of the new email
newmail=ol.CreateItem(olmailitem)
newmail.Subject= 'Testing Mail'
newmail.To='user1@yahoo.com', ... 'user200@example.com'
#newmail.CC='xyz@example.com'
#newmail.BCC='xyz@example.com'
newmail.Body= 'Hello, this is a test email.'

attach = 'long.pdf'
newmail.Attachments.Add(attach)

# To display the mail before sending it
#newmail.Display()

newmail.Send()
I used pytoQlik library here but which library can I use and how can I connect the App Id from the custom code and the object Id that I want to print in pdf ? then send them to multiple users of over 200 users.
 
André
Creator
Creator

Have a read of this, it is quite limited (you can't install additional packages) 
Custom code block | Qlik Cloud Help

However you can use a third party to run more complex code and use the 'Call URL' block
Custom scripts | Qlik Application Automation for OEM (Blendr.io) Help

Ken_T
Specialist
Specialist

@André do you have an updated link for " third party to run more complex code and use the 'Call URL' block" it does not work anymore.

André
Creator
Creator

Hi @Ken_T,

It was the following:

Externally hosted custom code
The Qlik Application Automation for OEM Custom code block does not support including libraries. If you want to add comprehensive custom code to your automation and include e.g. libraries, you should host your custom code externally.

An easy solution is to use repl.it to host your code. Repl.it offers a free account but you need to upgrade to a paid account in order to make your code private.

Here's an example of a Repl in repl.it of type PHP Webserver to host some PHP code, which is exposed using an HTTPS URL:

PHP code in Repl.it:

$input=json_decode(file_get_contents("php://input")); //get inputs from Blend
//add logic here
echo json_encode($input); //send response back to automation

Copy the unique URL of your Repl and use it in a block Call URL to include it in your automation:

Obviously you will need to add security checks to avoid unauthorized access to your hosted code, and you have to make sure to use a private Repl (requires a paid account).


Essentially using the Call URL block to ping any external source, do *something*, then the external source returns a response which you can then use in the Automation.