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

Raise when express.[input,output,session] are used outside of Express app #1067

Merged
merged 1 commit into from
Jan 25, 2024
Merged
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
34 changes: 31 additions & 3 deletions shiny/express/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,39 @@
# returns the input for the current session. This will work in the vast majority of
# cases, but when it fails, it will be very confusing.
def __getattr__(name: str) -> object:
session = _get_current_session()

if name == "input":
return _get_current_session().input # pyright: ignore
if session is None:
return _ExpressOnlyPlaceholder("input")
else:
return session.input # pyright: ignore
elif name == "output":
return _get_current_session().output # pyright: ignore
if session is None:
return _ExpressOnlyPlaceholder("output")
else:
return session.output # pyright: ignore
elif name == "session":
return _get_current_session()
if session is None:
return _ExpressOnlyPlaceholder("session")
else:
return session

raise AttributeError(f"Module 'shiny.express' has no attribute '{name}'")


class _ExpressOnlyPlaceholder:
"""Placeholder class for objects that can only be used in a Shiny Express app."""

def __init__(self, name: str):
self.name = name

def __getattr__(self, name: str) -> None:
raise RuntimeError(
f"shiny.express.{self.name} can only be used inside of a Shiny Express app."
)

def __call__(self, *args: object, **kwargs: object) -> None:
raise RuntimeError(
f"shiny.express.{self.name} can only be used inside of a Shiny Express app."
)
Loading