storage_documentation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from collections import Counter
  2. from datetime import datetime
  3. from nicegui import ui
  4. from . import doc
  5. counter = Counter() # type: ignore
  6. start = datetime.now().strftime(r'%H:%M, %d %B %Y')
  7. doc.title('Storage')
  8. @doc.demo('Storage', '''
  9. NiceGUI offers a straightforward method for data persistence within your application.
  10. It features three built-in storage types:
  11. - `app.storage.user`:
  12. Stored server-side, each dictionary is associated with a unique identifier held in a browser session cookie.
  13. Unique to each user, this storage is accessible across all their browser tabs.
  14. `app.storage.browser['id']` is used to identify the user.
  15. - `app.storage.general`:
  16. Also stored server-side, this dictionary provides a shared storage space accessible to all users.
  17. - `app.storage.browser`:
  18. Unlike the previous types, this dictionary is stored directly as the browser session cookie, shared among all browser tabs for the same user.
  19. However, `app.storage.user` is generally preferred due to its advantages in reducing data payload, enhancing security, and offering larger storage capacity.
  20. By default, NiceGUI holds a unique identifier for the browser session in `app.storage.browser['id']`.
  21. The user storage and browser storage are only available within `page builder functions </documentation/page>`_
  22. because they are accessing the underlying `Request` object from FastAPI.
  23. Additionally these two types require the `storage_secret` parameter in`ui.run()` to encrypt the browser session cookie.
  24. ''')
  25. def storage_demo():
  26. from nicegui import app
  27. # @ui.page('/')
  28. # def index():
  29. # app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  30. # with ui.row():
  31. # ui.label('your own page visits:')
  32. # ui.label().bind_text_from(app.storage.user, 'count')
  33. #
  34. # ui.run(storage_secret='private key to secure the browser session cookie')
  35. # END OF DEMO
  36. app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  37. with ui.row():
  38. ui.label('your own page visits:')
  39. ui.label().bind_text_from(app.storage.user, 'count')
  40. @doc.demo('Counting page visits', '''
  41. Here we are using the automatically available browser-stored session ID to count the number of unique page visits.
  42. ''')
  43. def page_visits():
  44. from collections import Counter
  45. from datetime import datetime
  46. from nicegui import app
  47. # counter = Counter()
  48. # start = datetime.now().strftime('%H:%M, %d %B %Y')
  49. #
  50. # @ui.page('/')
  51. # def index():
  52. # counter[app.storage.browser['id']] += 1
  53. # ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
  54. #
  55. # ui.run(storage_secret='private key to secure the browser session cookie')
  56. # END OF DEMO
  57. counter[app.storage.browser['id']] += 1
  58. ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
  59. @doc.demo('Storing UI state', '''
  60. Storage can also be used in combination with [`bindings`](/documentation/bindings).
  61. Here we are storing the value of a textarea between visits.
  62. The note is also shared between all tabs of the same user.
  63. ''')
  64. def ui_state():
  65. from nicegui import app
  66. # @ui.page('/')
  67. # def index():
  68. # ui.textarea('This note is kept between visits') \
  69. # .classes('w-full').bind_value(app.storage.user, 'note')
  70. # END OF DEMO
  71. ui.textarea('This note is kept between visits').classes('w-full').bind_value(app.storage.user, 'note')