mermaid_documentation.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.mermaid)
  4. def main_demo() -> None:
  5. ui.mermaid('''
  6. graph LR;
  7. A --> B;
  8. A --> C;
  9. ''')
  10. # END OF DEMO
  11. list(ui.context.client.elements.values())[-1].props['config'] = {'securityLevel': 'loose'} # HACK: for click_demo
  12. @doc.demo('Handle click events', '''
  13. You can register to click events by adding a `click` directive to a node and emitting a custom event.
  14. Make sure to set the `securityLevel` to `loose` in the `config` parameter to allow JavaScript execution.
  15. ''')
  16. def click_demo() -> None:
  17. ui.mermaid('''
  18. graph LR;
  19. A((Click me!));
  20. click A call emitEvent("mermaid_click", "You clicked me!")
  21. ''', config={'securityLevel': 'loose'})
  22. ui.on('mermaid_click', lambda e: ui.notify(e.args))
  23. @doc.demo('Handle errors', '''
  24. You can handle errors by listening to the `error` event.
  25. The event `args` contain the properties `hash`, `message`, `str` and an `error` object with additional information.
  26. ''')
  27. def error_demo() -> None:
  28. ui.mermaid('''
  29. graph LR;
  30. A --> B;
  31. A -> C;
  32. ''').on('error', lambda e: print(e.args['message']))
  33. # END OF DEMO
  34. list(ui.context.client.elements.values())[-1].props['config'] = {'securityLevel': 'loose'} # HACK: for click_demo
  35. doc.reference(ui.mermaid)