Skip to content

feat: Allow for App server= to take input only #920

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

Merged
merged 8 commits into from
Dec 19, 2023
37 changes: 29 additions & 8 deletions shiny/_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import os
import secrets
from inspect import signature
from pathlib import Path
from typing import Any, Callable, Optional, cast

Expand Down Expand Up @@ -101,19 +102,25 @@ def server(input: Inputs, output: Outputs, session: Session):
def __init__(
self,
ui: Tag | TagList | Callable[[Request], Tag | TagList] | Path,
server: Optional[Callable[[Inputs, Outputs, Session], None]],
server: Callable[[Inputs], None]
| Callable[[Inputs, Outputs, Session], None]
| None,
*,
static_assets: Optional["str" | "os.PathLike[str]" | dict[str, Path]] = None,
debug: bool = False,
) -> None:
if server is None:

def _server(inputs: Inputs, outputs: Outputs, session: Session):
pass

server = _server

self.server = server
self.server = noop_server_fn
elif len(signature(server).parameters) == 1:
self.server = wrap_server_fn_with_output_session(
cast(Callable[[Inputs], None], server)
)
elif len(signature(server).parameters) == 3:
self.server = cast(Callable[[Inputs, Outputs, Session], None], server)
else:
raise ValueError(
"`server` must have 1 (Inputs) or 3 parameters (Inputs, Outputs, Session)"
)

self._debug: bool = debug

Expand Down Expand Up @@ -446,3 +453,17 @@ def file_response_handler(req: Request) -> FileResponse:
file_response_handler,
name="shiny-app-static-assets-" + mount_point,
)


def noop_server_fn(input: Inputs, output: Outputs, session: Session) -> None:
pass


def wrap_server_fn_with_output_session(
server: Callable[[Inputs], None]
) -> Callable[[Inputs, Outputs, Session], None]:
def _server(input: Inputs, output: Outputs, session: Session):
# Only has 1 parameter, ignore output, session
server(input)

return _server