|
1 | 1 | # ltk |
2 | 2 | LTK is a little toolkit for writing UIs in PyScript |
| 3 | + |
| 4 | +LTK is implemented as a declarative Python library and leverages `jQuery` for DOM operations. |
| 5 | + |
| 6 | +## Widget Specification |
| 7 | + |
| 8 | +New widget types are created by symply subclassing `ltk.Widget`: |
| 9 | + |
| 10 | +```python |
| 11 | +class HBox(Widget): |
| 12 | + classes = [ "ltk-hbox" ] |
| 13 | +``` |
| 14 | + |
| 15 | +By default, widgets are created as `div` DOM elements. You can choose a different tag: |
| 16 | + |
| 17 | +```python |
| 18 | +class Preformatted(Widget): |
| 19 | + classes = [ "ltk-pre" ] |
| 20 | + tag = "pre" |
| 21 | +``` |
| 22 | + |
| 23 | +## Creating a UI |
| 24 | + |
| 25 | +To create a UI, elements are constructed declaratively: |
| 26 | + |
| 27 | +```python |
| 28 | +ltk.Table( |
| 29 | + ltk.TableRow( |
| 30 | + ltk.TableHeader("header1") |
| 31 | + ltk.TableHeader("header2") |
| 32 | + ), |
| 33 | + [ |
| 34 | + ltk.TableRow( |
| 35 | + ltk.TableData(value1), |
| 36 | + ltk.TableData(value2), |
| 37 | + ) |
| 38 | + for value1, value2 in data |
| 39 | + ], |
| 40 | +) |
| 41 | +``` |
| 42 | + |
| 43 | +Widgets are added to others by using jQuery's `append` and `appendTo` calls: |
| 44 | +```python |
| 45 | +ltk.body.append( |
| 46 | + ltk.Table(...) |
| 47 | +) |
| 48 | + |
| 49 | +container = ltk.VBox(...) |
| 50 | +ltk.H1("This is a header").appendTo(container) |
| 51 | +``` |
| 52 | + |
| 53 | +## Styling |
| 54 | + |
| 55 | +Widgets can be styled using element styles: |
| 56 | +```python |
| 57 | +ltk.Text("Some text") |
| 58 | + .css("background-color", "red") |
| 59 | + .css("color", "white") |
| 60 | + .css("padding", 8) |
| 61 | +``` |
| 62 | + |
| 63 | +Widgets can also be styled using an external stylesheet: |
| 64 | +```python |
| 65 | +ltk.Text("Some text").addClass("my-special-text) |
| 66 | +``` |
| 67 | + |
| 68 | +```css |
| 69 | +.ltk-text { |
| 70 | + font-family: Arial; |
| 71 | +} |
| 72 | + |
| 73 | +.my-special-text { |
| 74 | + font-family: Courier; |
| 75 | + background-color: red; |
| 76 | + color: white; |
| 77 | + padding: 8px; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Events |
| 82 | + |
| 83 | +Event handlers are attached using jQuery mechanisms. As the functions cross PyOdide and JavaScript namespaces, they need to be wrapped with `pyodide.ffi.create_proxy` calls. We use the shortcut offered by `ltk.proxy`: |
| 84 | +```python |
| 85 | +def buy(event): |
| 86 | + purchase(...) |
| 87 | + |
| 88 | +Card("Buy Now").on("click", ltk.proxy(buy)) |
| 89 | +``` |
| 90 | + |
| 91 | +## Examples |
| 92 | + |
| 93 | +See the `LTK Kitchen Sink` or explore the `examples` folder |
| 94 | + |
| 95 | + |
| 96 | +## License |
| 97 | + |
| 98 | +LTK is covered under the Apache License: |
| 99 | + |
| 100 | + - The Apache license is a permissive open source software license. |
| 101 | + |
| 102 | + - It allows users to freely use, modify, and distribute the software (including for commercial purposes). |
| 103 | + |
| 104 | + - Modified versions can be distributed without having to release the source code. Though source code changes should be documented. |
| 105 | + |
| 106 | + - The license requires modified versions to retain the Apache license and copyright notice. |
| 107 | + |
| 108 | + - The software is provided by the authors "as is" with no warranties. |
| 109 | + |
| 110 | + - Users are not granted patent rights by contributors, but contributors cannot revoke patent grants for previous contributions. |
| 111 | + |
| 112 | + - The license does not require derived works to adopt the Apache license. Though this is encouraged for consistency. |
| 113 | + |
| 114 | + |
0 commit comments