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

Add clientdata accessor to session; allow access to input names via dir() #1832

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* `shiny create` includes new and improved `ui.Chat()` template options. Most of these templates leverage the new [`{chatlas}` package](https://posit-dev.github.io/chatlas/), our opinionated approach to interfacing with various LLM. (#1806)

* Client data values (e.g., url info, output sizes/styles, etc.) can now be accessed in the server-side Python code via `session.clientdata`, which is collection of reactive `Inputs`. For example, `session.clientdata.url_search()` reactively reads the URL search parameters. See [this PR](https://github.com/posit-dev/py-shiny/pull/1832) for a more complete example. (#1832)

### Bug fixes

* `ui.Chat()` now correctly handles new `ollama.chat()` return value introduced in `ollama` v0.4. (#1787)
Expand Down
12 changes: 11 additions & 1 deletion shiny/session/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class Session(ABC):
id: str
input: Inputs
output: Outputs
clientdata: Inputs
user: str | None
groups: list[str] | None

Expand Down Expand Up @@ -519,6 +520,7 @@ def __init__(

self.input: Inputs = Inputs(dict())
self.output: Outputs = Outputs(self, self.ns, outputs=dict())
self.clientdata: Inputs = Inputs(dict())

self.user: str | None = None
self.groups: list[str] | None = None
Expand Down Expand Up @@ -692,7 +694,12 @@ def _manage_inputs(self, data: dict[str, object]) -> None:
# The keys[0] value is already a fully namespaced id; make that explicit by
# wrapping it in ResolvedId, otherwise self.input will throw an id
# validation error.
self.input[ResolvedId(keys[0])]._set(val)
k = keys[0]
self.input[ResolvedId(k)]._set(val)

if k.startswith(".clientdata_"):
k2 = k.split("_", 1)[1]
self.clientdata[ResolvedId(k2)]._set(val)

self.output._manage_hidden()

Expand Down Expand Up @@ -1352,6 +1359,9 @@ def __contains__(self, key: str) -> bool:
# creates a reactive dependency, and returns whether the value is set.
return self[key].is_set()

def __dir__(self):
return list(self._map.keys())


# ======================================================================================
# Outputs
Expand Down
Loading