styling_appearance.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from nicegui import events, ui
  2. from ..demo import browser_window, python_window
  3. from ..tools import load_demo, subheading, text_demo
  4. name = 'styling_appearance'
  5. title = 'Styling & Appearance'
  6. description = '''
  7. NiceGUI allows to customize the appearance of UI elements in various ways, including CSS, Tailwind CSS and Quasar properties.
  8. '''
  9. def content() -> None:
  10. @text_demo('Styling', '''
  11. NiceGUI uses the [Quasar Framework](https://quasar.dev/) version 1.0 and hence has its full design power.
  12. Each NiceGUI element provides a `props` method whose content is passed [to the Quasar component](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components):
  13. Have a look at [the Quasar documentation](https://quasar.dev/vue-components/button#design) for all styling props.
  14. Props with a leading `:` can contain JavaScript expressions that are evaluated on the client.
  15. You can also apply [Tailwind CSS](https://tailwindcss.com/) utility classes with the `classes` method.
  16. If you really need to apply CSS, you can use the `style` method. Here the delimiter is `;` instead of a blank space.
  17. All three functions also provide `remove` and `replace` parameters in case the predefined look is not wanted in a particular styling.
  18. ''')
  19. def design_demo():
  20. ui.radio(['x', 'y', 'z'], value='x').props('inline color=green')
  21. ui.button(icon='touch_app').props('outline round').classes('shadow-lg')
  22. ui.label('Stylish!').style('color: #6E93D6; font-size: 200%; font-weight: 300')
  23. subheading('Try styling NiceGUI elements!')
  24. ui.markdown('''
  25. Try out how
  26. [Tailwind CSS classes](https://tailwindcss.com/),
  27. [Quasar props](https://justpy.io/quasar_tutorial/introduction/#props-of-quasar-components),
  28. and CSS styles affect NiceGUI elements.
  29. ''').classes('bold-links arrow-links mb-[-1rem]')
  30. with ui.row():
  31. ui.label('Select an element from those available and start styling it!').classes('mx-auto my-auto')
  32. select_element = ui.select({
  33. ui.label: 'ui.label',
  34. ui.checkbox: 'ui.checkbox',
  35. ui.switch: 'ui.switch',
  36. ui.input: 'ui.input',
  37. ui.textarea: 'ui.textarea',
  38. ui.button: 'ui.button',
  39. }, value=ui.button, on_change=lambda: live_demo_ui.refresh()).props('dense')
  40. @ui.refreshable
  41. def live_demo_ui():
  42. with ui.column().classes('w-full items-stretch gap-8 no-wrap min-[1500px]:flex-row'):
  43. with python_window(classes='w-full max-w-[44rem]'):
  44. with ui.column().classes('w-full gap-4'):
  45. ui.markdown(f'''
  46. ```py
  47. from nicegui import ui
  48. element = {select_element.options[select_element.value]}('element')
  49. ```
  50. ''').classes('mb-[-0.25em]')
  51. with ui.row().classes('items-center gap-0 w-full px-2'):
  52. def handle_classes(e: events.ValueChangeEventArguments):
  53. try:
  54. element.classes(replace=e.value)
  55. except ValueError:
  56. pass
  57. ui.markdown("`element.classes('`")
  58. ui.input(on_change=handle_classes).classes('mt-[-0.5em] text-mono grow').props('dense')
  59. ui.markdown("`')`")
  60. with ui.row().classes('items-center gap-0 w-full px-2'):
  61. def handle_props(e: events.ValueChangeEventArguments):
  62. element._props = {'label': 'Button', 'color': 'primary'}
  63. try:
  64. element.props(e.value)
  65. except ValueError:
  66. pass
  67. element.update()
  68. ui.markdown("`element.props('`")
  69. ui.input(on_change=handle_props).classes('mt-[-0.5em] text-mono grow').props('dense')
  70. ui.markdown("`')`")
  71. with ui.row().classes('items-center gap-0 w-full px-2'):
  72. def handle_style(e: events.ValueChangeEventArguments):
  73. try:
  74. element.style(replace=e.value)
  75. except ValueError:
  76. pass
  77. ui.markdown("`element.style('`")
  78. ui.input(on_change=handle_style).classes('mt-[-0.5em] text-mono grow').props('dense')
  79. ui.markdown("`')`")
  80. ui.markdown('''
  81. ```py
  82. ui.run()
  83. ```
  84. ''')
  85. with browser_window(classes='w-full max-w-[44rem] min-[1500px]:max-w-[20rem] min-h-[10rem] browser-window'):
  86. element: ui.element = select_element.value("element")
  87. live_demo_ui()
  88. @text_demo('Tailwind CSS', '''
  89. [Tailwind CSS](https://tailwindcss.com/) is a CSS framework for rapidly building custom user interfaces.
  90. NiceGUI provides a fluent, auto-complete friendly interface for adding Tailwind classes to UI elements.
  91. You can discover available classes by navigating the methods of the `tailwind` property.
  92. The builder pattern allows you to chain multiple classes together (as shown with "Label A").
  93. You can also call the `tailwind` property with a list of classes (as shown with "Label B").
  94. Although this is very similar to using the `classes` method, it is more convenient for Tailwind classes due to auto-completion.
  95. Last but not least, you can also predefine a style and apply it to multiple elements (labels C and D).
  96. Note that sometimes Tailwind is overruled by Quasar styles, e.g. when using `ui.button('Button').tailwind('bg-red-500')`.
  97. This is a known limitation and not fully in our control.
  98. But we try to provide solutions like the `color` parameter: `ui.button('Button', color='red-500')`.
  99. ''')
  100. def tailwind_demo():
  101. from nicegui import Tailwind
  102. ui.label('Label A').tailwind.font_weight('extrabold').text_color('blue-600').background_color('orange-200')
  103. ui.label('Label B').tailwind('drop-shadow', 'font-bold', 'text-green-600')
  104. red_style = Tailwind().text_color('red-600').font_weight('bold')
  105. label_c = ui.label('Label C')
  106. red_style.apply(label_c)
  107. ui.label('Label D').tailwind(red_style)
  108. load_demo(ui.query)
  109. load_demo(ui.colors)
  110. load_demo(ui.dark_mode)