Skip to main content

Utility decorators and helpers for Taipy GUI applications

Project description

taipy-utils

Code Style: Black Python codecov Tests

Utility decorators for cleaner Taipy GUI applications.

Installation

pip install taipy-utils
# or
uv add taipy-utils

The Initial Problem

Taipy callbacks often become messy when mixing framework code with business logic. The following code comes from a personal project that uses Taipy, before adding the decorator:

def make_qr_code(state):
    with state as s:
        message = s.qr_code_input
        if len(message) > 1500:
            notify(s, "e", "Text too long")  # Framework-specific error handling
            return
        # ... 20 more lines of business logic mixed with state management

Issues:

  • Long, hard-to-read functions
  • Business logic tightly coupled to Taipy
  • Can't reuse logic outside Taipy
  • Difficult to test

The Solution

Separate concerns with decorators:

from taipy_utils import taipy_callback

def generate_qr_code(message: str, add_logo: bool, ...) -> str:
    if len(message) > 1500:
        raise ValueError("Text too long")  # Standard Python error handling
    # ... Rest of code
    return file_path

@taipy_callback
def make_qr_code(state):
    state.image_path = generate_qr_code(
        message=state.qr_code_input,
        add_logo=state.add_logo,
        ...
    )

Benefits:

  • Separation of concerns: The UI stuff in one side, the business logic in another.
  • Pythonic error handling (standard exceptions using ValueError)
  • Reusable logic across frameworks
  • Easy to test
  • Automatic user notifications if an function raises an exception

Why This Package?

After creating @taipy_callback, I built @hold_control_during_execution for convenience. With several Taipy projects, making a package made sense:

  1. Version control separate from individual projects
  2. Share code across projects without copy-paste (DRY principle)
  3. Might help others in the Taipy community

If you find it useful, great! If not, it still keeps my projects cleaner. After I created the @taipy_callback decorator, I created @hold_control_during_execution, which is a convenience decorator. Since I have several Taipy projects, I thought that creating a package would be clean thing to do, this is why:

1- It is a way to version this code separately from other projects. Creating a dedicated package seems like the ultimate way to separate concerns. 2- By creating a package, I can easily share the code in different projects without copy-pasting a file. Since this code may live in different projects, a package is a way to keep the code DRY.

And well, this code may be useful to others, so I share it as a package.

Features

@taipy_callback

Automatically translates Python exceptions into Taipy notifications:

from taipy_utils import taipy_callback

@taipy_callback
def on_submit(state):
    if state.age < 18:
        raise ValueError("Must be 18 or older")  # Shows warning notification
    process_data(state.data)  # Any other exception shows error notification
  • ValueError → Warning notification (doesn't stop execution)
  • Other exceptions → Error notification (re-raised for debugging)

@hold_control_during_execution

This decorator triggers the hold_control() function before a callback execution, and the resume_control() at the end.

from taipy_utils import hold_control_during_execution

@hold_control_during_execution("Loading data...")
def on_load_data(state):
    # calls hold_control()
    data = fetch_from_api()
    state.data = data
    # calls resume_control()

Combine Decorators

You can combine decorators if you wish:

@taipy_callback
@hold_control_during_execution("Processing...")
def on_process(state):
    if not state.file:
        raise ValueError("Please select a file")
    result = process_file(state.file)
    state.result = result

License

MIT.

Contributing

Issues welcome at github.com/enarroied/taipy_utils.

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

taipy_utils-0.1.0.tar.gz (129.6 kB view details)

Uploaded Source

Built Distribution

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

taipy_utils-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file taipy_utils-0.1.0.tar.gz.

File metadata

  • Download URL: taipy_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 129.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.13

File hashes

Hashes for taipy_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b88c7eee98a65e55b0b7af66ad71d35c0446da964c73ff20ef8543d4a12a5980
MD5 9d02192c99576e1f4726175904104085
BLAKE2b-256 b0bf8db411f1337c2368286a3306b4c278b234a9c0835d3ceb8335d85ac89610

See more details on using hashes here.

File details

Details for the file taipy_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for taipy_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b31e523fb373ee42944bd0a4464a9338d6e009ea6257ce4347f4939cf4e90e95
MD5 aa3738bc9e4162f1afdf9e4a92335c73
BLAKE2b-256 9824e10ef65d77e4ab72de75451910afbe63d7bb396178508c74592d323714cc

See more details on using hashes here.

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