1
0

windows.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from typing import Callable, Literal, Optional, Union
  2. from nicegui import ui
  3. WindowType = Literal['python', 'bash', 'browser']
  4. WINDOW_BG_COLORS = {
  5. 'python': ('#eef5fb', '#2b323b'),
  6. 'bash': ('#e8e8e8', '#2b323b'),
  7. 'browser': ('#ffffff', '#181c21'),
  8. }
  9. def _dots() -> None:
  10. with ui.row().classes('gap-1 relative left-[1px] top-[1px]'):
  11. ui.icon('circle').classes('text-[13px] text-red-400')
  12. ui.icon('circle').classes('text-[13px] text-yellow-400')
  13. ui.icon('circle').classes('text-[13px] text-green-400')
  14. def _window(type_: WindowType, *, title: str = '', tab: Union[str, Callable] = '', classes: str = '') -> ui.column:
  15. bar_color = ('#00000010', '#ffffff10')
  16. color = WINDOW_BG_COLORS[type_]
  17. with ui.card() \
  18. .classes(f'no-wrap bg-[{color[0]}] dark:bg-[{color[1]}] rounded-xl overflow-hidden p-0 gap-0 {classes}') \
  19. .style('box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1)'):
  20. with ui.row().classes(f'w-full h-8 p-2 bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}]'):
  21. _dots()
  22. if title:
  23. ui.label(title) \
  24. .classes('text-sm text-gray-600 dark:text-gray-400 absolute left-1/2 top-[6px]') \
  25. .style('transform: translateX(-50%)')
  26. if tab:
  27. with ui.row().classes('gap-0'):
  28. with ui.label().classes(f'w-2 h-[24px] bg-[{color[0]}] dark:bg-[{color[1]}]'):
  29. ui.label().classes(
  30. f'w-full h-full bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}] rounded-br-[6px]')
  31. 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'):
  32. if callable(tab):
  33. tab()
  34. else:
  35. ui.label(tab)
  36. with ui.label().classes(f'w-2 h-[24px] bg-[{color[0]}] dark:bg-[{color[1]}]'):
  37. ui.label().classes(
  38. f'w-full h-full bg-[{bar_color[0]}] dark:bg-[{bar_color[1]}] rounded-bl-[6px]')
  39. return ui.column().classes('w-full h-full overflow-auto')
  40. def python_window(title: Optional[str] = None, *, classes: str = '') -> ui.column:
  41. """Create a window for Python code."""
  42. return _window('python', title=title or 'main.py', classes=classes).classes('px-4 py-2 python-window')
  43. def bash_window(*, classes: str = '') -> ui.column:
  44. """Create a window for Bash code."""
  45. return _window('bash', title='bash', classes=classes).classes('px-4 py-2 bash-window')
  46. def browser_window(title: Optional[Union[str, Callable]] = None, *, classes: str = '') -> ui.column:
  47. """Create a browser window."""
  48. return _window('browser', tab=title or 'NiceGUI', classes=classes).classes('p-4 browser-window')