Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
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)
3 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é
Contributor III
Contributor III

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