12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import logging
- from nicegui import ui
- from ..documentation_tools import text_demo
- def main_demo() -> None:
- """Storage
- NiceGUI offers a straightforward method for data persistence within your application.
- It features three built-in storage types:
- - `app.storage.user`: Stored server-side, each dictionary is associated with a unique identifier held in a browser session cookie. Unique to each user, this storage is accessible across all their browser tabs.
- - app.storage.general`: Also stored server-side, this dictionary provides a shared storage space accessible to all users.
- - `app.storage.browser`: Unlike the previous types, this dictionary is stored directly as the browser session cookie, shared among all browser tabs for the same user. However, `app.storage.user` is generally preferred due to its advantages in reducing data payload, enhancing security, and offering larger storage capacity.
- """
- from nicegui import app
- # @ui.page('/')
- # def index():
- # app.storage.user['count'] = app.storage.user.get('count', 0) + 1
- # with ui.row():
- # ui.label('your own page visits:')
- # ui.label().bind_text_from(app.storage.user, 'count')
- #
- # ui.run(storage_secret='private key to secure the browser session cookie')
- # END OF DEMO
- app.storage.user['count'] = app.storage.user.get('count', 0) + 1
- with ui.row():
- ui.label('your own page visits:')
- ui.label().bind_text_from(app.storage.user, 'count')
- def more() -> None:
- @text_demo('Counting page visits', '''
- Here we are using the automatically available browser stored session id to count the number of unique page visits.
- ''')
- def page_visits():
- from collections import Counter
- from datetime import datetime
- from nicegui import app
- counter = Counter()
- start = datetime.now().strftime('%H:%M, %d %B %Y')
- @ui.page('/')
- def index():
- counter[app.storage.session.browser[id]] += 1
- ui.label(f'{len(counter)} unique views ({sum(counter.values())} overall) since {start}')
- # ui.run(storage_secret='private key to secure the browser session cookie')
|