1
0

log.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import asyncio
  2. import urllib
  3. from justpy.htmlcomponents import WebPage
  4. from .custom_view import CustomView
  5. from .element import Element
  6. class LogView(CustomView):
  7. def __init__(self, max_lines: int):
  8. super().__init__('log', __file__, max_lines=max_lines)
  9. class Log(Element):
  10. def __init__(self, max_lines: int = None):
  11. """Log view
  12. Create a log view that allows to add new lines without re-transmitting the whole history to the client.
  13. :param max_lines: maximum number of lines before dropping oldest ones (default: None)
  14. """
  15. super().__init__(LogView(max_lines=max_lines))
  16. self.classes('border whitespace-pre font-mono').style('opacity: 1 !important; cursor: text !important')
  17. async def push_async(self, line: str):
  18. await asyncio.gather(*[
  19. self.view.run_method(f'push("{urllib.parse.quote(line)}")', socket)
  20. for socket in WebPage.sockets[Element.wp_stack[-1].page_id].values()
  21. ])
  22. def push(self, line: str):
  23. asyncio.get_event_loop().create_task(self.push_async(line))