Dynamic Dropdown for Inputs
Support dynamic dropdown for inputs based on data from external source.
Use-case
In this guide, we show how you can create a dynamic dropdown list for inputs. The dropdown retrieves the values from an external source. It is possible to do so by storing the values in the KV store, and also to directly integrate the external source with the HTTP Pebble function, http()
.
Update KV store on schedule
To get started, we create a flow that fetches the data from the external source and set the value in the KV store. The value will be in the form of a list of strings.
In this example, the flow fetches data from a PostgreSQL table on an hourly schedule. You can change the cron
property to run at a different frequency depending on how frequently you expect the data at the source to change. If the external source is in a database that supports change data capture, as in this case where we use PostgreSQL table, you can also leverage debezium trigger and immediately update the KV store.
id: update_kv_storenamespace: company.team
tasks: - id: fetch_departments type: io.kestra.plugin.jdbc.postgresql.Query url: "jdbc:postgresql://{{ secret('POSTGRES_HOST') }}:5432/postgres" username: "{{ secret('POSTGRES_USERNAME') }}" password: "{{ secret('POSTGRES_PASSWORD') }}" sql: select department_name from departments fetchType: FETCH
- id: department_key type: io.kestra.plugin.core.kv.Set key: "{{ task.id }}" kvType: JSON value: "{{ outputs.fetch_departments.rows | jq('.[].department_name') }}"
triggers: - id: schedule type: io.kestra.plugin.core.trigger.Schedule cron: "0 */1 * * *"
This is how the KV store will look post execution of the above flow.
Flow supporting Dynamic Inputs
Let us now create the flow that supports dynamic dropdown for inputs powered by the KV store key.
id: dynamic_input_flownamespace: company.team
inputs: - id: department displayName: Department Name type: SELECT expression: "{{ kv('department_key') }}"
tasks: - id: hello type: io.kestra.plugin.core.log.Log message: "The selected department is {{ inputs.department }}"
When you execute this flow, the department
input will have a dropdown that contains the values fetched from the department_key
key in the KV store.
Dynamic Inputs with HTTP function
With the http()
function, you can make SELECT
and MULTISELECT
inputs dynamic by fetching options from an external API. This proves valuable when your data used in dropdowns changes frequently or when you already have an API serving that data for existing applications.
The example below demonstrates how to create a flow with two dynamic dropdowns: one for selecting a product category and another for selecting a product from that category. The first dropdown fetches the product categories from an external HTTP API. The second dropdown makes another HTTP call to dynamically retrieve products matching the selected category.
id: dynamic_dropdownsnamespace: company.teaminputs: - id: category type: SELECT expression: "{{ http(uri = 'https://dummyjson.com/products/categories') | jq('.[].slug') }}" - id: product type: SELECT dependsOn: inputs: - category expression: "{{ http(uri = 'https://dummyjson.com/products/category/' + inputs.category) | jq('.products[].title') }}"tasks: - id: display_selection type: io.kestra.plugin.core.log.Log message: | You selected Category: {{ inputs.category }} And Product: {{ inputs.product }}