Fix: Compatibility with modern NiceGUI and improved theme synchronization#52
Merged
Merged
Conversation
… missing slot context Co-authored-by: AlePiccin <149101764+AlePiccin@users.noreply.github.com>
…ssues Fix use_theme compatibility with modern NiceGUI
Owner
|
@AlePiccin Thanks for your PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Overview
This PR addresses compatibility issues with recent versions of NiceGUI and enhances how theme changes are propagated to clients, especially when called from startup hooks or shared contexts.
🛠️ Key Changes
NiceGUI Modernization:
Removed reliance on is_auto_index_client, which is no longer present in recent NiceGUI versions.
Implemented a safer context check using bool(ui.context.slot_stack) to determine if the function is being called within a UI socket context or a global background task (like app.on_startup).
Robust Context Handling:
Fixed the ui.context.client error that occurred when calling use_theme during the application startup phase.
The logic now gracefully handles cases where no active slot/client exists yet.
New Feature: Instant Shared Updates:
Introduced a more dynamic shared logic. When shared=True, the component now iterates through all active Client.instances to inject the necessary JavaScript (_remove_old_theme_js) in real-time.
This ensures that a theme change triggered globally is immediately reflected for all connected users without requiring a page refresh.
📝 Technical Details
JS Injection: Extracted the CSS cleanup logic into a constant _remove_old_theme_js for better maintainability.
Thread Safety: Used list(Client.instances.values()) to prevent "dictionary changed size during iteration" errors when updating multiple clients simultaneously.
Logical Defaults:
If called outside a context: Defaults to shared=True.
If called inside a page: Defaults to shared=False (per-client).
🧪 How to Test
Call use_theme() inside an app.on_startup handler. It should no longer throw a context error.
Open multiple browser tabs. Change the theme with shared=True and verify that all tabs update the Tabulator theme instantly via JS injection.