example.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. class example:
  10. def __init__(self, content: Union[Callable, type, str], tight: bool = False, skip: bool = True) -> None:
  11. self.content = content
  12. self.markdown_classes = f'mr-8 w-full flex-none lg:w-{48 if tight else 80} xl:w-80'
  13. self.rendering_classes = f'w-{48 if tight else 64} flex-none lg:mt-12'
  14. self.source_classes = f'w-80 flex-grow overflow-auto lg:mt-12'
  15. self.skip = skip
  16. def __call__(self, f: Callable) -> Callable:
  17. if self.skip:
  18. return
  19. with ui.row().classes('flex w-full'):
  20. if isinstance(self.content, str):
  21. self._add_html_anchor(ui.markdown(self.content).classes(self.markdown_classes))
  22. else:
  23. doc = self.content.__doc__ or self.content.__init__.__doc__
  24. html: str = docutils.core.publish_parts(doc, writer_name='html')['html_body']
  25. html = html.replace('<p>', '<h4>', 1)
  26. html = html.replace('</p>', '</h4>', 1)
  27. html = apply_tailwind(html)
  28. self._add_html_anchor(ui.html(html).classes(self.markdown_classes))
  29. with ui.card() \
  30. .classes(self.rendering_classes) \
  31. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  32. self._add_dots()
  33. f()
  34. code = inspect.getsource(f).splitlines()
  35. while not code[0].startswith(' ' * 8):
  36. del code[0]
  37. code = [l[8:] for l in code]
  38. while code[0].startswith('global '):
  39. del code[0]
  40. code.insert(0, '```python')
  41. code.insert(1, 'from nicegui import ui')
  42. if code[2].split()[0] not in ['from', 'import']:
  43. code.insert(2, '')
  44. for l, line in enumerate(code):
  45. if line.startswith('# ui.'):
  46. code[l] = line[2:]
  47. if line.startswith('# ui.run('):
  48. break
  49. else:
  50. code.append('')
  51. code.append('ui.run()')
  52. code.append('```')
  53. code = '\n'.join(code)
  54. with ui.card() \
  55. .classes(self.source_classes) \
  56. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); background: #e3eafd'):
  57. self._add_dots()
  58. ui.markdown(code)
  59. return f
  60. @staticmethod
  61. def _add_html_anchor(element: ui.html) -> None:
  62. html = element.content
  63. match = REGEX_H4.search(html)
  64. if not match:
  65. return
  66. headline = match.groups()[0].strip()
  67. headline_id = SPECIAL_CHARACTERS.sub('_', headline).lower()
  68. if not headline_id:
  69. return
  70. icon = '<span class="material-icons">link</span>'
  71. anchor = f'<a href="reference#{headline_id}" class="text-gray-300 hover:text-black">{icon}</a>'
  72. html = html.replace('<h4', f'<h4 id="{headline_id}"', 1)
  73. html = html.replace('</h4>', f' {anchor}</h4>', 1)
  74. element.content = html
  75. @staticmethod
  76. def _add_dots() -> None:
  77. with ui.row().classes('gap-1').style('transform: translate(-6px, -6px)'):
  78. ui.icon('circle').style('font-size: 75%').classes('text-red-400')
  79. ui.icon('circle').style('font-size: 75%').classes('text-yellow-400')
  80. ui.icon('circle').style('font-size: 75%').classes('text-green-400')