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

Conversation

pfalcon
Copy link

@pfalcon pfalcon commented Jun 8, 2025

The idea is similar to e.g. Python's dict.get() method, which can take an optional default if a given key doesn't exist in the dict.

This change would allow to users to "bootstrap" otherwise circular dependencies in simple cases. For example, it allows to turn this code:

from shiny.express import input, render, ui

@render.express
def myrender():
    ui.input_select("sel", None, ["All", "So", "Much"], selected="All")
    input.sel()

which doesn't render anything nor produces errors (underneath it throws SilentException which isn't reported) into following code:

from shiny.express import input, render, ui

@render.express
def myrender():
    ui.input_select("sel", None, ["All", "So", "Much"], selected=input.sel.get("All"))
    input.sel.get("All")

which works as expected (initial selected value is "All", and if user chooses another, it's preserved across myrender() run).

The idea is similar to e.g. Python's dict.get() method, which can take an
optional default if a given key doesn't exist in the dict.

This change would allow to users to "bootstrap" otherwise circular
dependencies in simple cases. For example, it allows to turn this
code:

```
from shiny.express import input, render, ui

@render.express
def myrender():
    ui.input_select("sel", None, ["All", "So", "Much"], selected="All")
    input.sel()
```

which doesn't render anything nor produces errors (underneath it throws
SilentException which isn't reported) into following code:

```
from shiny.express import input, render, ui

@render.express
def myrender():
    ui.input_select("sel", None, ["All", "So", "Much"], selected=input.sel.get("All"))
    input.sel.get("All")
```

which works as expected (initial selected value is "All", and if user
chooses another, it's preserved across myrender() run).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant