clipboard_documentation.py 1.1 KB

1234567891011121314151617181920212223242526272829
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo('Read and write to the clipboard', '''
  4. The following demo shows how to use `ui.clipboard.read()` and `ui.clipboard.write()` to interact with the clipboard.
  5. Note that your browser may ask for permission to access the clipboard or may not support this feature at all.
  6. ''')
  7. def main_demo() -> None:
  8. ui.button('Write', on_click=lambda: ui.clipboard.write('Hi!'))
  9. async def read() -> None:
  10. ui.notify(await ui.clipboard.read())
  11. ui.button('Read', on_click=read)
  12. @doc.demo('Client-side clipboard', '''
  13. In order to avoid the round-trip to the server, you can also use the client-side clipboard API directly.
  14. This might be supported by more browsers because the clipboard access is directly triggered by a user action.
  15. ''')
  16. def client_side_clipboard() -> None:
  17. ui.button('Write').on('click', js_handler='''
  18. () => navigator.clipboard.writeText("Ho!")
  19. ''')
  20. ui.button('Read').on('click', js_handler='''
  21. async () => emitEvent("clipboard", await navigator.clipboard.readText())
  22. ''')
  23. ui.on('clipboard', lambda e: ui.notify(e.args))