param.reactive.reactive_ops.where#

reactive_ops.where(x, y) rx[source]#

Return either x or y depending on the current state of the expression.

This method implements a reactive version of a ternary conditional expression. It evaluates the current reactive expression as a condition and returns x if the condition is True, or y if the condition is False.

Parameters:
  • x (object) – The value to return if the reactive condition evaluates to True.

  • y (object) – The value to return if the reactive condition evaluates to False.

Returns:

A reactive expression that evaluates to x or y based on the current state of the condition.

Return type:

rx

Examples

Use .where to implement a reactive conditional:

>>> import param
>>> rx_value = param.rx(True)
>>> rx_result = rx_value.rx.where("Condition is True", "Condition is False")

Check the result when the condition is True:

>>> rx_result.rx.value
'Condition is True'

Change the reactive condition and observe the updated result:

>>> rx_value.rx.value = False
>>> rx_result.rx.value
'Condition is False'

Combine .where with reactive expressions for dynamic updates:

>>> rx_num = param.rx(10)
>>> rx_condition = rx_num > 5
>>> rx_result = rx_condition.rx.where("Above 5", "5 or below")
>>> rx_result.rx.value
'Above 5'

Update the reactive value and see the conditional result change:

>>> rx_num.rx.value = 3
>>> rx_result.rx.value
'5 or below'