Skip to content
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

Introduce ui.context as a simpler method to get Client, Slot-Stack etc #2879

Merged
merged 5 commits into from
Apr 22, 2024

Conversation

rodja
Copy link
Member

@rodja rodja commented Apr 12, 2024

As I wrote #2811 (comment), it hit me that we could provide ui.context to get the current client object or Slot-Stack. I also think it would later also be a good place to cluster page_title, favicon, viewport, ... which currently can only be configured via page builder decorator.

Copy link
Contributor

@falkoschindler falkoschindler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder an API like ui.context.client was possible. This would require context to be a class instance and I'm not sure if this is possible and practical. It would also hide the fact that client isn't a constant field, but the result of a function. So we might just leave it as ui.context.get_client().

router.py Outdated Show resolved Hide resolved
@rodja rodja added this to the 1.4.22 milestone Apr 14, 2024
@Alyxion
Copy link
Contributor

Alyxion commented Apr 15, 2024

I wonder an API like ui.context.client was possible. This would require context to be a class instance and I'm not sure if this is possible and practical. It would also hide the fact that client isn't a constant field, but the result of a function. So we might just leave it as ui.context.get_client().

In the end always the balance between lazyness/simplicity and readability. In Flask world thats basically exactly what request does when you import it from Flask. It - where ever you are in the code execution right now as long as the HTTP API callback is in the call stack - will always contain the current context's / thread's request properties even though it looks like a global variable.

_cv_request: ContextVar[RequestContext] = ContextVar("flask.request_ctx")
request_ctx: RequestContext = LocalProxy(  # type: ignore[assignment]
    _cv_request, unbound_message=_no_req_msg
)
request: Request = LocalProxy(  # type: ignore[assignment]
    _cv_request, "request", unbound_message=_no_req_msg
)
session: SessionMixin = LocalProxy(  # type: ignore[assignment]
    _cv_request, "session", unbound_message=_no_req_msg
)

see https://github.com/pallets/flask/blob/main/src/flask/globals.py

@falkoschindler falkoschindler modified the milestones: 1.4.22, 1.4.23 Apr 15, 2024
@falkoschindler falkoschindler merged commit 1541006 into main Apr 22, 2024
7 checks passed
@falkoschindler falkoschindler deleted the ui-context branch April 22, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants