function updating
A decorator function that temporarily sets an object's `_updating` flag to True during the execution of a wrapped method, then restores the original flag value.
/tf/active/vicechatdev/panel_edits/custom_tabulator.py
3 - 11
simple
Purpose
This decorator is used to track when an object is in the process of being updated. It's commonly used in UI frameworks like Panel to prevent recursive updates or to signal that an object is currently undergoing changes. The decorator ensures the `_updating` flag is properly restored even if an exception occurs during the wrapped function's execution.
Source Code
def updating(fn):
def wrapped(self, *args, **kwargs):
updating = self._updating
self._updating = True
try:
fn(self, *args, **kwargs)
finally:
self._updating = updating
return wrapped
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
fn |
- | - | positional_or_keyword |
Parameter Details
fn: The function to be wrapped/decorated. Expected to be a method that takes 'self' as its first parameter, followed by any number of positional and keyword arguments. This should be a method of a class that has a '_updating' attribute.
Return Value
Returns a wrapped function that, when called, will execute the original function with the _updating flag management logic. The wrapped function returns None (implicitly), as the original function's return value is not captured or returned.
Dependencies
panel
Required Imports
import panel as pn
Usage Example
python
import panel as pn
class MyComponent:
def __init__(self):
self._updating = False
@updating
def update_value(self, new_value):
# This method will execute with _updating=True
print(f"Updating flag: {self._updating}")
self.value = new_value
print(f"Value updated to: {self.value}")
def check_updating(self):
return self._updating
# Usage
component = MyComponent()
print(f"Before update: {component.check_updating()}") # False
component.update_value(42) # Prints: Updating flag: True
print(f"After update: {component.check_updating()}") # False
Best Practices
- Ensure the class using this decorator has a '_updating' attribute initialized before calling any decorated methods
- This decorator does not return the wrapped function's return value, so it should only be used with methods that don't need to return values
- The decorator uses a try-finally block to ensure the _updating flag is always restored, even if an exception occurs
- This pattern is useful for preventing recursive updates in reactive UI frameworks
- Consider checking the '_updating' flag in other methods to conditionally skip operations during updates
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function guard_execution 49.9% similar
-
function transaction 48.2% similar
-
function with_lock_v1 47.4% similar
-
function disable_constant 47.3% similar
-
function async_action 47.1% similar