page.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import justpy as jp
  2. from typing import Optional
  3. from pygments.formatters import HtmlFormatter
  4. from ..globals import config, page_stack, view_stack
  5. class Page(jp.QuasarPage):
  6. def __init__(self, route: str, title: Optional[str] = None, favicon: Optional[str] = None,
  7. classes: str = 'q-ma-md column items-start',
  8. css: str = HtmlFormatter().get_style_defs('.codehilite')):
  9. """Page
  10. Creates a new page at the given path.
  11. :param route: the route of the new page. All paths must start with '/', otherwise an error occurs.
  12. """
  13. super().__init__()
  14. self.delete_flag = False
  15. self.title = title or config.title
  16. self.favicon = favicon or config.favicon
  17. self.tailwind = True # use Tailwind classes instead of Quasars
  18. self.css = css
  19. self.head_html += '''
  20. <script>
  21. confirm = () => { setTimeout(location.reload.bind(location), 100); return false; };
  22. </script>
  23. ''' # avoid confirmation dialog for reload
  24. self.view = jp.Div(a=self, classes=classes, style='row-gap: 1em')
  25. self.view.add_page(self)
  26. jp.Route(route, lambda: self)
  27. def __enter__(self):
  28. page_stack.append(self)
  29. view_stack.append(self.view)
  30. return self
  31. def __exit__(self, *_):
  32. page_stack.pop()
  33. view_stack.pop()