json_editor_documentation.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.json_editor)
  4. def main_demo() -> None:
  5. json = {
  6. 'array': [1, 2, 3],
  7. 'boolean': True,
  8. 'color': '#82b92c',
  9. None: None,
  10. 'number': 123,
  11. 'object': {
  12. 'a': 'b',
  13. 'c': 'd',
  14. },
  15. 'time': 1575599819000,
  16. 'string': 'Hello World',
  17. }
  18. ui.json_editor({'content': {'json': json}},
  19. on_select=lambda e: ui.notify(f'Select: {e}'),
  20. on_change=lambda e: ui.notify(f'Change: {e}'))
  21. @doc.demo('Run methods', '''
  22. You can run methods of the JSONEditor instance using the `run_editor_method` method.
  23. This demo shows how to expand and collapse all nodes and how to get the current data.
  24. ''')
  25. def methods_demo() -> None:
  26. json = {
  27. 'Name': 'Alice',
  28. 'Age': 42,
  29. 'Address': {
  30. 'Street': 'Main Street',
  31. 'City': 'Wonderland',
  32. },
  33. }
  34. editor = ui.json_editor({'content': {'json': json}})
  35. ui.button('Expand All', on_click=lambda: editor.run_editor_method('expand', 'path => true'))
  36. ui.button('Collapse All', on_click=lambda: editor.run_editor_method('expand', 'path => false'))
  37. async def get_data() -> None:
  38. data = await editor.run_editor_method('get')
  39. ui.notify(data)
  40. ui.button('Get Data', on_click=get_data)
  41. doc.reference(ui.json_editor)