Skip to content

Commit b4d05c1

Browse files
committed
Fix MVC - Support latest PyScript - Add more MVC widgets
1 parent 6d36e14 commit b4d05c1

28 files changed

+387
-155
lines changed

examples/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
("examples/styling.py", styling.create()),
2121
("examples/dom.py", dom.create()),
2222
("examples/inputs.py", inputs.create()),
23-
("examples/tictactoe.py", tictactoe.create()),
23+
("examples/mvc.py", mvc.create()),
2424
("examples/table.py", table.create()),
2525
("examples/custom.py", custom.create()),
2626
("examples/app.py", app.create()),
@@ -31,5 +31,5 @@
3131
("examples/canvas.py", canvas.create()),
3232
("examples/pizza.py", pizza.create()),
3333
("examples/splits.py", splits.create()),
34-
("examples/mvc.py", mvc.create()),
34+
("examples/tictactoe.py", tictactoe.create()),
3535
]

examples/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/canvas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44
import random

examples/custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/documentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import inspect
44
import ltk

examples/dom.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

@@ -96,16 +96,15 @@ def color(event):
9696
return (
9797
ltk.VBox(
9898
ltk.Heading1("Dynamic DOM Operations"),
99-
ltk.Text("We're using the following jQuery APIs: ",
100-
ltk.Link("https://api.jquery.com/append", "append"),
101-
ltk.Link("https://api.jquery.com/appendTo", "appendTo"),
102-
ltk.Link("https://api.jquery.com/prepend", "prepend"),
103-
ltk.Link("https://api.jquery.com/eq", "eq"),
104-
ltk.Link("https://api.jquery.com/after", "after"),
105-
ltk.Link("https://api.jquery.com/before", "before"),
106-
ltk.Link("https://api.jquery.com/css", "css"),
107-
ltk.Link("https://api.jquery.com/remove", "remove."),
108-
),
99+
ltk.Text("We're using the following jQuery APIs: "),
100+
ltk.Link("https://api.jquery.com/append", "append"),
101+
ltk.Link("https://api.jquery.com/appendTo", "appendTo"),
102+
ltk.Link("https://api.jquery.com/prepend", "prepend"),
103+
ltk.Link("https://api.jquery.com/eq", "eq"),
104+
ltk.Link("https://api.jquery.com/after", "after"),
105+
ltk.Link("https://api.jquery.com/before", "before"),
106+
ltk.Link("https://api.jquery.com/css", "css"),
107+
ltk.Link("https://api.jquery.com/remove", "remove."),
109108
ltk.Container(
110109
ltk.Button("Append", append),
111110
ltk.Button("AppendTo", append_to),

examples/editor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/inputs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import logging
44
import ltk

examples/mvc.py

Lines changed: 87 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,102 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

5+
DELIVERY_OPTIONS = ["1-day", "2-day", "pickup"]
6+
57
class Product(ltk.Model):
6-
""" This is a model with two fields """
7-
name: str = ""
8-
price: float = 0.0
8+
name: str = "Wrench"
9+
price: float = 50.0
10+
count: int = 10
11+
warranty: bool = False
12+
service: bool = True
13+
delivery: int = 1
14+
summary: str = ""
915

10-
product = Product()
16+
def changed(self, name, value):
17+
if name == "summary":
18+
return
19+
self.summary = f"""
20+
{self.count} * {self.name} =
21+
${round(self.count * self.price):,}
22+
{'including warranty' if self.warranty else ''}
23+
{'with service' if self.service else ''}
24+
- Delivery: {DELIVERY_OPTIONS[self.delivery]}
25+
"""
1126

12-
def clear(event):
13-
product.name = ""
27+
product = Product()
28+
product.count = 60
1429

1530
def create():
31+
form = ltk.VBox(
32+
row(
33+
"Name:",
34+
ltk.Input(product.name).width(300)
35+
),
36+
row(
37+
"Price:",
38+
ltk.Input(product.price).width(300)
39+
.attr("type", "number")
40+
),
41+
row(
42+
"Count:",
43+
ltk.Slider(product.count, 0, 100).width(250)
44+
.css("margin-top", 10),
45+
ltk.Label(product.count)
46+
.css("margin", "5px 15px")
47+
),
48+
row(
49+
"Warranty:",
50+
ltk.Checkbox(product.warranty)
51+
.css("width", 18)
52+
),
53+
row(
54+
"Delivery:",
55+
ltk.Select(DELIVERY_OPTIONS, product.delivery)
56+
.css("margin-top", 2)
57+
.height(24)
58+
),
59+
row(
60+
"Service:",
61+
ltk.Switch("With white gloves:", product.service)
62+
),
63+
ltk.Div("<hr>"),
64+
row(
65+
"Summary:",
66+
ltk.Text(product.summary)
67+
)
68+
)
1669
return (
1770
ltk.VBox(
1871
ltk.Heading2("LTK Model-View Demo"),
19-
20-
ltk.Label("Product Name (as ltk.Input):"),
21-
ltk.Input(product.name)
22-
.attr("placeholder", "Please enter a name")
23-
.css("border", "2px solid red"),
24-
25-
ltk.Break(),
26-
27-
ltk.Label("Product Name (as ltk.Text):"),
28-
ltk.Text(product.name)
72+
form
2973
.css("border", "2px solid green")
30-
.css("height", 34),
31-
32-
ltk.Break(),
33-
ltk.Button("Clear product.name", ltk.proxy(clear)),
74+
.css("padding", 12)
75+
.css("font-size", 24)
76+
.attr("name", "MVC"),
77+
ltk.Button("Buy Hammers", order_hammers)
78+
.css("margin-top", 24)
79+
.css("border-radius", 8)
80+
.css("padding", 12)
3481
)
35-
.css("font-size", 24)
82+
.attr("id", "mvc")
3683
.attr("name", "MVC")
3784
)
85+
86+
def order_hammers(event):
87+
product.name = "Hammer"
88+
product.count = 10
89+
product.price = 100.0
90+
product.warranty = True
91+
product.delivery = 2
92+
product.service = False
93+
94+
def row(label, *widgets):
95+
return ltk.HBox(
96+
ltk.Text(label)
97+
.css("font-size", 16)
98+
.height(33)
99+
.width(120),
100+
*widgets
101+
)
102+

examples/pitch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/pizza.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

5+
class Order(ltk.Model):
6+
pizza_type: str = "Cheese"
7+
specialties: str = "None"
8+
toppings: str = "None"
9+
510
def create():
611

712
@ltk.callback

examples/pubsub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44
import random

examples/pydata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/splits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/styling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/svg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

examples/tictactoe.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
1+
# LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
22

33
import ltk
44

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE -->
1+
<!-- LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE -->
22

33
<!DOCTYPE html>
44
<html lang="en">
@@ -41,8 +41,8 @@
4141
</script>
4242

4343
<!-- Import PyScript - see kitchensink.js for activation -->
44-
<script type="module" src="https://pyscript.net/releases/2023.11.1/core.js"></script>
45-
<link rel="stylesheet" href="https://pyscript.net/releases/2023.11.1/core.css" />
44+
<script type="module" src="https://pyscript.net/releases/2024.10.2/core.js"></script>
45+
<link rel="stylesheet" href="https://pyscript.net/releases/2024.10.2/core.css" />
4646

4747
<!-- Import jQuery - used in ltk/jquery.py -->
4848
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>

kitchensink.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def setsource(src):
2727
ltk.find("#progress").remove()
2828
ltk.find("#title").append(f" took {window.startTime() / 1000:.3f}s to load")
2929

30-
3130
tabs = ltk.Tabs(
3231
ltk.HBox(
3332
example

ltk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
2+
Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
33
44
LTK (Little Toolkit) is a library for building client-side web applications using Python and CSS.
55
"""

ltk/jquery.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
import pyscript # pylint: disable=import-error
1010
from pyscript import window # pylint: disable=import-error
11+
try:
12+
import pyodide # pylint: disable=import-error
13+
except:
14+
pass
1115

1216

1317
__all__ = [
@@ -18,13 +22,12 @@
1822
]
1923

2024

21-
def is_micro_python():
22-
""" Returns True if running on MicroPython, False otherwise. """
23-
return "Clang" not in sys.version
25+
PYODIDE = "Clang" in sys.version
26+
MICROPYTHON = not PYODIDE
2427

2528

2629
def _fix_time_on_micropython():
27-
if is_micro_python():
30+
if MICROPYTHON:
2831
class MonkeyPatchedTimeModuleForMicroPython:
2932
""" Monkey patches the time module on MicroPython. """
3033
clone = MonkeyPatchedTimeModuleForMicroPython()
@@ -252,6 +255,14 @@ def observe(element, handler):
252255
observer.observe(element[0], config)
253256

254257

258+
def error(message):
259+
""" Displays an error message in the browser console. """
260+
if PYODIDE:
261+
import traceback # pylint: disable=import-outside-toplevel,import-error
262+
traceback.print_exc()
263+
print(message)
264+
265+
255266
def proxy(function):
256267
"""
257268
Returns a proxy for the given function.
@@ -260,11 +271,20 @@ def proxy(function):
260271
"""
261272
if not function:
262273
return None
263-
try:
264-
import pyodide # pylint: disable=import-outside-toplevel,import-error
265-
return pyodide.ffi.create_proxy(function)
266-
except: # pylint: disable=bare-except
267-
return function
274+
275+
def function_called_from_javascript(*args):
276+
try:
277+
return function(*args)
278+
except Exception as e: # pylint: disable=broad-except
279+
error(f"Error calling function from JavaScript {function}: {e}")
280+
return None
281+
282+
if PYODIDE:
283+
return pyodide.ffi.create_proxy(function_called_from_javascript)
284+
else:
285+
return function_called_from_javascript
286+
287+
window.proxy = proxy
268288

269289

270290
def get_url_parameter(key):

ltk/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE
2+
LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE
33
"""
44

55
import json

ltk/ltk.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE */
1+
/* LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE */
22

33
.ltk-vbox {
44
display: flex;

ltk/ltk.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* LTK - Copyright 2023 - All Rights Reserved - chrislaffra.com - See LICENSE */
1+
/* LTK - Copyright 2024 - All Rights Reserved - chrislaffra.com - See LICENSE */
22

33
(function ltk() {
44
if (window.__ltk__) return
@@ -125,4 +125,5 @@
125125
const viewportBottom = viewportTop + $(window).height();
126126
return elementBottom > viewportTop && elementTop < viewportBottom;
127127
};
128+
128129
})()

0 commit comments

Comments
 (0)