Skip to main content

Dataclass for trame UI binding

Project description

trame-dataclass

Dataclass for trame UI binding and reactivity handling.

License

This library is OpenSource and follow the Apache Software License

Installation

Install the application/library

pip install trame-dataclass

Usage example

from typing import Any

from trame.app import TrameApp
from trame.app.dataclass import ClientOnly, ServerOnly, StateDataModel, Sync, watch
from trame.ui.html import DivLayout
from trame.widgets import dataclass, html


class SimpleStructure(StateDataModel):
    name = Sync(str, "John Doe")  # server <=> client
    age = Sync(int, 1)  # server <=> client
    derived_value = Sync(int)  # server <=> client
    something = ServerOnly(Any)  # server
    local_edit = ClientOnly(int)  # server => client

    @watch("age")
    def _update_derived(self, age):
        self.derived_value = 80 - age

    @watch("local_edit")
    def _never_called(self, local_edit):
        print("local_edit changed to", local_edit)


class GettingStarted(TrameApp):
    def __init__(self, server=None):
        super().__init__(server)
        self._data = SimpleStructure(self.server)
        self._data.watch(["age"], self.print_age)
        self._build_ui()

    def print_age(self, age):
        print(f"Age changed to {age=}")

    def toggle_active_user(self):
        if self.state.active_user:
            self.state.active_user = None
        else:
            self.state.active_user = self._data._id

    def _modify_data(self):
        self._data.age += 1

    def _build_ui(self):
        with DivLayout(self.server) as self.ui:
            # Edit user on server
            html.Button("Server change", click=self._modify_data)

            # Provide data class instance to the UI as a variable
            with self._data.provide_as("user"):
                html.Button("Edit local", click="user.local_edit = Date.now()")

                html.Pre("{{ JSON.stringify(user, null, 2) }}")
                html.Hr()
                html.Div(
                    "Hello {{ user.name }} - derived value = {{ user.derived_value }}"
                )
                html.Hr()
                html.Span("Your name:")
                html.Input(type="text", v_model="user.name")
                html.Hr()
                html.Span("Your age:")
                html.Input(
                    type="range", min=0, max=120, step=1, v_model_number="user.age"
                )
            html.Hr()

            # Adjust dynamic user
            html.Button(
                "Toggle user ({{ active_user || 'None' }})",
                click=self.toggle_active_user,
            )

            # Dynamically provide a dataclass to UI
            with dataclass.Provider(
                name="dynamic_user",
                instance=("active_user", None),
            ):
                html.Pre("{{ JSON.stringify(dynamic_user, null, 2) }}")


def main():
    app = GettingStarted()
    app.server.start()


if __name__ == "__main__":
    main()

Development setup

We recommend using uv for setting up and managing a virtual environment for your development.

# Create venv and install all dependencies
uv sync --all-extras --dev

# Activate environment
source .venv/bin/activate

# Install commit analysis
pre-commit install
pre-commit install --hook-type commit-msg

# Allow live code edit
uv pip install -e .

Build and install the Vue components

cd vue-components
npm i
npm run build
cd -

For running tests and checks, you can run nox.

# run all
nox

# lint
nox -s lint

# tests
nox -s tests

Professional Support

  • Training: Learn how to confidently use trame from the expert developers at Kitware.

  • Support: Our experts can assist your team as you build your web application and establish in-house expertise.

  • Custom Development: Leverage Kitware’s 25+ years of experience to quickly build your web application.

Commit message convention

Semantic release rely on conventional commits to generate new releases and changelog.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trame_dataclass-2.0.6.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trame_dataclass-2.0.6-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file trame_dataclass-2.0.6.tar.gz.

File metadata

  • Download URL: trame_dataclass-2.0.6.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trame_dataclass-2.0.6.tar.gz
Algorithm Hash digest
SHA256 acf49bbeec5c05b9983a4718ae37aef38998aeb92cc7dbba0324f7ce98837ec5
MD5 3233475d50715b7fbc31c7f751c78be3
BLAKE2b-256 eef8456ff49c190eb83c9b16bdae85fffe1cc39fa3c56d986c033050c940b811

See more details on using hashes here.

Provenance

The following attestation bundles were made for trame_dataclass-2.0.6.tar.gz:

Publisher: test_and_release.yml on Kitware/trame-dataclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trame_dataclass-2.0.6-py3-none-any.whl.

File metadata

  • Download URL: trame_dataclass-2.0.6-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for trame_dataclass-2.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 6aa8496116d9bc4f63e58f14000c23ea35882f7f371e14dad1498b6b5c470ab8
MD5 400573a065ee6522190c26d83d5cad51
BLAKE2b-256 fc18bc19538971dcd19ccf1dcc21e642511392fd4dceb7d671f151ed0701b863

See more details on using hashes here.

Provenance

The following attestation bundles were made for trame_dataclass-2.0.6-py3-none-any.whl:

Publisher: test_and_release.yml on Kitware/trame-dataclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page