1
0

storage_documentation.py 4.1 KB

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