example.py 5.5 KB

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