demo.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import inspect
  2. from typing import Callable, Optional, Union
  3. import isort
  4. from nicegui import ui
  5. from .intersection_observer import IntersectionObserver as intersection_observer
  6. PYTHON_BGCOLOR = '#00000010'
  7. PYTHON_COLOR = '#eef5fb'
  8. BASH_BGCOLOR = '#00000010'
  9. BASH_COLOR = '#e8e8e8'
  10. BROWSER_BGCOLOR = '#00000010'
  11. BROWSER_COLOR = '#ffffff'
  12. def remove_prefix(text: str, prefix: str) -> str:
  13. return text[len(prefix):] if text.startswith(prefix) else text
  14. def demo(f: Callable) -> Callable:
  15. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  16. code = inspect.getsource(f).split('# END OF DEMO')[0].strip().splitlines()
  17. while not code[0].strip().startswith('def') and not code[0].strip().startswith('async def'):
  18. del code[0]
  19. del code[0]
  20. indentation = len(code[0]) - len(code[0].lstrip())
  21. code = [line[indentation:] for line in code]
  22. code = ['from nicegui import ui'] + [remove_prefix(line, '# ') for line in code]
  23. code = ['' if line == '#' else line for line in code]
  24. if not code[-1].startswith('ui.run('):
  25. code.append('')
  26. code.append('ui.run()')
  27. code = isort.code('\n'.join(code), no_sections=True, lines_after_imports=1)
  28. with python_window(classes='w-full max-w-[44rem]'):
  29. async def copy_code():
  30. await ui.run_javascript('navigator.clipboard.writeText(`' + code + '`)', respond=False)
  31. ui.notify('Copied to clipboard', type='positive', color='primary')
  32. ui.markdown(f'````python\n{code}\n````')
  33. ui.icon('content_copy', size='xs') \
  34. .classes('absolute right-2 top-10 opacity-10 hover:opacity-80 cursor-pointer') \
  35. .on('click', copy_code)
  36. with browser_window(title=getattr(f, 'tab', None),
  37. classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  38. intersection_observer(on_intersection=f)
  39. return f
  40. def _window_header(bgcolor: str) -> ui.row():
  41. return ui.row().classes(f'w-full h-8 p-2 bg-[{bgcolor}]')
  42. def _dots() -> None:
  43. with ui.row().classes('gap-1 relative left-[1px] top-[1px]'):
  44. ui.icon('circle').classes('text-[13px] text-red-400')
  45. ui.icon('circle').classes('text-[13px] text-yellow-400')
  46. ui.icon('circle').classes('text-[13px] text-green-400')
  47. def _title(title: str) -> None:
  48. ui.label(title).classes('text-sm text-gray-600 absolute left-1/2 top-[6px]').style('transform: translateX(-50%)')
  49. def _tab(content: Union[str, Callable], color: str, bgcolor: str) -> None:
  50. with ui.row().classes('gap-0'):
  51. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  52. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-br-[6px]')
  53. with ui.row().classes(f'text-sm text-gray-600 px-6 py-1 h-[24px] rounded-t-[6px] bg-[{color}] items-center gap-2'):
  54. if callable(content):
  55. content()
  56. else:
  57. ui.label(content)
  58. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  59. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-bl-[6px]')
  60. def window(color: str, bgcolor: str, *,
  61. title: str = '', tab: Union[str, Callable] = '', classes: str = '') -> ui.column:
  62. with ui.card().classes(f'no-wrap bg-[{color}] rounded-xl p-0 gap-0 {classes}') \
  63. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  64. with _window_header(bgcolor):
  65. _dots()
  66. if title:
  67. _title(title)
  68. if tab:
  69. _tab(tab, color, bgcolor)
  70. return ui.column().classes('w-full h-full overflow-auto')
  71. def python_window(title: Optional[str] = None, *, classes: str = '') -> ui.card:
  72. return window(PYTHON_COLOR, PYTHON_BGCOLOR, title=title or 'main.py', classes=classes).classes('p-2 python-window')
  73. def bash_window(*, classes: str = '') -> ui.card:
  74. return window(BASH_COLOR, BASH_BGCOLOR, title='bash', classes=classes).classes('p-2 bash-window')
  75. def browser_window(title: Optional[Union[str, Callable]] = None, *, classes: str = '') -> ui.card:
  76. return window(BROWSER_COLOR, BROWSER_BGCOLOR, tab=title or 'NiceGUI', classes=classes).classes('p-4 browser-window')