Преглед изворни кода

fix demo about attaching `ui.log` to a logger

Falko Schindler пре 2 месеци
родитељ
комит
dd1fa04aaa
1 измењених фајлова са 10 додато и 5 уклоњено
  1. 10 5
      website/documentation/content/log_documentation.py

+ 10 - 5
website/documentation/content/log_documentation.py

@@ -13,6 +13,8 @@ def main_demo() -> None:
 
 
 @doc.demo('Attach to a logger', '''
 @doc.demo('Attach to a logger', '''
     You can attach a `ui.log` element to a Python logger object so that log messages are pushed to the log element.
     You can attach a `ui.log` element to a Python logger object so that log messages are pushed to the log element.
+    When used inside a page function, it is important to remove the handler when the client disconnects.
+    Otherwise, the handler will keep a reference to the log element and the latter will not be garbage collected.
 ''')
 ''')
 def logger_handler():
 def logger_handler():
     import logging
     import logging
@@ -34,11 +36,14 @@ def logger_handler():
             except Exception:
             except Exception:
                 self.handleError(record)
                 self.handleError(record)
 
 
-    log = ui.log(max_lines=10).classes('w-full')
-    handler = LogElementHandler(log)
-    logger.addHandler(handler)
-    ui.context.client.on_disconnect(lambda: logger.removeHandler(handler))
-    ui.button('Log time', on_click=lambda: logger.warning(datetime.now().strftime('%X.%f')[:-5]))
+    # @ui.page('/')
+    def page():
+        log = ui.log(max_lines=10).classes('w-full')
+        handler = LogElementHandler(log)
+        logger.addHandler(handler)
+        ui.context.client.on_disconnect(lambda: logger.removeHandler(handler))
+        ui.button('Log time', on_click=lambda: logger.warning(datetime.now().strftime('%X.%f')[:-5]))
+    page()  # HIDE
 
 
 
 
 doc.reference(ui.log)
 doc.reference(ui.log)