storage_documentation.py 3.4 KB

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