example.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import inspect
  2. import re
  3. from typing import Callable, Optional, Union
  4. import docutils.core
  5. import isort
  6. from nicegui import ui
  7. from nicegui.elements.markdown import apply_tailwind, prepare_content
  8. from .intersection_observer import IntersectionObserver as intersection_observer
  9. REGEX_H4 = re.compile(r'<h4.*?>(.*?)</h4>')
  10. SPECIAL_CHARACTERS = re.compile('[^(a-z)(A-Z)(0-9)-]')
  11. PYTHON_BGCOLOR = '#00000010'
  12. PYTHON_COLOR = '#eef5fb'
  13. BASH_BGCOLOR = '#00000010'
  14. BASH_COLOR = '#e8e8e8'
  15. BROWSER_BGCOLOR = '#00000010'
  16. BROWSER_COLOR = '#ffffff'
  17. def remove_prefix(text: str, prefix: str) -> str:
  18. return text[len(prefix):] if text.startswith(prefix) else text
  19. def add_html_with_anchor_link(html: str, menu: Optional[ui.drawer]) -> str:
  20. match = REGEX_H4.search(html)
  21. headline = match.groups()[0].strip()
  22. headline_id = SPECIAL_CHARACTERS.sub('_', headline).lower()
  23. icon = '<span class="material-icons">link</span>'
  24. link = f'<a href="#{headline_id}" class="hover:text-black auto-link" style="color: #ddd">{icon}</a>'
  25. target = f'<div id="{headline_id}" style="position: relative; top: -90px"></div>'
  26. html = html.replace('<h4', f'{target}<h4', 1)
  27. html = html.replace('</h4>', f' {link}</h4>', 1)
  28. ui.html(html).classes('documentation bold-links arrow-links')
  29. if menu:
  30. with menu:
  31. async def click():
  32. if await ui.run_javascript(f'!!document.querySelector("div.q-drawer__backdrop")'):
  33. menu.hide()
  34. ui.open(f'#{headline_id}')
  35. ui.link(headline, f'#{headline_id}').props('data-close-overlay').on('click', click)
  36. class example:
  37. def __init__(self,
  38. content: Union[Callable, type, str],
  39. menu: Optional[ui.drawer],
  40. browser_title: Optional[str] = None,
  41. immediate: bool = False) -> None:
  42. self.content = content
  43. self.menu = menu
  44. self.browser_title = browser_title
  45. self.immediate = immediate
  46. def __call__(self, f: Callable) -> Callable:
  47. with ui.column().classes('w-full mb-8'):
  48. if isinstance(self.content, str):
  49. html = prepare_content(self.content, 'fenced-code-blocks tables')
  50. else:
  51. doc = self.content.__doc__ or self.content.__init__.__doc__
  52. html: str = docutils.core.publish_parts(doc, writer_name='html5_polyglot')['html_body']
  53. html = html.replace('<p>', '<h4>', 1)
  54. html = html.replace('</p>', '</h4>', 1)
  55. html = html.replace('param ', '')
  56. html = apply_tailwind(html)
  57. add_html_with_anchor_link(html, self.menu)
  58. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  59. code = inspect.getsource(f).split('# END OF EXAMPLE')[0].strip().splitlines()
  60. while not code[0].startswith(' ' * 8):
  61. del code[0]
  62. code = ['from nicegui import ui'] + [remove_prefix(line[8:], '# ') for line in code]
  63. code = ['' if line == '#' else line for line in code]
  64. if not code[-1].startswith('ui.run('):
  65. code.append('')
  66. code.append('ui.run()')
  67. code = isort.code('\n'.join(code), no_sections=True, lines_after_imports=1)
  68. with python_window(classes='w-full max-w-[44rem]'):
  69. ui.markdown(f'```python\n{code}\n```')
  70. with browser_window(self.browser_title,
  71. classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  72. if self.immediate:
  73. f()
  74. else:
  75. intersection_observer(on_intersection=f)
  76. return f
  77. def _window_header(bgcolor: str) -> ui.row():
  78. return ui.row().classes(f'w-full h-8 p-2 bg-[{bgcolor}]')
  79. def _dots() -> None:
  80. with ui.row().classes('gap-1 relative left-[1px] top-[1px]'):
  81. ui.icon('circle').classes('text-[13px] text-red-400')
  82. ui.icon('circle').classes('text-[13px] text-yellow-400')
  83. ui.icon('circle').classes('text-[13px] text-green-400')
  84. def _title(title: str) -> None:
  85. ui.label(title).classes('text-sm text-gray-600 absolute left-1/2 top-[6px]').style('transform: translateX(-50%)')
  86. def _tab(name: str, color: str, bgcolor: str) -> None:
  87. with ui.row().classes('gap-0'):
  88. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  89. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-br-[6px]')
  90. ui.label(name).classes(f'text-sm text-gray-600 px-6 py-1 h-[24px] rounded-t-[6px] bg-[{color}]')
  91. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  92. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-bl-[6px]')
  93. def window(color: str, bgcolor: str, *, title: str = '', tab: str = '', classes: str = '') -> ui.column:
  94. with ui.card().classes(f'no-wrap bg-[{color}] rounded-xl p-0 gap-0 {classes}') \
  95. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  96. with _window_header(bgcolor):
  97. _dots()
  98. if title:
  99. _title(title)
  100. if tab:
  101. _tab(tab, color, bgcolor)
  102. return ui.column().classes('w-full h-full overflow-auto')
  103. def python_window(title: Optional[str] = None, *, classes: str = '') -> ui.card:
  104. return window(PYTHON_COLOR, PYTHON_BGCOLOR, title=title or 'main.py', classes=classes).classes('p-2 python-window')
  105. def bash_window(*, classes: str = '') -> ui.card:
  106. return window(BASH_COLOR, BASH_BGCOLOR, title='bash', classes=classes).classes('p-2 bash-window')
  107. def browser_window(title: Optional[str] = None, *, classes: str = '') -> ui.card:
  108. return window(BROWSER_COLOR, BROWSER_BGCOLOR, tab=title or 'NiceGUI', classes=classes).classes('p-4 browser-window')