demo.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import inspect
  2. import re
  3. from typing import Callable, Literal, Optional, Union
  4. import isort
  5. from nicegui import helpers, ui
  6. from .intersection_observer import IntersectionObserver as intersection_observer
  7. WindowType = Literal['python', 'bash', 'browser']
  8. UNCOMMENT_PATTERN = re.compile(r'^(\s*)# ?')
  9. WINDOW_BG_COLORS = {
  10. 'python': ('#eef5fb', '#2b323b'),
  11. 'bash': ('#e8e8e8', '#2b323b'),
  12. 'browser': ('#ffffff', '#181c21'),
  13. }
  14. def uncomment(text: str) -> str:
  15. """non-executed lines should be shown in the code examples"""
  16. return UNCOMMENT_PATTERN.sub(r'\1', text)
  17. def demo(f: Callable) -> Callable:
  18. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  19. code = inspect.getsource(f).split('# END OF DEMO')[0].strip().splitlines()
  20. code = [line for line in code if not line.endswith("# HIDE")]
  21. while not code[0].strip().startswith('def') and not code[0].strip().startswith('async def'):
  22. del code[0]
  23. del code[0]
  24. if code[0].strip().startswith('"""'):
  25. while code[0].strip() != '"""':
  26. del code[0]
  27. del code[0]
  28. indentation = len(code[0]) - len(code[0].lstrip())
  29. code = [line[indentation:] for line in code]
  30. code = ['from nicegui import ui'] + [uncomment(line) for line in code]
  31. code = ['' if line == '#' else line for line in code]
  32. if not code[-1].startswith('ui.run('):
  33. code.append('')
  34. code.append('ui.run()')
  35. code = isort.code('\n'.join(code), no_sections=True, lines_after_imports=1)
  36. with python_window(classes='w-full max-w-[44rem]'):
  37. def copy_code():
  38. ui.run_javascript('navigator.clipboard.writeText(`' + code + '`)')
  39. ui.notify('Copied to clipboard', type='positive', color='primary')
  40. ui.markdown(f'````python\n{code}\n````')
  41. ui.icon('content_copy', size='xs') \
  42. .classes('absolute right-2 top-10 opacity-10 hover:opacity-80 cursor-pointer') \
  43. .on('click', copy_code, [])
  44. with browser_window(title=getattr(f, 'tab', None),
  45. classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window') as window:
  46. spinner = ui.spinner(size='lg').props('thickness=2')
  47. async def handle_intersection():
  48. window.remove(spinner)
  49. if helpers.is_coroutine_function(f):
  50. await f()
  51. else:
  52. f()
  53. intersection_observer(on_intersection=handle_intersection)
  54. return f
  55. def _dots() -> None:
  56. with ui.row().classes('gap-1 relative left-[1px] top-[1px]'):
  57. ui.icon('circle').classes('text-[13px] text-red-400')
  58. ui.icon('circle').classes('text-[13px] text-yellow-400')
  59. ui.icon('circle').classes('text-[13px] text-green-400')
  60. def window(type_: WindowType, *, title: str = '', tab: Union[str, Callable] = '', classes: str = '') -> ui.column:
  61. bar_color = ('#00000010', '#ffffff10')
  62. color = WINDOW_BG_COLORS[type_]
  63. with ui.card().classes(f'no-wrap bg-[{color[0]}] dark:bg-[{color[1]}] rounded-xl p-0 gap-0 {classes}') \
  64. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  65. with ui.row().classes(f'w-full h-8 p-2 bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}]'):
  66. _dots()
  67. if title:
  68. ui.label(title) \
  69. .classes('text-sm text-gray-600 dark:text-gray-400 absolute left-1/2 top-[6px]') \
  70. .style('transform: translateX(-50%)')
  71. if tab:
  72. with ui.row().classes('gap-0'):
  73. with ui.label().classes(f'w-2 h-[24px] bg-[{color[0]}] dark:bg-[{color[1]}]'):
  74. ui.label().classes(
  75. f'w-full h-full bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}] rounded-br-[6px]')
  76. with ui.row().classes(f'text-sm text-gray-600 dark:text-gray-400 px-6 py-1 h-[24px] rounded-t-[6px] bg-[{color[0]}] dark:bg-[{color[1]}] items-center gap-2'):
  77. if callable(tab):
  78. tab()
  79. else:
  80. ui.label(tab)
  81. with ui.label().classes(f'w-2 h-[24px] bg-[{color[0]}] dark:bg-[{color[1]}]'):
  82. ui.label().classes(
  83. f'w-full h-full bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}] rounded-bl-[6px]')
  84. return ui.column().classes('w-full h-full overflow-auto')
  85. def python_window(title: Optional[str] = None, *, classes: str = '') -> ui.column:
  86. return window('python', title=title or 'main.py', classes=classes).classes('p-2 python-window')
  87. def bash_window(*, classes: str = '') -> ui.column:
  88. return window('bash', title='bash', classes=classes).classes('p-2 bash-window')
  89. def browser_window(title: Optional[Union[str, Callable]] = None, *, classes: str = '') -> ui.column:
  90. return window('browser', tab=title or 'NiceGUI', classes=classes).classes('p-4 browser-window')