json_editor_documentation.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. The colon ":" in front of the method name "expand" indicates that the value "path => true" is a JavaScript expression
  25. that is evaluated on the client before it is passed to the method.
  26. ''')
  27. def methods_demo() -> None:
  28. json = {
  29. 'Name': 'Alice',
  30. 'Age': 42,
  31. 'Address': {
  32. 'Street': 'Main Street',
  33. 'City': 'Wonderland',
  34. },
  35. }
  36. editor = ui.json_editor({'content': {'json': json}})
  37. ui.button('Expand', on_click=lambda: editor.run_editor_method(':expand', 'path => true'))
  38. ui.button('Collapse', on_click=lambda: editor.run_editor_method(':expand', 'path => false'))
  39. ui.button('Readonly', on_click=lambda: editor.run_editor_method('updateProps', {'readOnly': True}))
  40. async def get_data() -> None:
  41. data = await editor.run_editor_method('get')
  42. ui.notify(data)
  43. ui.button('Get Data', on_click=get_data)
  44. doc.reference(ui.json_editor)