1
0

demo.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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'):
  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. ui.markdown(f'```python\n{code}\n```')
  33. with browser_window(self.browser_title,
  34. classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  35. intersection_observer(on_intersection=f)
  36. return f
  37. def _window_header(bgcolor: str) -> ui.row():
  38. return ui.row().classes(f'w-full h-8 p-2 bg-[{bgcolor}]')
  39. def _dots() -> None:
  40. with ui.row().classes('gap-1 relative left-[1px] top-[1px]'):
  41. ui.icon('circle').classes('text-[13px] text-red-400')
  42. ui.icon('circle').classes('text-[13px] text-yellow-400')
  43. ui.icon('circle').classes('text-[13px] text-green-400')
  44. def _title(title: str) -> None:
  45. ui.label(title).classes('text-sm text-gray-600 absolute left-1/2 top-[6px]').style('transform: translateX(-50%)')
  46. def _tab(name: str, color: str, bgcolor: str) -> None:
  47. with ui.row().classes('gap-0'):
  48. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  49. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-br-[6px]')
  50. ui.label(name).classes(f'text-sm text-gray-600 px-6 py-1 h-[24px] rounded-t-[6px] bg-[{color}]')
  51. with ui.label().classes(f'w-2 h-[24px] bg-[{color}]'):
  52. ui.label().classes(f'w-full h-full bg-[{bgcolor}] rounded-bl-[6px]')
  53. def window(color: str, bgcolor: str, *, title: str = '', tab: str = '', classes: str = '') -> ui.column:
  54. with ui.card().classes(f'no-wrap bg-[{color}] rounded-xl p-0 gap-0 {classes}') \
  55. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  56. with _window_header(bgcolor):
  57. _dots()
  58. if title:
  59. _title(title)
  60. if tab:
  61. _tab(tab, color, bgcolor)
  62. return ui.column().classes('w-full h-full overflow-auto')
  63. def python_window(title: Optional[str] = None, *, classes: str = '') -> ui.card:
  64. return window(PYTHON_COLOR, PYTHON_BGCOLOR, title=title or 'main.py', classes=classes).classes('p-2 python-window')
  65. def bash_window(*, classes: str = '') -> ui.card:
  66. return window(BASH_COLOR, BASH_BGCOLOR, title='bash', classes=classes).classes('p-2 bash-window')
  67. def browser_window(title: Optional[str] = None, *, classes: str = '') -> ui.card:
  68. return window(BROWSER_COLOR, BROWSER_BGCOLOR, tab=title or 'NiceGUI', classes=classes).classes('p-4 browser-window')