Repository navigation
Using URL hash as storage #6356
Replies: 3 comments
|
I think this goes against how NiceGUI works: the page is built on the server, and the server never sees the hash w/o first establishing the JS+websocket, which is slow. Compare what goes over the network in each case. Server-side storage (
Hash storage:
Query params give you the shareable URL and follow the short path, because they are part of the page request. You can keep the URL in sync without a reload using Query params demofrom nicegui import ui
@ui.page('/')
def index(filter: str = 'all'):
ui.select(['all', 'open', 'done'], value=filter,
on_change=lambda e: ui.navigate.history.replace(f'?filter={e.value}'))
ui.run()Open |
|
Thanks for the well-argued idea, @johanfforsberg! The use case is real: filters, expander state and similar settings should survive a copied link. But I agree with @evnchn that the hash is not the right place for it in NiceGUI, and I would not add it as a storage type. Two additions to Evan's answer:
If you still prefer the hash for a specific reason, |
|
Thanks for the thoughtful and quick replies! I get your points, basically using the URL hash is a hacky "roundtrip" that requires going an extra step through the browser for each change. The hash is also arguably not intended for this use case, and it already has an established convention. I'll look further into For sure nicegui should stick to web standards and conventions as far as possible. However it also speaks to its flexibility that it's perfectly possible to do this kind of "hack" on the application level, if necessary. |
Uh oh!
There was an error while loading. Please reload this page.
I think the "storage" options in NiceGUI are great and cover a lot of different use cases.
However there is one use case that I think is missing (but correct me if there is already a way!).
In the case where some application state should also be part of the URL, there is already the possibility of using routing and
ui.sub_pages. This is great as it allows e.g. linking directly to a certain part of the application. But this only works for navigation between (sub)pages. In a single page application, there might still be various settings that influence the page. If they are put in NiceGUI storage, they can be persisted, but they are not part of the URL. That can be a drawback e.g. when sending the current URL to someone else, as they won't necessarily see the same thing.There is the URL "hash" (or "fragment") part (after the first #) which is client only. It's never sent to the server, but the browser can read/write it and detect if it is changed. Changing it doesn't cause a page reload. So it's an interesting place to store text data, e.g. encoded as JSON or URL query parameters. I have tried this in some (toy) applications and it's pretty easy to do. But I wonder if it would make sense to add as a NiceGUI "storage" type? It would be totally local to the browser tab.
The hash/fragment is a bit overloaded as it is often used for text "anchors". But from what I can tell there are no restrictions on how it can be used. It could get confusing though, and maybe the URL hash is too easy to "lose" to use it for any important state. I am mainly thinking about things like filters, open/closed state of expanders, etc.
All reactions