1
0

storage_documentation.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 mechanism for data persistence within your application.
  10. It features five built-in storage types:
  11. - `app.storage.tab`:
  12. Stored server-side in memory, this dictionary is unique to each non-duplicated tab session and can hold arbitrary objects.
  13. Data will be lost when restarting the server until <https://github.com/zauberzeug/nicegui/discussions/2841> is implemented.
  14. This storage is only available within [page builder functions](/documentation/page)
  15. and requires an established connection, obtainable via [`await client.connected()`](/documentation/page#wait_for_client_connection).
  16. - `app.storage.client`:
  17. Also stored server-side in memory, this dictionary is unique to each client connection and can hold arbitrary objects.
  18. Data will be discarded when the page is reloaded or the user navigates to another page.
  19. Unlike data stored in `app.storage.tab` which can be persisted on the server even for days,
  20. `app.storage.client` helps caching resource-hungry objects such as a streaming or database connection you need to keep alive
  21. for dynamic site updates but would like to discard as soon as the user leaves the page or closes the browser.
  22. This storage is only available within [page builder functions](/documentation/page).
  23. - `app.storage.user`:
  24. Stored server-side, each dictionary is associated with a unique identifier held in a browser session cookie.
  25. Unique to each user, this storage is accessible across all their browser tabs.
  26. `app.storage.browser['id']` is used to identify the user.
  27. This storage is only available within [page builder functions](/documentation/page)
  28. and requires the `storage_secret` parameter in`ui.run()` to sign the browser session cookie.
  29. - `app.storage.general`:
  30. Also stored server-side, this dictionary provides a shared storage space accessible to all users.
  31. - `app.storage.browser`:
  32. Unlike the previous types, this dictionary is stored directly as the browser session cookie, shared among all browser tabs for the same user.
  33. However, `app.storage.user` is generally preferred due to its advantages in reducing data payload, enhancing security, and offering larger storage capacity.
  34. By default, NiceGUI holds a unique identifier for the browser session in `app.storage.browser['id']`.
  35. This storage is only available within [page builder functions](/documentation/page)
  36. and requires the `storage_secret` parameter in `ui.run()` to sign the browser session cookie.
  37. The following table will help you to choose storage.
  38. | Storage type | `client` | `tab` | `browser` | `user` | `general` |
  39. |-----------------------------|----------|--------|-----------|--------|-----------|
  40. | Location | Server | Server | Browser | Server | Server |
  41. | Across tabs | No | No | Yes | Yes | Yes |
  42. | Across browsers | No | No | No | No | Yes |
  43. | Across server restarts | No | Yes | No | Yes | Yes |
  44. | Across page reloads | No | Yes | Yes | Yes | Yes |
  45. | Needs page builder function | Yes | Yes | Yes | Yes | No |
  46. | Needs client connection | No | Yes | No | No | No |
  47. | Write only before response | No | No | Yes | No | No |
  48. | Needs serializable data | No | No | Yes | Yes | Yes |
  49. | Needs `storage_secret` | No | No | Yes | Yes | No |
  50. ''')
  51. def storage_demo():
  52. from nicegui import app
  53. # @ui.page('/')
  54. # def index():
  55. # app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  56. # with ui.row():
  57. # ui.label('your own page visits:')
  58. # ui.label().bind_text_from(app.storage.user, 'count')
  59. #
  60. # ui.run(storage_secret='private key to secure the browser session cookie')
  61. # END OF DEMO
  62. app.storage.user['count'] = app.storage.user.get('count', 0) + 1
  63. with ui.row():
  64. ui.label('your own page visits:')
  65. ui.label().bind_text_from(app.storage.user, 'count')
  66. @doc.demo('Counting page visits', '''
  67. Here we are using the automatically available browser-stored session ID to count the number of unique page visits.
  68. ''')
  69. def page_visits():
  70. from collections import Counter
  71. from datetime import datetime
  72. from nicegui import app
  73. # counter = Counter()
  74. # start = datetime.now().strftime('%H:%M, %d %B %Y')
  75. #
  76. # @ui.page('/')
  77. # def index():
  78. # counter[app.storage.browser['id']] += 1
  79. # ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
  80. #
  81. # ui.run(storage_secret='private key to secure the browser session cookie')
  82. # END OF DEMO
  83. counter[app.storage.browser['id']] += 1
  84. ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
  85. @doc.demo('Storing UI state', '''
  86. Storage can also be used in combination with [`bindings`](/documentation/section_binding_properties).
  87. Here we are storing the value of a textarea between visits.
  88. The note is also shared between all tabs of the same user.
  89. ''')
  90. def ui_state():
  91. from nicegui import app
  92. # @ui.page('/')
  93. # def index():
  94. # ui.textarea('This note is kept between visits') \
  95. # .classes('w-full').bind_value(app.storage.user, 'note')
  96. # END OF DEMO
  97. ui.textarea('This note is kept between visits').classes('w-full').bind_value(app.storage.user, 'note')
  98. @doc.demo('Storing data per browser tab', '''
  99. When storing data in `app.storage.tab`, a single user can open multiple tabs of the same app, each with its own storage data.
  100. This may be beneficial in certain scenarios like search or when performing data analysis.
  101. It is also more secure to use such a volatile storage for scenarios like logging into a bank account or accessing a password manager.
  102. ''')
  103. def tab_storage():
  104. from nicegui import app
  105. # @ui.page('/')
  106. # async def index():
  107. # await ui.context.client.connected()
  108. with ui.column(): # HIDE
  109. app.storage.tab['count'] = app.storage.tab.get('count', 0) + 1
  110. ui.label(f'Tab reloaded {app.storage.tab["count"]} times')
  111. ui.button('Reload page', on_click=ui.navigate.reload)
  112. @doc.demo('Maximum age of tab storage', '''
  113. By default, the tab storage is kept for 30 days.
  114. You can change this by setting `app.storage.max_tab_storage_age`.
  115. *Added in version 2.10.0*
  116. ''')
  117. def max_tab_storage_age():
  118. from nicegui import app
  119. from datetime import timedelta
  120. # app.storage.max_tab_storage_age = timedelta(minutes=1).total_seconds()
  121. ui.label(f'Tab storage age: {timedelta(minutes=1).total_seconds()} seconds') # HIDE
  122. # @ui.page('/')
  123. # def index():
  124. # ui.label(f'Tab storage age: {app.storage.max_tab_storage_age} seconds')
  125. @doc.demo('Short-term memory', '''
  126. The goal of `app.storage.client` is to store data only for the duration of the current page visit.
  127. In difference to data stored in `app.storage.tab`
  128. - which is persisted between page changes and even browser restarts as long as the tab is kept open -
  129. the data in `app.storage.client` will be discarded if the user closes the browser, reloads the page or navigates to another page.
  130. This is beneficial for resource-hungry, intentionally short-lived or sensitive data.
  131. An example is a database connection, which should be closed as soon as the user leaves the page.
  132. Additionally, this storage useful if you want to return a page with default settings every time a user reloads.
  133. Meanwhile, it keeps the data alive during in-page navigation.
  134. This is also helpful when updating elements on the site at intervals, such as a live feed.
  135. ''')
  136. def short_term_memory():
  137. from nicegui import app
  138. # @ui.page('/')
  139. # async def index():
  140. with ui.column(): # HIDE
  141. cache = app.storage.client
  142. cache['count'] = 0
  143. ui.label().bind_text_from(cache, 'count', lambda n: f'Updated {n} times')
  144. ui.button('Update content',
  145. on_click=lambda: cache.update(count=cache['count'] + 1))
  146. ui.button('Reload page', on_click=ui.navigate.reload)
  147. doc.text('Indentation', '''
  148. By default, the general and user storage data is stored in JSON format without indentation.
  149. You can change this to an indentation of 2 spaces by setting
  150. `app.storage.general.indent = True` or `app.storage.user.indent = True`.
  151. ''')
  152. doc.text('Redis storage', '''
  153. You can use [Redis](https://redis.io/) for storage as an alternative to the default file storage.
  154. This is useful if you have multiple NiceGUI instances and want to share data across them.
  155. To activate this feature install the `redis` package (`pip install nicegui[redis]`)
  156. and provide the `NICEGUI_REDIS_URL` environment variable to point to your Redis server.
  157. Our [Redis storage example](https://github.com/zauberzeug/nicegui/tree/main/examples/redis_storage) shows
  158. how you can setup it up with a reverse proxy or load balancer.
  159. Please note that the Redis sync always contains all the data, not only the changed values.
  160. - For `app.storage.general` this is the whole dictionary.
  161. - For `app.storage.user` it's all the data of the user.
  162. - For `app.storage.tab` it's all the data stored for this specific tab.
  163. If you have large data sets, we suggest to use a database instead.
  164. See our [database example](https://github.com/zauberzeug/nicegui/blob/main/examples/sqlite_database/main.py) for a demo with SQLite.
  165. But of course to sync between multiple instances you should replace SQLite with PostgreSQL or similar.
  166. *Added in version 2.10.0*
  167. ''')