Skip to content

Value.get: Accept "default" parameter #2010

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions shiny/reactive/_reactives.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,15 @@ def __init__(
def __call__(self) -> T:
return self.get()

def get(self) -> T:
def get(self, default: T | MISSING_TYPE = MISSING) -> T:
"""
Read the reactive value.
Read the reactive value. If value is not set, a default can be optionally
returned.

Parameters
----------
default
An optional default to return if value is not set.

Returns
-------
Expand All @@ -138,14 +144,16 @@ def get(self) -> T:
Raises
------
:class:`~shiny.types.SilentException`
If the value is not set.
If the value is not set and default is not provided.
RuntimeError
If called from outside a reactive function.
"""

self._value_dependents.register()

if isinstance(self._value, MISSING_TYPE):
if default is not MISSING:
return default
raise SilentException

return self._value
Expand Down