-
-
Notifications
You must be signed in to change notification settings - Fork 634
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import random | ||
|
||
from nicegui import ui | ||
from nicegui.binding import BindableProperty, bind_from | ||
|
||
|
||
class colorful_label(ui.label): | ||
"""A label with a bindable background color.""" | ||
|
||
# this class variable defines what happens when the background property changes | ||
background = BindableProperty(on_change=lambda sender, bg: sender.on_background_change(bg)) | ||
|
||
def __init__(self, text: str): | ||
super().__init__(text) | ||
self.background = None # initialize the background property | ||
|
||
def on_background_change(self, bg: str) -> None: | ||
"""Update the classes of the label when the background property changes.""" | ||
self._classes = [c for c in self._classes if not c.startswith('bg-')] | ||
self._classes.append(bg) | ||
self.update() | ||
|
||
|
||
def shuffle(): | ||
for key in data: | ||
data[key] = random.choice([True, False]) | ||
|
||
|
||
ui.button('shuffle', on_click=shuffle) | ||
data = {} | ||
for k in 'abcde': | ||
data[k] = random.choice([True, False]) | ||
label = colorful_label(k.upper()).classes('w-48 text-center') | ||
# binding from the data to the label | ||
# there is also a bind_to method which would propagate changes from the label to the data | ||
# and a bind method which would propagate changes both ways | ||
bind_from(label, 'background', data, k, backward=lambda x: 'bg-green' if x else 'bg-red') | ||
|
||
ui.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters