json_editor_documentation.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. Note that requesting data from the client is only supported for page functions, not for the shared auto-index page.
  27. ''')
  28. def methods_demo() -> None:
  29. # @ui.page('/')
  30. def page():
  31. json = {
  32. 'Name': 'Alice',
  33. 'Age': 42,
  34. 'Address': {
  35. 'Street': 'Main Street',
  36. 'City': 'Wonderland',
  37. },
  38. }
  39. editor = ui.json_editor({'content': {'json': json}})
  40. ui.button('Expand', on_click=lambda: editor.run_editor_method(':expand', 'path => true'))
  41. ui.button('Collapse', on_click=lambda: editor.run_editor_method(':expand', 'path => false'))
  42. ui.button('Readonly', on_click=lambda: editor.run_editor_method('updateProps', {'readOnly': True}))
  43. async def get_data() -> None:
  44. data = await editor.run_editor_method('get')
  45. ui.notify(data)
  46. ui.button('Get Data', on_click=get_data)
  47. page() # HIDE
  48. doc.reference(ui.json_editor)