Skip to content

Fix: Compatibility with modern NiceGUI and improved theme synchronization#52

Merged
CrystalWindSnake merged 3 commits into
CrystalWindSnake:mainfrom
AlePiccin:main
Mar 9, 2026
Merged

Fix: Compatibility with modern NiceGUI and improved theme synchronization#52
CrystalWindSnake merged 3 commits into
CrystalWindSnake:mainfrom
AlePiccin:main

Conversation

@AlePiccin
Copy link
Copy Markdown
Contributor

🚀 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.

Copilot AI and others added 3 commits March 6, 2026 17:52
… missing slot context

Co-authored-by: AlePiccin <149101764+AlePiccin@users.noreply.github.com>
…ssues

Fix use_theme compatibility with modern NiceGUI
@CrystalWindSnake
Copy link
Copy Markdown
Owner

@AlePiccin Thanks for your PR!

@CrystalWindSnake CrystalWindSnake merged commit d698cfe into CrystalWindSnake:main Mar 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants