storage_documentation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from collections import Counter
  2. from datetime import datetime
  3. from nicegui import ui
  4. from ..documentation_tools import text_demo
  5. def main_demo() -> None:
  6. """Storage
  7. NiceGUI offers a straightforward method for data persistence within your application.
  8. It features three built-in storage types:
  9. - `app.storage.user`:
  10. Stored server-side, each dictionary is associated with a unique identifier held in a browser session cookie.
  11. Unique to each user, this storage is accessible across all their browser tabs.
  12. `app.storage.browser['id']` is used to identify the user.
  13. - `app.storage.general`:
  14. Also stored server-side, this dictionary provides a shared storage space accessible to all users.
  15. - `app.storage.browser`:
  16. Unlike the previous types, this dictionary is stored directly as the browser session cookie, shared among all browser tabs for the same user.
  17. However, `app.storage.user` is generally preferred due to its advantages in reducing data payload, enhancing security, and offering larger storage capacity.
  18. By default, NiceGUI holds a unique identifier for the browser session in `app.storage.browser['id']`.
  19. The user storage and browser storage are only available within `page builder functions </documentation/page>`_
  20. because they are accessing the underlying `Request` object from FastAPI.
  21. Additionally these two types require the `storage_secret` parameter in`ui.run()` to encrypt the browser session cookie.
  22. """
  23. from nicegui import app
  24. # @ui.page('/')
  25. # def index():
  26. # app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  27. # with ui.row():
  28. # ui.label('your own page visits:')
  29. # ui.label().bind_text_from(app.storage.user, 'count')
  30. #
  31. # ui.run(storage_secret='private key to secure the browser session cookie')
  32. # END OF DEMO
  33. app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  34. with ui.row():
  35. ui.label('your own page visits:')
  36. ui.label().bind_text_from(app.storage.user, 'count')
  37. counter = Counter()
  38. start = datetime.now().strftime('%H:%M, %d %B %Y')
  39. def more() -> None:
  40. @text_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. @text_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')