A generated file is not enough to create your script. You will have to implement a Client which can access a particular activity or activities of a project. We need this in order to specify which environment we want to connect to and also provide access_token so that the script can be authenticated.
A script is always run from inside an activity. So, if you attach your scripts to a template for a particular version, the scripts will be available to run for the activity of that particular version of the template.
You can also attach a script inside an activity.
There are two ways you generate a client :
By specifying empty Config and Client, you tell the python API that you want to use the current environment and logged-in authentication to use. So, the access that you have in KES will be available in python API.
config = Config() client = Client(config)
By Specifying config parameters. This is particularly helpful when you are developing scripts and you are accessing activities/activity which the user will not have access to. Then specifying an access_token help the API to connect to those activities.
Remember, the script can only run inside an activity. There would be a possibility that the activity user doesn't have access to other activities which are being read or manipulated using scripts.
config = Config( kes_service_address='kes-table-service-training.delightfuldesert-b9ce0345.westeurope.azurecontainerapps.io:443', access_token='token') client = Client(config)
So, once the client is created, now you can either open projects, or a single project or just work on a single activity.
For example, if you want the script to read or manipulate only a single and current activity from where the script is being run, then you can use the below function to access the activity.
activity = client.open_activity_by_id()
You don't need to pass the activity_id parameter as it will automatically be taken care of by KES.
This type of working with an activity is particularly helpful when you want to just read/manipulate a single activity. And if you attach a script to a template, then you don't have to worry about adding any activity_id in the scripts.
You can also pass an activity_id of another activity or the same activity in the above code.
activity_id = uuid.UUID('bff7a456-9242-4113-96a1-1cdb409dfca0') activity = client.open_activity_by_id(activity_id=activity_id)
What you saw above was a simple and direct way to work with an activity.
But there is another way you can open an activity by first opening a project and then iterating through activities inside the project.
project = client.open_project_by_id(UUID("870f7d2b-e9e6-4667-aaf4-690996e8ec00")) # by id activity = project.activities[0] # fetches the first activity in the list.
You can also open a project using master project id. Below you will find how to do that.
projects = client.load_project_by_master_id("master666") # provide master project Id. project = projects[0] # select a project from the list if there are multiple projects with the same master project id. activity = project.activities[0] # fetches the first activity.
You can define your own logic on how to select a project or activity from a list. Above we are simply accessing by index.
For a list of projects, we provide below details:
project_id: UUID, project_name: str, project_number: str, master_project_id: str
For activities, we provide below details:
id: UUID, description: str
How to get project_id and activity_id from KES?
You can look at the URL when you are inside a project or an activity.
For ex. :
- Inside a project
- https://training.kes.rhdhv.io/project/48dcbd37-19e2-42e0-8580-dac5d7db3d6c
- 48dcbd37-19e2-42e0-8580-dac5d7db3d6c is the project_id. The UUId presiding project/
- Inside an activity
- https://training.kes.rhdhv.io/project/48dcbd37-19e2-42e0-8580-dac5d7db3d6c/inspection/4efc6346-d956-4e2f-998c-19a01a289df9/category/a88ff2fa-5fd8-49d8-89a2-91b0535a24ea?hideAppBar=0
- 48dcbd37-19e2-42e0-8580-dac5d7db3d6c is the project_id.
- d956-4e2f-998c-19a01a289df9 is the activity_id. The UUID presiding inspection/
So, now you know different ways how to read a project and an activity. Further, we will dive into how you can read and create Assets and Values.
Once you have initialized a client and fetch an activity. You can use that activity to manipulate and read values in KES.
In order to do that first we need to understand a few terminologies and concepts.
row: A row is an Asset Values in KES. So whenever you add any values or an answer in KES, we first create an Asset in backend.
You can picturise the above in below format from the example generated file: Generated File.
Test Asset
string_test_property | decimal_test_property | single_select_test_property | multi_select_test_property |
example answer | 1.0 | YES | CHOICE_0 |
In python client, we always have an array of rows for a table. So, in case of non-repeating assets, you can only add one row but when you fetch and read you will have to use index [0].
In case of repeating asset, you can add multiple rows.
One thing to note is, we don't do an update. In order to update an existing answer, you will first have to delete a row and then create it again with the updated value.
Let's see an example.
# Create a table activity = client.open_activity_by_id() asset_table = activity.build_table(test_asset_table_def) # Read existing rows asset_table.load() # Deleting row if it exists if len(asset_table) > 0: asset_table.clear() # Create a row row = TestAssetRow( single_select_test_property=SingleSelectTestProperty.YES, multi_select_test_property=MultiSelectTestProperty.CHOICE_0 | MultiSelectTestProperty.CHOICE_1, string_test_property="Text", decimal_test_property=12.0 ) # Add the row and get a reference ref = asset_table.append_row(row)
- activity.build_table initializes the table for you which you can use to read rows or create rows for that type of table(Asset).
- asset_table.load() reads all the existing rows if there are any.
- asset_table.clear() deletes all the existing rows if there are any.
- asset_table.append_row(row) creates a row in KES.
create an object of Row Class and fill in the values in order to write answers to KES.
You can also read data from KES once you load it.
For ex. : print(table[0].single_select_test_property) will print YES in the output. Note the way we are accessing members of the table using [0] index. We always need to use index[0] for a non repeating asset, and any index(from 0 to n) for a repeating asset if there are n rows.