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 a 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

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.1.tar.gz (129.3 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.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for taipy_utils-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ae61d9156eb2458faacc5c2584710c4403398bf1c77254673941743d41783a31
MD5 3edc388ba5e11ed14cb0b67b83f8345d
BLAKE2b-256 cc56980001316c21d256628394b3a24789f8cf0a156a61a7de31c9d36babe716

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for taipy_utils-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cedeec9e13d0be9d5b29b0083b4c937649c21d19a2cd98a2468a60c1ff2ac648
MD5 8329fae4d9bb0c2b1b0231c675dcbe10
BLAKE2b-256 cce668aa6ee38101c10ddba00260bff7f4f4da45291f54e9e8f3259a05ffc53b

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