styling_appearance.py 6.4 KB

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