demo.py 4.2 KB

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