Skip to content

Commit

Permalink
Get source directly as micropython does not have source
Browse files Browse the repository at this point in the history
  • Loading branch information
laffra committed Nov 15, 2023
1 parent bbbdb90 commit 6bd2c14
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 36 deletions.
12 changes: 6 additions & 6 deletions examples/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from examples import table
from examples import tictactoe

elements = [
helloworld.create(),
tictactoe.create(),
table.create(),
custom.create(),
app.create(),
items = [
("examples/helloworld.py", helloworld.create()),
("examples/tictactoe.py", tictactoe.create()),
("examples/table.py", table.create()),
("examples/custom.py", custom.create()),
("examples/app.py", app.create()),
]
10 changes: 5 additions & 5 deletions examples/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import inspect
import ltk
import js

def create():
handler = ltk.proxy(lambda item: js.alert(item.label))
def handler(item):
ltk.find("#right").append(ltk.Text(f"Menu item: {item.label}").element)
return (
ltk.VBox(
ltk.MenuBar(
Expand All @@ -18,16 +17,17 @@ def create():
).css("background-color", "lightblue"),
ltk.HBox(
ltk.VBox(ltk.Text("Left Panel"))
.attr("id", "left")
.css("border-right", "2px solid lightgray")
.css("padding", "50px 20px")
.css("background-color", "lightyellow")
.css("width", "20%"),
ltk.VBox(ltk.Text("Right Panel"))
.attr("id", "right")
.css("padding", "50px 20px")
.css("background-color", "lightgreen")
.css("width", "80%"),
).css("border-top", "2px solid lightgray")
)
.attr("name", "Application") # example
.attr("src", inspect.getsource(create)) # example
.attr("name", "Application")
)
5 changes: 1 addition & 4 deletions examples/custom.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# LTK - Copyrights Reserved 2023 - chrislaffra.com - See LICENSE

import inspect
import ltk


def create():
class CustomWidget(ltk.VBox):
classes = [ "custom Widget" ]
Expand All @@ -26,6 +24,5 @@ def __init__(self, src, label):
.css("margin-top", 20),
ltk.H4("Tip: drag the card."),
)
.attr("name", "Custom Widget") # example
.attr("src", inspect.getsource(create)) # example
.attr("name", "Custom Widget")
)
5 changes: 1 addition & 4 deletions examples/helloworld.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# LTK - Copyrights Reserved 2023 - chrislaffra.com - See LICENSE

import inspect
import ltk


def create():
return (
ltk.Text("Hello World 🎉")
.css("padding", "100px 10px")
.css("background-color", "orange")
.css("font-size", 42)
.attr("name", "Hello World") # example
.attr("src", inspect.getsource(create)) # example
.attr("name", "Hello World")
)
4 changes: 1 addition & 3 deletions examples/table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# LTK - Copyrights Reserved 2023 - chrislaffra.com - See LICENSE

import inspect
import ltk

def get_averages():
Expand Down Expand Up @@ -43,6 +42,5 @@ def create():
),
ltk.H4("Tip: resize the country column using the orange handle."),
)
.attr("name", "HTML Tables") # example
.attr("src", inspect.getsource(create)) # example
.attr("name", "HTML Tables")
)
5 changes: 1 addition & 4 deletions examples/tictactoe.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# LTK - Copyrights Reserved 2023 - chrislaffra.com - See LICENSE

import inspect
import ltk


def create():
def choose(event):
if not ltk.find(event.target).text():
Expand Down Expand Up @@ -32,8 +30,7 @@ def enter(event):
),
ltk.H4("Tip: Click inside the squares."),
)
.attr("name", "Tic Tac Toe") # example
.attr("src", inspect.getsource(create)) # example
.attr("name", "Tic Tac Toe")
)

ltk.inject(__file__, "tictactoe.css")
8 changes: 4 additions & 4 deletions ltk/jquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def repeat(function, timeout_seconds=1):
def repeat(function, timeout_seconds=1.0):
js.setInterval(proxy(function), timeout_seconds * 1000)

def get(route, handler):
wrapper = proxy(lambda data, status, xhr: handler(data.to_py()))
return jQuery.get(route, wrapper, "json")
def get(route, handler, kind="json"):
wrapper = proxy(lambda data, *rest: handler(data if isinstance(data, str) else data.to_py()))
return jQuery.get(route, wrapper, kind)

def delete(route, handler):
wrapper = proxy(lambda data, status, xhr: handler(data.to_py()))
wrapper = proxy(lambda data, *rest: handler(data.to_py()))
return js.ajax(route, "DELETE", wrapper)

def post(route, data, handler):
Expand Down
5 changes: 2 additions & 3 deletions ltk/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ def __init__(self, *tabs):

def add_tab(self, tab):
tab_id = f"{self.name}-{self.labels.children().length}"
print("add tab", tab, tab.attr("name"))
self.labels.append(
LI().append(Link(f"#{tab_id}").text(tab.attr("name")))
)
Expand Down Expand Up @@ -293,8 +292,8 @@ def show(self, element):
find("#main").css("opacity", 0.3)
(self
.appendTo(body)
.css("top", element.offset().top + 28)
.css("left", min(element.offset().left, body.width() - self.width() - 10))
.css("top", element.offset().top + 32)
.css("left", min(element.offset().left, body.width() - self.width() - 12))
.addClass("ltk-menupopup-open")
)

Expand Down
14 changes: 11 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ def cleanup(src):
if not "# example" in line
])

def getsource(file):
def setsource(src):
ltk.find(f'textarea[file="{file}"]').val(src)

ltk.get(file, setsource, "html")
return file

ltk.find("#progress").remove()

ltk.body.append(
Expand All @@ -18,16 +25,17 @@ def cleanup(src):
example.css("width", "40%"),
ltk.VBox(
ltk.H2("The source:"),
ltk.TextArea(cleanup(example.attr("src")))
.css("height", 500)
ltk.TextArea(getsource(file))
.attr("file", file)
.css("height", 800)
.css("border-width", 0)
.css("font-family", "Courier")
)
.css("width", "60%")
.css("padding-left", 24)
.css("border-left", "2px solid lightgray"),
).attr("name", example.attr("name"))
for example in examples.elements
for file, example in examples.items
).css("margin-bottom", 24),
ltk.Link("https://github.com/laffra/ltk", ltk.Text("source"))
.attr("target", "_blank")
Expand Down

0 comments on commit 6bd2c14

Please sign in to comment.