2024-09-13 13:53:19 +00:00
---
id: run-py
2024-10-15 07:14:16 +00:00
title: Run Python Code
2024-09-13 13:53:19 +00:00
---
2024-10-15 07:14:16 +00:00
In ToolJet, custom **Run Python Code** can be used to interact with components and queries, making it possible to customize actions and data handling.
2024-09-13 13:53:19 +00:00
2024-10-15 07:14:16 +00:00
< img className = "screenshot-full" src = "/img/datasource-reference/custom-python/add-run-py.png" alt = "Run Python code" / >
< div style = {{paddingTop:'24px'}} >
2024-09-13 13:53:19 +00:00
## Using Python Code to Trigger Component Specific Actions
2024-10-15 07:14:16 +00:00
1. Drag a **Text** component onto the canvas. We will set the text on the Text component using the Python query.
2. Create a query and select **Run Python code** from the available data sources
3. Paste the below code in the code editor and save the query:
2024-09-13 13:53:19 +00:00
```python
class Person:
def __init__ (self, name, age):
self.name = name
self.age = age
def myfunc(self):
return "Hello my name is " + self.name
p1 = Person(tj_globals.currentUser.firstName, 36)
components.text1.setText(p1.myfunc())
```
2024-10-15 07:14:16 +00:00
4. The above code has a function `myfunc` which returns a string and we are using a ** [Component Specific Action ](/docs/tooljet-concepts/component-specific-actions )** to set the Text Component's value from the Python query.
2024-09-13 13:53:19 +00:00
:::tip
- As of now, Run Python code only supports the [Python standard library ](https://docs.python.org/3/library/ ).
- Check ** [RunPy Limitations ](/docs/contributing-guide/troubleshooting/runpy-limitations )** to go through the limitations with using Python code
:::
2024-10-15 07:14:16 +00:00
< / div >
< div style = {{paddingTop:'24px'}} >
## Trigger Queries Using Run Python Code
2024-09-13 13:53:19 +00:00
To trigger queries in Python, you can use the below functions:
```py
actions.runQuery('getSalesData')
#replace getSalesData with your query name
```
2024-10-15 07:14:16 +00:00
Or
2024-09-13 13:53:19 +00:00
```py
queries.getSalesData.run()
#replace getSalesData with your query name
```
2024-10-15 07:14:16 +00:00
< / div >
< div style = {{paddingTop:'24px'}} >
2024-09-13 13:53:19 +00:00
## Get Query Data
To immediately access the data returned by a query in **Run Python code** , you can use the below functions:
2024-10-15 07:14:16 +00:00
### Trigger a Query and Retrieve its Data
2024-09-13 13:53:19 +00:00
```py
await queries.getSalesData.run()
#replace getSalesData with your query name
value = queries.getSalesData.getData()
#replace getSalesData with your query name
value
```
2024-10-15 07:14:16 +00:00
### Trigger a Query and Retrieve its Raw Data
2024-09-13 13:53:19 +00:00
```py
await queries.getCustomerData.run()
#replace getCustomerData with your query name
value = queries.getCustomerData.getRawData()
#replace getCustomerData with your query name
value
```
2024-10-15 07:14:16 +00:00
### Trigger a Query and Retrieve its Loading State
2024-09-13 13:53:19 +00:00
```py
await queries.getTodos.run()
#replace getTodos with your query name
value = queries.getTodos.getLoadingState()
#replace getTodos with your query name
value
```
2024-10-15 07:14:16 +00:00
< / div >
< div style = {{paddingTop:'24px'}} >
2024-09-13 13:53:19 +00:00
## Get Variables
To set and access variables or page variables in **Run Python code** , you can use the below functions:
2024-10-15 07:14:16 +00:00
### Set a Variable
2024-09-13 13:53:19 +00:00
```py
actions.setVariable('color','blue')
#replace color with your desired variable name
```
2024-10-15 07:14:16 +00:00
### Immediately Retrieve a Variable After Setting it
2024-09-13 13:53:19 +00:00
```py
actions.setVariable('mode','dark')
#replace mode with your desired variable name
actions.getVariable('mode')
#replace mode with your desired variable name
```
2024-10-15 07:14:16 +00:00
### Set a Page-Specific Variable
2024-09-13 13:53:19 +00:00
```py
2024-10-15 07:14:16 +00:00
actions.setPageVariable('version', 1)
2024-09-13 13:53:19 +00:00
#replace version with your desired variable name
```
2024-10-15 07:14:16 +00:00
### Immediately Retrieve a Page-Specific Variable After Setting it
2024-09-13 13:53:19 +00:00
```py
actions.setPageVariable('number',1)
#replace number with your desired variable name
actions.getPageVariable('number')
#replace number with your desired variable name
```
2024-10-15 07:14:16 +00:00
< / div >
< div style = {{paddingTop:'24px'}} >
2024-09-13 13:53:19 +00:00
## Using Transformations With Python
2024-10-15 07:14:16 +00:00
**Run Python code** can be used to transform the data that is fetched in the queries. To test transformations using Python, create a new **REST API** query, leave the method as *GET* and enter the below url under the **URL** property.
```yaml
2024-09-13 13:53:19 +00:00
https://dummyjson.com/products
```
Click on the **Run** button and check the preview of the returned data, below is the data structure of the response:
```js
products_data = {
"products": [
{"title": "iPhone 9", ...},
{"title": "iPhone X", ...},
# Additional products...
]
}
```
2024-10-15 07:14:16 +00:00
### Filter the Titles From the Response
To extract a list of product titles from the given data structure, we iterate through the *products* list and collect each product's *title* using the below code. Enable **Transformations** in the Query Editor and use the below code:
2024-09-13 13:53:19 +00:00
```python
return [product["title"] for product in data["products"]]
```
2024-10-15 07:14:16 +00:00
### Filter Products by Category
2024-09-13 13:53:19 +00:00
2024-10-15 07:14:16 +00:00
To filter products by a specific category, such as *smartphones* , and extract their titles. Enable **Transformations** in the Query Editor and use the below code:
2024-09-13 13:53:19 +00:00
```python
return [product["title"] for product in data["products"] if product["category"] == "smartphones"]
```
2024-10-15 07:14:16 +00:00
### Calculate Average Price of a Category
2024-09-13 13:53:19 +00:00
2024-10-15 07:14:16 +00:00
To calculate the average price of products within the *laptops* category. Enable **Transformations** in the Query Editor and use the below code:
2024-09-13 13:53:19 +00:00
```python
return sum(product["price"] for product in data["products"] if product["category"] == "laptops") / len([product for product in data["products"] if product["category"] == "laptops"]) if len([product for product in data["products"] if product["category"] == "laptops"]) > 0 else 0
```
2024-10-15 07:14:16 +00:00
< / div >
< div style = {{paddingTop:'24px'}} >
2024-09-13 13:53:19 +00:00
## Refer Python Query Data in Components
Just like other dynamic values, you can refer the data returned by **Run Python code** queries using double curly braces`{{}}`.
2024-10-15 07:14:16 +00:00
For instance, if you have a **Run Python code** query named *updatedProductInfo* , you can pass `{{queries.updatedProductInfo.data}}` under the **Data** property of a Table component to populate it with the data returned by the *updatedProductInfo* query.
:::info
Issues with writing custom Python code? Ask in our [Slack community ](https://www.tooljet.com/slack ).
:::
< / div >