ソースを参照

raise more informative exception if slot stack is empty

Falko Schindler 1 年間 前
コミット
46769cbbaf
1 ファイル変更6 行追加1 行削除
  1. 6 1
      nicegui/context.py

+ 6 - 1
nicegui/context.py

@@ -15,7 +15,12 @@ def get_slot_stack() -> List[Slot]:
 
 
 def get_slot() -> Slot:
 def get_slot() -> Slot:
     """Return the current slot."""
     """Return the current slot."""
-    return get_slot_stack()[-1]
+    slot_stack = get_slot_stack()
+    if not slot_stack:
+        raise RuntimeError('The current slot cannot be determined because the slot stack for this task is empty.\n'
+                           'This may happen if you try to create UI from a background task.\n'
+                           'To fix this, enter the target slot explicitly using `with container_element:`.')
+    return slot_stack[-1]
 
 
 
 
 def get_client() -> Client:
 def get_client() -> Client: