Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example: custom binding #1581

Merged
merged 2 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions examples/custom_binding/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python3
import random
from typing import Optional

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, value: sender.on_background_change(value))

def __init__(self, text: str = '') -> None:
super().__init__(text)
self.background: Optional[str] = None # initialize the background property

def on_background_change(self, bg_class: 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_class)
self.update()


temperatures = {'Berlin': 5, 'New York': 15, 'Tokio': 25}
ui.button(icon='refresh', on_click=lambda: temperatures.update({city: random.randint(0, 30) for city in temperatures}))


for city in temperatures:
label = colorful_label().classes('w-48 text-center') \
.bind_text_from(temperatures, city, backward=lambda t, city=city: f'{city} ({t}°C)')
# Bind background color from temperature.
# There is also a bind_to method which would propagate changes from the label to the temperatures dictionary
# and a bind method which would propagate changes both ways.
bind_from(self_obj=label, self_name='background',
other_obj=temperatures, other_name=city,
backward=lambda t: 'bg-green' if t < 10 else 'bg-yellow' if t < 20 else 'bg-orange')

ui.run()
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ async def index_page(client: Client) -> None:
'[zauberzeug/nicegui](https://hub.docker.com/r/zauberzeug/nicegui) docker image')
example_link('Download Text as File', 'providing in-memory data like strings as file download')
example_link('Generate PDF', 'create SVG preview and PDF download from input form elements')
example_link('Custom Binding', 'create a custom binding for a label with a bindable background color')

with ui.row().classes('dark-box min-h-screen mt-16'):
link_target('why')
Expand Down
Loading