example.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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]) -> None:
  11. self.content = content
  12. self.markdown_classes = f'w-full max-w-screen-lg flex-none'
  13. self.rendering_classes = f'w-80 text-lg'
  14. self.source_classes = f'w-[43rem] overflow-auto'
  15. def __call__(self, f: Callable) -> Callable:
  16. with ui.row().classes('q-mb-xl'):
  17. if isinstance(self.content, str):
  18. _add_html_anchor(ui.markdown(self.content).classes(self.markdown_classes))
  19. else:
  20. doc = self.content.__doc__ or self.content.__init__.__doc__
  21. html: str = docutils.core.publish_parts(doc, writer_name='html5_polyglot')['html_body']
  22. html = html.replace('<p>', '<h4>', 1)
  23. html = html.replace('</p>', '</h4>', 1)
  24. html = html.replace('param ', '')
  25. html = apply_tailwind(html)
  26. _add_html_anchor(ui.html(html).classes(self.markdown_classes))
  27. with ui.row().classes('items-stretch max-w-screen-lg'):
  28. code = inspect.getsource(f).splitlines()
  29. while not code[0].startswith(' ' * 12):
  30. del code[0]
  31. code = [l[12:] for l in code]
  32. while code[0].startswith('global '):
  33. del code[0]
  34. code.insert(0, '```python')
  35. code.insert(1, 'from nicegui import ui')
  36. if code[2].split()[0] not in ['from', 'import']:
  37. code.insert(2, '')
  38. for l, line in enumerate(code):
  39. if line.startswith('# ui.'):
  40. code[l] = line[2:]
  41. if line.startswith('# ui.run('):
  42. break
  43. else:
  44. code.append('')
  45. code.append('ui.run()')
  46. code.append('```')
  47. code = '\n'.join(code)
  48. with python_window().classes(self.source_classes):
  49. ui.markdown(code)
  50. with browser_window().classes(self.rendering_classes):
  51. f()
  52. return f
  53. def _add_html_anchor(element: ui.html) -> None:
  54. html = element.content
  55. match = REGEX_H4.search(html)
  56. if not match:
  57. return
  58. headline = match.groups()[0].strip()
  59. headline_id = SPECIAL_CHARACTERS.sub('_', headline).lower()
  60. if not headline_id:
  61. return
  62. icon = '<span class="material-icons">link</span>'
  63. anchor = f'<a href="reference#{headline_id}" class="text-gray-300 hover:text-black">{icon}</a>'
  64. html = html.replace('<h4', f'<h4 id="{headline_id}"', 1)
  65. html = html.replace('</h4>', f' {anchor}</h4>', 1)
  66. element.content = html
  67. def _add_dots() -> None:
  68. with ui.row().classes('gap-1').style('transform: translate(-6px, -6px)'):
  69. ui.icon('circle').style('font-size: 75%').classes('text-red-400')
  70. ui.icon('circle').style('font-size: 75%').classes('text-yellow-400')
  71. ui.icon('circle').style('font-size: 75%').classes('text-green-400')
  72. def window(color: str) -> ui.card:
  73. with ui.card().style(f'box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); background: {color}') as card:
  74. _add_dots()
  75. return card
  76. def python_window() -> ui.card:
  77. return window('#eff5ff')
  78. def browser_window() -> ui.card:
  79. with ui.card().classes('h-fill').style(f'box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); background: white') as card:
  80. with ui.row():
  81. ui.icon('language').classes('text-blue-400').style('font-size: 90%; margin: -4px 0px 0px -4px')
  82. ui.label('localhost:8080').classes('text-blue-200').style('font-size: 60%; margin: -3px 0px 0px -12px')
  83. return card
  84. def bash_window() -> ui.card:
  85. return window('#e8e8e8')
  86. def css_for_examples() -> str:
  87. return '''
  88. dl {
  89. display: grid;
  90. grid-template-columns: max-content auto;
  91. }
  92. dt {
  93. grid-column-start: 1;
  94. margin-right: 1em;
  95. font-weight: bold;
  96. }
  97. dd {
  98. grid-column-start: 2;
  99. }
  100. '''