1
0

json_editor_documentation.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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', on_click=lambda: editor.run_editor_method('expand', 'path => true'))
  36. ui.button('Collapse', on_click=lambda: editor.run_editor_method('expand', 'path => false'))
  37. ui.button('Readonly', on_click=lambda: editor.run_editor_method('updateProps', r'{readOnly: true}'))
  38. async def get_data() -> None:
  39. data = await editor.run_editor_method('get')
  40. ui.notify(data)
  41. ui.button('Get Data', on_click=get_data)
  42. doc.reference(ui.json_editor)