TABLE OF CONTENTS
- 1) Download the generated script.
- 2) Create a folder or a project in your IDE.
- 3) Add logic in your main.py file
- 4) Sample main.py file
- 5) Upload script to a template
Let's consider a simple example where you want to calculate area of a site, provided length and width of the site.
You create a category in KES and add a Site Asset(Sub Category).
Add properties Length, Width and Area.
The user will provide Length and Width, and the Area is calculated using the script and all the values are pushed to KES using the kes-python-client.
You can either publish the template or you can verify the changes using the unpublished template. Let's see how to verify your changes using the script before publishing it.
The better way would be to create separate Asset(Sub Category) for reading values and writing values from/to KES. So, in an ideal scenario, we should have two Assets, one with property Length and Width. And the other with Area.
1) Download the generated script.
You can download your generated script for the unpublished template from the template editor screen, by clicking on the three vertical dot menu and Download script.
You will get a file with name generated.py.
You can rename the file if you want, but for now we will just let it remain as it is.
2) Create a folder or a project in your IDE.
Now that you have got the generated file, you need to create a folder where you can store all your Python files.
We will create one file main.py in the folder, as main.py is always the starting point of your scripts. Please note to create main.py in the root folder.
3) Add logic in your main.py file
At the moment, you have an empty main.py file. Let's start adding logic to your scripts. Please refer Script Anatomy and Develop, Upload and Run Scripts for more details.
I will be testing the logic in my local machine by connecting to the training environment, so for that I will need to provide configuration to access KES services.
I will need token which I can get it by previewing the activity and go to the preview project by clicking the rounded Breadcrumb link.
You will go to the below page from where you can obtain the script token which will be used to authenticate you locally. Once you click on Obtain script token, the token will be copied to your clipboard. You can copy it and save it somewhere.
Now that we have token in place, we can start building our logic in our main.py script.
4) Sample main.py file
The below file is use to test the script locally, that's the reason you will see configuration and constant activity_id.
import uuid from kes.client import Client, Config from generated import site_table_def, SiteRow config = Config( kes_service_address='kes-table-service-training.delightfuldesert-b9ce0345.westeurope.azurecontainerapps.io:443', access_token='meEm359HTJ') client = Client(config) activity_id = uuid.UUID('caadb114-9eae-47b4-af5d-24355e9dcec1') activity = client.open_activity_by_id(activity_id=activity_id) table = activity.build_table(site_table_def) table.load() if len(table) == 0: exit("There are no values to read from KES. Please add length and width in KES activity.") read_length = table[0].length read_width = table[0].width print(read_length) print(read_width) calculated_area = read_length * read_width # # Deleting row if it exists if len(table) > 0: table.clear() # Create a row row = SiteRow( length=read_length, width=read_width, area=calculated_area ) ref = table.append_row(row)
Once you have tested it locally and see the area value reflected in KES. Now you can go ahead and test it in KES preview activity by uploading it. Remember we are still testing the script with a preview activity and an unpublished version of the template.
You can either remove the config and activity that you provided or leave it as it is as we are going to connect to that same preview activity in KES. For now, I'll leave it as it is and upload it to KES.
5) Zip the folder and upload it to KES Activity.
Once you have compressed the folder to a Zip file and uploaded to the preview activity. You will see below Run and delete option.
Now you can modify the Length and Width and see if the scripts correctly updates the Area in the activity.
Once you have tested it in a preview activity and if everything looks fine. You can go ahead and publish the template.
5) Upload script to a template
Now, your script is ready to be used. But before uploading to a template, you will need to replace the generated file now with a new generated file as the published template is a different version in KES than an unpublished version.
- Go to the template once it is published and click on download script.
- Once downloaded, you can compare the UUIDs of the properties and Asset, it will indeed be different.
- You just have to replace the generated file.
- You can test it again with an actual activity in local by replacing the token and activity_id.
- But for now, we can just remove the token and activity_id from our code and upload the script to the template.
You can see the after change main.py file below.
from kes.client import Client, Config from generated import site_table_def, SiteRow config = Config() client = Client(config) activity = client.open_activity_by_id() table = activity.build_table(site_table_def) table.load() if len(table) == 0: exit("There are no values to read from KES. Please add length and width in KES activity.") read_length = table[0].length read_width = table[0].width print(read_length) print(read_width) calculated_area = read_length * read_width # # Deleting row if it exists if len(table) > 0: table.clear() # Create a row row = SiteRow( length=read_length, width=read_width, area=calculated_area ) ref = table.append_row(row)
You can compare the file before and after changes, and see that we have removed the Configuration parameters and activity_id. This we have done to ensure that, once uploaded to a template, it can work for any activity that is created for that template and version. We will take care of the backend process to update the activity where it is being run.
Once it is uploaded, now when you create an activity or see an activity that was created earlier before adding the script after publishing. You will see that the script will be available for the user to run in the activity.
After filling in Length and Width, if we run the script it will fill the answer for Area with the calculated value.
Voila, now your template is actually automated. Happy Scripting.