Replies: 1 comment 1 reply
-
Hi @frankvp11, Yes, observables.py isn't documented, yet, because it was only meant for internal use. We needed an alternative to binding, which is rather inefficient for large collections. But I think it is rather stable and should be added to the documentation. Implementing something like Regarding your use case: How important is it to detach components? Without this requirement you could introduce a simple data model and bind UI elements against it. @dataclass
class State:
index: int
def increment_index(self, step: int):
self.index += step
class UiComponent:
def __init__(self, name: str, state: State):
ui.label().bind_text_from(state, "index", backward=lambda x: f"{name}: {x}")
state = State(index=10)
UiComponent('A', state)
UiComponent('B', state)
ui.button("Previous Value", on_click=lambda: state.increment_index(-1))
ui.button("Next Value", on_click=lambda: state.increment_index(1)) Long story short, I'm not sure if we really need more observables, or if something like "unbinding" elements is what you actually need. |
Beta Was this translation helpful? Give feedback.
-
Currently we have the observables.py, which isn't documented and only has support for collection like objects (list, dict, set).
Will there / is there a way that we can add support for singular variables also?
I realize that you can easily just make an observable dict with a 'random' key, and your variable as the value, but this feels uninspired. Perhaps that's how a ObservableVariable class (or otherwise named) might be made, but I think that support for this would also be nice.
With that, I have another 'question' / subject matter: State management.
I am interested in being able to keep track of the state of my program in a better way. For example, sometimes I might have a variable that is kept across various classes, where the classes are UI elements like headers, footers, cards, etc. When I change the variable in one place, I'd like to be able to change the variable in all places right away (or have the option to 'not' change it), without explicitly doing it.
With that, I've created two examples, from which I hope to make improvements. One thing to note is, this example is quite simplistic, so you might not see the use in it right away, but when you have more than 5 components each trying to use the same, persistent variable it get's annoying to do it the second way.
Firstly, I'd like to share my 'mock' API for ObservableVariable (or rather ObservableInt):
Then, we have the first example, which is using the ObservableInt to automatically update all instances of a variable:
You can try playing around with the page a bit - notice how the header and footer both change when you increment / decrement without me explicitly saying that it should increment/decrement both. Also, you'll notice the "Detach footer" button. This button makes it so that the footer no longer updates when the variable changes.
Then, there is the 'dummy' way of doing this:
So what do I want to come from this?
I'd like to know what the best practice is in managing state across various classes using NiceGUI in a way where I'm not explicitly stating it.
Beta Was this translation helpful? Give feedback.
All reactions