example.py 6.2 KB

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