Skip to content

Commit 3dc1f18

Browse files
committed
Update example to show two products
1 parent 396c67c commit 3dc1f18

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

examples/reactive.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
DELIVERY_OPTIONS = ["1-day", "2-day", "pickup"]
66

77
class Product(ltk.Model):
8-
name: str = "Wrench"
9-
price: float = 50.0
108
count: int = 10
9+
name: str = "Screwdriver"
10+
price: float = 50.0
1111
warranty: bool = False
1212
service: bool = True
1313
delivery: int = 1
@@ -24,10 +24,21 @@ def changed(self, name, value):
2424
- Delivery: {DELIVERY_OPTIONS[self.delivery]}
2525
"""
2626

27-
product = Product(count=60)
27+
product1 = Product(name="Wrench", delivery=0)
28+
product2 = Product(name="Drill", count=6, price=500, service=False)
2829

29-
def create():
30-
form = ltk.VBox(
30+
def order_hammers(event):
31+
product2.name = "Hammer"
32+
product2.count = 10
33+
product2.price = 100.0
34+
product2.warranty = True
35+
product2.delivery = 2
36+
product2.service = False
37+
38+
def create_form(name, product):
39+
print("Create form for", product)
40+
return ltk.VBox(
41+
ltk.Label(name),
3142
row(
3243
"Name:",
3344
ltk.Input(product.name).width(300)
@@ -65,31 +76,30 @@ def create():
6576
ltk.Text(product.summary)
6677
)
6778
)
79+
80+
def create():
6881
return (
6982
ltk.VBox(
7083
ltk.Heading2("Reactive LTK Demo"),
71-
form
84+
create_form("Product 1", product1)
85+
.css("border", "2px solid green")
86+
.css("padding", 12)
87+
.css("margin-bottom", 12)
88+
.css("font-size", 24),
89+
create_form("Product 1", product2)
7290
.css("border", "2px solid green")
7391
.css("padding", 12)
7492
.css("font-size", 24)
7593
.attr("name", "Reactive"),
76-
ltk.Button("Buy Hammers", order_hammers)
94+
ltk.Button("Set Product 2 to Hammer", order_hammers)
7795
.css("margin-top", 24)
7896
.css("border-radius", 8)
79-
.css("padding", 12)
97+
.css("padding", 12),
8098
)
8199
.attr("id", "reactive")
82100
.attr("name", "Reactive")
83101
)
84102

85-
def order_hammers(event):
86-
product.name = "Hammer"
87-
product.count = 10
88-
product.price = 100.0
89-
product.warranty = True
90-
product.delivery = 2
91-
product.service = False
92-
93103
def row(label, *widgets):
94104
return ltk.HBox(
95105
ltk.Text(label)

ltk/widgets.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,24 @@ def __init__(self, **kwargs):
549549
getattr(self, name).set_value(value)
550550

551551
def __setattr__(self, name: str, value):
552-
if hasattr(self, name) and isinstance(getattr(self, name), ModelAttribute):
553-
attribute = getattr(self, name)
554-
attribute.set_value(value)
552+
try:
553+
if hasattr(self, name) and isinstance(getattr(self, name), ModelAttribute):
554+
attribute = getattr(self, name)
555+
attribute.set_value(value)
556+
try:
557+
self.changed(name, value)
558+
except Exception as e: # pylint: disable=broad-except
559+
print(e)
560+
else:
561+
object.__setattr__(self, name, value)
562+
except Exception as e: # pylint: disable=broad-except
563+
print("Cannot set attribute", name, value, self, e)
555564
try:
556-
self.changed(name, value)
557-
except Exception as e: # pylint: disable=broad-except
558-
print(e)
559-
else:
560-
object.__setattr__(self, name, value)
565+
import traceback
566+
traceback.print_exc()
567+
except:
568+
pass
569+
raise e
561570

562571
def decode(self, json_encoding: str):
563572
""" Decode the JSON encoding of the model """
@@ -626,7 +635,10 @@ def get_value(self):
626635

627636
def set_value(self, value):
628637
""" Set the value of the attribute """
629-
typed_value = type(self.value)(value)
638+
try:
639+
typed_value = type(self.value)(value)
640+
except:
641+
typed_value = value
630642
if typed_value == self.value:
631643
return
632644
self.value = typed_value

0 commit comments

Comments
 (0)