Skip to content

Commit 42fc70f

Browse files
schloerkewch
andauthored
feat: Allow for App server= to take input only (#920)
Co-authored-by: Winston Chang <[email protected]>
1 parent 028721d commit 42fc70f

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

shiny/_app.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import os
55
import secrets
6+
from inspect import signature
67
from pathlib import Path
78
from typing import Any, Callable, Optional, cast
89

@@ -101,19 +102,25 @@ def server(input: Inputs, output: Outputs, session: Session):
101102
def __init__(
102103
self,
103104
ui: Tag | TagList | Callable[[Request], Tag | TagList] | Path,
104-
server: Optional[Callable[[Inputs, Outputs, Session], None]],
105+
server: Callable[[Inputs], None]
106+
| Callable[[Inputs, Outputs, Session], None]
107+
| None,
105108
*,
106109
static_assets: Optional["str" | "os.PathLike[str]" | dict[str, Path]] = None,
107110
debug: bool = False,
108111
) -> None:
109112
if server is None:
110-
111-
def _server(inputs: Inputs, outputs: Outputs, session: Session):
112-
pass
113-
114-
server = _server
115-
116-
self.server = server
113+
self.server = noop_server_fn
114+
elif len(signature(server).parameters) == 1:
115+
self.server = wrap_server_fn_with_output_session(
116+
cast(Callable[[Inputs], None], server)
117+
)
118+
elif len(signature(server).parameters) == 3:
119+
self.server = cast(Callable[[Inputs, Outputs, Session], None], server)
120+
else:
121+
raise ValueError(
122+
"`server` must have 1 (Inputs) or 3 parameters (Inputs, Outputs, Session)"
123+
)
117124

118125
self._debug: bool = debug
119126

@@ -446,3 +453,17 @@ def file_response_handler(req: Request) -> FileResponse:
446453
file_response_handler,
447454
name="shiny-app-static-assets-" + mount_point,
448455
)
456+
457+
458+
def noop_server_fn(input: Inputs, output: Outputs, session: Session) -> None:
459+
pass
460+
461+
462+
def wrap_server_fn_with_output_session(
463+
server: Callable[[Inputs], None]
464+
) -> Callable[[Inputs, Outputs, Session], None]:
465+
def _server(input: Inputs, output: Outputs, session: Session):
466+
# Only has 1 parameter, ignore output, session
467+
server(input)
468+
469+
return _server

0 commit comments

Comments
 (0)