Utility decorators and helpers for Taipy GUI applications
Project description
taipy-utils
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:
- Version control separate from individual projects
- Share code across projects without copy-paste (DRY principle)
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b88c7eee98a65e55b0b7af66ad71d35c0446da964c73ff20ef8543d4a12a5980
|
|
| MD5 |
9d02192c99576e1f4726175904104085
|
|
| BLAKE2b-256 |
b0bf8db411f1337c2368286a3306b4c278b234a9c0835d3ceb8335d85ac89610
|
File details
Details for the file taipy_utils-0.1.0-py3-none-any.whl.
File metadata
- Download URL: taipy_utils-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b31e523fb373ee42944bd0a4464a9338d6e009ea6257ce4347f4939cf4e90e95
|
|
| MD5 |
aa3738bc9e4162f1afdf9e4a92335c73
|
|
| BLAKE2b-256 |
9824e10ef65d77e4ab72de75451910afbe63d7bb396178508c74592d323714cc
|