teleport_documentation.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.teleport)
  4. def main_demo() -> None:
  5. markdown = ui.markdown('Enter your **name**!')
  6. def inject_input():
  7. with ui.teleport(f'#c{markdown.id} strong'):
  8. ui.input('name').classes('inline-flex').props('dense outlined')
  9. ui.button('inject input', on_click=inject_input)
  10. @doc.demo('Radio element with arbitrary content', '''
  11. With the right CSS selector, you can place any content inside a standard radio element.
  12. ''')
  13. def arbitrary_content():
  14. options = ['Star', 'Thump Up', 'Heart']
  15. radio = ui.radio({x: '' for x in options}, value='Star').props('inline')
  16. with ui.teleport(f'#c{radio.id} > div:nth-child(1) .q-radio__label'):
  17. ui.icon('star', size='md')
  18. with ui.teleport(f'#c{radio.id} > div:nth-child(2) .q-radio__label'):
  19. ui.icon('thumb_up', size='md')
  20. with ui.teleport(f'#c{radio.id} > div:nth-child(3) .q-radio__label'):
  21. ui.icon('favorite', size='md')
  22. ui.label().bind_text_from(radio, 'value')
  23. @doc.demo('Injecting a graph into a table cell', '''
  24. This demo shows how to inject ECharts graphs into table cells.
  25. ''')
  26. def graph_in_table():
  27. columns = [
  28. {'name': 'name', 'label': 'Product', 'field': 'name', 'align': 'center'},
  29. {'name': 'sales', 'label': 'Sales', 'field': 'sales', 'align': 'center'},
  30. ]
  31. rows = [
  32. {'name': 'A', 'data': [10, 8, 2, 4]},
  33. {'name': 'B', 'data': [3, 5, 7, 8]},
  34. {'name': 'C', 'data': [2, 1, 3, 7]},
  35. ]
  36. table = ui.table(columns=columns, rows=rows, row_key='name').classes('w-72')
  37. for r, row in enumerate(rows):
  38. with ui.teleport(f'#c{table.id} tr:nth-child({r+1}) td:nth-child(2)'):
  39. ui.echart({
  40. 'xAxis': {'type': 'category', 'show': False},
  41. 'yAxis': {'type': 'value', 'show': False},
  42. 'series': [{'type': 'line', 'data': row['data']}],
  43. }).classes('w-44 h-20')
  44. doc.reference(ui.teleport)