Skip to main content

ltk

LTK is a little toolkit for writing web UIs in PyScript. For an explanation of PyScript and LTK, see YouTube.

For examples, see:

  • The LTK kitchensink for a live demo of all the widgets.
  • A Todo App that showcase declarative UIs using Reactive LTK.
  • A Slides Player that downloads a Google Slides presentation and shows it with LTK.
  • Why use LTK? See the pitch, written with LTK.
  • A personal website, chrislaffra.com, that uses several animations, SVG, styling, and timers to render a visual resume.
  • An animated Holiday card where PyScript logo tree ornaments are animated using Python code in the browser using LTK.
  • PySheets, a spreadsheet written in Python, using LTK.

LTK is implemented as a declarative Python library and leverages jQuery for DOM operations.

Installing LTK

Install LTK from pypi:

python3 -m pip install pyscript-ltk

Hello World

import ltk

ltk.Text("Hello World").appendTo(ltk.body)

Getting Started

To get started with LTK, we recommend you try it out on pyscript.com:

Widget Specification

New widget types are created by subclassing ltk.Widget:

class HBox(Widget):
    classes = [ "ltk-hbox" ]

By default, widgets are created as div DOM elements. You can choose a different tag:

class Preformatted(Widget):
    classes = [ "ltk-pre" ]
    tag = "pre"

Creating a UI

To create a UI, elements are constructed declaratively:

ltk.Table(
    ltk.TableRow(
        ltk.TableHeader("header1")
        ltk.TableHeader("header2")
    ),
    [
        ltk.TableRow(
            ltk.TableData(value1),
            ltk.TableData(value2),
        )
        for value1, value2 in data
    ],
)

Widgets are added to others by using jQuery's append and appendTo calls:

ltk.body.append(
    ltk.Table(...).element
)

container = ltk.VBox(...)
ltk.H1("This is a header").appendTo(container)

When an LTK widget is created, a corresponding jQuery element is attached to it in the ltk.Widget.__init__ constructor. It uses the tag value defined by the declaring class and the constructed element is referred to as element. As the append call is a JavaScript function, implemented by jQuery, we do not pass the LTK widget directly, but pass its element to append to the DOM.

Styling

Widgets can be styled using using three different approaches:

  1. Styling with element styles using jQuery's css function:
ltk.Text("Error: Flux capacitor low!")
    .css("background-color", "red")
    .css("color", "white")
    .css("padding", 8)
  1. Styling using a dict to make it easier to share styles:
error = {
    "background-color": "red",
    "color": "white",
    "padding": 8,
}
ltk.Text("Error: Flux capacitor low!", error)
  1. Styling using CSS classes and an external stylesheet, using jQuery's addClass function:
ltk.Text("Some text").addClass("my-special-text)

The external style sheet should have these rules:

.ltk-text {
    font-family: Arial;
}

.my-special-text {
    font-family: Courier;
    background-color: red;
    color: white;
    padding: 8px;
}

External stylesheets can be included in the original index.html or injected at runtime from Python using:

ltk.inject_style("https://example.org/awesome_styles.css")

Events

Event handlers are attached using jQuery mechanisms.

def buy(event):
    purchase(...)

ltk.Card("Buy Now").on("click", ltk.proxy(buy))

You can also use the more declarative decorator:

@ltk.callback
def buy(event):
    purchase(...)

ltk.Card("Buy Now").on("click", buy)

Examples

See the LTK kitchensink or explore the examples folder

Applications built using LTK

License

LTK is covered under the Apache License:

  • The Apache license is a permissive open source software license.

  • It allows users to freely use, modify, and distribute the software (including for commercial purposes).

  • Modified versions can be distributed without having to release the source code. Though source code changes should be documented.

  • The license requires modified versions to retain the Apache license and copyright notice.

  • The software is provided by the authors "as is" with no warranties.

  • Users are not granted patent rights by contributors, but contributors cannot revoke patent grants for previous contributions.

  • The license does not require derived works to adopt the Apache license. Though this is encouraged for consistency.

Upload new version to PyPi

If you never ran setuptools, twine, etc, on your machine do this:

python3 -m pip install --user --upgrade setuptools wheel twine build

To make a build, first increment the version in pyproject.toml:

[project]
name = "pyscript-ltk"
version = "0.1.22"

Then build the package into a source distribution and a Python wheel:

python3 -m build

Then verify whether the build works for pypi:

twine check dist/*

Then upload to the pypi test environment:

twine upload --repository pypitest dist/*

Finally, if the pypi test upload appears to work fine, run:

twine upload dist/*

Or, as a one-liner:

rm dist/* && python3 -m build && twine upload dist/*

If you get an error that a certain version already exists, check the contents of the dist folder.

Metadata

Release files for pyscript-ltk 0.2.30

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyscript-ltk 0.2.30
File Size Uploaded Provenance Actions
pyscript_ltk-0.2.30.tar.gz 41.0 kB Without provenance Details for pyscript_ltk-0.2.30.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyscript-ltk 0.2.30
File Interpreter ABI Platform Actions
pyscript_ltk-0.2.30-py3-none-any.whl Python 3 none any Details for pyscript_ltk-0.2.30-py3-none-any.whl

Total release size: 79.9 kB

Release files / pyscript_ltk-0.2.30.tar.gz

Download URL pyscript_ltk-0.2.30.tar.gz
Size 41.0 kB
Tags Source
SHA-256 checksum
How to use checksums
55169d7f129bef4c5976d47ae2f94edd92040a10d216ca1abb8ed56ff8960d87
BLAKE2b-256 checksum
How to use checksums
1cd68e037744f7dc25645ecd4a847ed3151d12e4d379df048981a0e83c44af60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / pyscript_ltk-0.2.30-py3-none-any.whl

Download URL pyscript_ltk-0.2.30-py3-none-any.whl
Size 38.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2a2a45477cfb2217b6cb35447e0d3120c20296e1b8a0d5d1021e5e96f6df9e37
BLAKE2b-256 checksum
How to use checksums
949576c97bc6164f39dc01148d27c63202b6c513563e17ed2543f331c70eedbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for pyscript-ltk 0.2.30

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.30 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page