-
-
Notifications
You must be signed in to change notification settings - Fork 604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide app.storage.client as a location to store volatile data which only matters for the current connection #2820
Provide app.storage.client as a location to store volatile data which only matters for the current connection #2820
Conversation
…in the current Client instance - which in practice means "per browser tab".
Moved context import to top of the file
Adjusted documentation of app.storage.client.
…eature/per_session_data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you replace the double quotes with single quotes? See https://github.com/zauberzeug/nicegui/blob/main/CONTRIBUTING.md#single-vs-double-quotes
Added app.storage.client clear test
…eature/per_session_data
I just started a first proof-of-conecept for |
Done. Have read the CONTRIBUTING.md of course, after so many years of C++ and double quotes for everything except single character it though "hurts" as much as I would have to use a knife in my left and fork in my right hand ;-)... fixed the other branch/PR as well. |
Dropped implementation client.current_client for now Added exceptions for calls to app.storage.client from auto index or w/o client connection. Added tests for changes
Added the missing checks + unit tests, removed Client.current_client and added the sections to the documentation page based upon your examples in the .tab variant. |
Removed client connection state checks and tests
…eature/per_session_data
Updated. I already referred #2837 in the documentation to explain when to use tab and when to use client. The count in It features four built-in storage types: needs to be adjusted accordingly when both PRs should be united in the main branch. |
…data # Conflicts: # nicegui/storage.py # website/documentation/content/storage_documentation.py
Resolved the conflicts with the new main branch and adjusted the doc accordingly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just made minimal changes to the documentation. In markdown we like start each sentence in a new line.
@falkoschindler it would be great if you can have a look, too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reviewed the code and made a few minor changes. Most prominently I moved the tests into test_storage.py. Even though this file is already pretty long, we tend to move tests for file x.py into a file test_x.py. So all storage tests are in one place.
I've got three questions remaining:
-
- Is
client.state
a good name for this kind of storage? What do you think aboutclient.storage
? I'm undecided though. Since it is very volatile, it's somehow less than real storage. But it is implemented and documented as "storage".
- Is
-
- Why do we need to call
get_slot_stack()
inclear()
? I guess it's for some important but slightly unintuitive reason.
- Why do we need to call
-
- Why are there two buttons and two labels in
test_client_storage
?
- Why are there two buttons and two labels in
Thanks @falkoschindler, regarding your questions:
The reason why I chose state was actually 1:1 taken from Streamlit st.session_state which has exactly the same behaviour - "keep the state" as long as the user is connected. I think it's a bit philosophical what one interpretes into the words "state" and "storage", after all its also called Solid State Drive and not Solid Storage Drive, right? ;-)
Otherwise app.reset crashes during the tests because it is also called w/o an available slot and then context.get_client would crash.
Fixed, I assume through a git merge which duplicated those lines. |
… into feature/per_session_data
Ok, I've rewritten this part a little:
|
Code for testing all our storages: @ui.page('/')
async def index():
if 'time' not in app.storage.browser:
app.storage.browser['time'] = time.time()
ui.label(f'{app.storage.browser["time"]=}')
if 'time' not in app.storage.client:
app.storage.client['time'] = time.time()
ui.label(f'{app.storage.client["time"]=}')
if 'time' not in app.storage.user:
app.storage.user['time'] = time.time()
ui.label(f'{app.storage.user["time"]=}')
if 'time' not in app.storage.general:
app.storage.general['time'] = time.time()
ui.label(f'{app.storage.general["time"]=}')
await context.get_client().connected()
if 'time' not in app.storage.tab:
app.storage.tab['time'] = time.time()
ui.label(f'{app.storage.tab["time"]=}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is ready to merge now. Thanks everyone! 🙂
As discussed in pull request #2811 and the discussion Add app.storage.tab or similar #1308 I added a feature which allows the storage of data per Client connection.
This - in practice - and when either implementing an own single page application approach, e.g. based upon the example/single_page_app or when #2811 is merged enables developers to create per-browser tab user data instances, for example with different logins and/or settings with the same comfort of as currently when using app.storage.user - so without handing over another per-page instance from event handler to handler.