A web development framework for Python that enables building fast, beautiful, and interactive web applications using reactive programming principles.
npx @tessl/cli install tessl/pypi-shiny@1.4.0A web development framework that enables building fast, beautiful, and interactive web applications using reactive programming principles. Shiny for Python brings the power of reactive programming from R Shiny to Python, allowing developers to create data-driven web applications with minimal web development knowledge.
pip install shinyimport shiny
from shiny import App, ui, render, reactive
from shiny import Inputs, Outputs, Session
# Type imports for API signatures
from typing import Callable, Literal, Mapping, Generator
from htmltools import Tag, TagList, TagChild, TagFunction
from starlette.requests import Request
from pathlib import PathFor Express mode (simplified syntax):
from shiny.express import input, output, ui, renderfrom shiny import App, ui, render, Inputs, Outputs, Session
# Define UI
app_ui = ui.page_fluid(
ui.h1("Hello Shiny!"),
ui.input_slider("n", "Number of observations", 0, 100, 20),
ui.output_text_verbatim("txt")
)
# Define server logic
def server(input: Inputs, output: Outputs, session: Session):
@output
@render.text
def txt():
return f"The value of n is {input.n()}"
# Create app
app = App(app_ui, server)from shiny.express import input, output, ui, render
ui.h1("Hello Express Shiny!")
ui.input_slider("n", "Number of observations", 0, 100, 20)
@render.text
def txt():
return f"The value of n is {input.n()}"Shiny for Python follows a reactive programming model with these key concepts:
Core application class, lifecycle management, and deployment utilities for creating and running Shiny applications.
class App:
def __init__(
self,
ui: Tag | TagList | Callable[[Request], Tag | TagList] | Path,
server: Callable[[Inputs], None] | Callable[[Inputs, Outputs, Session], None] | None = None,
*,
static_assets: str | Path | Mapping[str, str | Path] | None = None,
bookmark_store: Literal["url", "server", "disable"] = "disable",
debug: bool = False,
): ...
def run_app(
app: str | App = "app:app",
host: str = "127.0.0.1",
port: int = 8000,
*,
autoreload_port: int = 0,
reload: bool = False,
reload_dirs: list[str] | None = None,
reload_includes: list[str] | tuple[str, ...] = ("*.py", "*.css", "*.js", "*.html", "*.yml"),
reload_excludes: list[str] | tuple[str, ...] = (".*", "*.pyc"),
ws_max_size: int = 16777216,
log_level: str | None = None,
app_dir: str | None = ".",
factory: bool = False,
launch_browser: bool = False,
dev_mode: bool = True,
**kwargs: object,
) -> None: ...Reactive values, calculations, effects, and event handling that form the core of Shiny's reactive system.
def reactive.value(value: T | None = None, *, read_only: bool = False) -> Value[T]: ...
def reactive.calc(fn: Callable[[], T] | None = None, *, session: Session | None = None) -> Calc[T]: ...
def reactive.effect(fn: Callable[[], None] | None = None, *, suspended: bool = False, priority: int = 0, session: Session | None = None) -> Effect: ...
def reactive.event(*args: Callable[[], object], ignore_none: bool = True, ignore_init: bool = False) -> Callable[[Callable[[], T]], Callable[[], T]]: ...
class reactive.ExtendedTask:
def __init__(self, func: Callable[..., Awaitable[T]]): ...
status: Value[Literal["initial", "running", "success", "error", "cancelled"]]
value: Value[T]
error: Value[BaseException]
def invoke(self, *args: object, **kwargs: object) -> None: ...
def cancel(self) -> None: ...
def result(self) -> T: ...Functions for rendering different types of outputs including text, plots, images, tables, and dynamic UI content.
def render.text(fn: Callable[[], str] | None = None, *, inline: bool = False) -> text: ...
def render.plot(fn: Callable[[], object] | None = None, *, alt: str | None = None, width: float | None = None, height: float | None = None, **kwargs: object) -> plot: ...
def render.table(fn: Callable[[], object] | None = None, *, index: bool = False, classes: str = "table shiny-table w-auto", **kwargs: object) -> table: ...
def render.data_frame(fn: Callable[[], object] | None = None) -> data_frame: ...
def render.ui(fn: Callable[[], Tag | TagChild] | None = None) -> ui: ...
def render.download(fn: Callable[[], str | bytes] | None = None, *, filename: str | Callable[[], str] | None = None, media_type: str | Callable[[], str] | None = None, **kwargs: object) -> download: ...
def render.code(fn: Callable[[], str] | None = None, *, placeholder: bool = True) -> code: ...
def render.image(fn: Callable[[], ImgData] | None = None, *, delete_file: bool = False) -> image: ...
def render.express(fn: Callable[[], None] | None = None, *, inline: bool = False, container: TagFunction | None = None, **kwargs: object) -> express: ...Comprehensive set of UI functions for building layouts, input controls, output containers, and interactive elements.
def ui.page_fluid(*args: TagChild, **kwargs: TagAttr) -> Tag: ...
def ui.input_text(id: str, label: str, value: str = "", **kwargs: TagAttr) -> Tag: ...
def ui.input_slider(id: str, label: str, min: float, max: float, value: float, **kwargs: TagAttr) -> Tag: ...
def ui.output_text(id: str, inline: bool = False, **kwargs: TagAttr) -> Tag: ...
def ui.card(*args: TagChild, **kwargs: TagAttr) -> Tag: ...
def ui.layout_sidebar(sidebar: Sidebar, *args: TagChild, **kwargs: TagAttr) -> Tag: ...Session handling, user input/output management, and session utilities for managing application state and user interactions.
class Session:
def __init__(self, **kwargs: object): ...
def send_custom_message(self, type: str, message: dict[str, object]) -> None: ...
def download(self, id: str, filename: str, media_type: str = "application/octet-stream") -> None: ...
class Inputs:
def __call__(self) -> dict[str, object]: ...
def __getattr__(self, name: str) -> Callable[[], object]: ...
class Outputs:
def __setattr__(self, name: str, value: OutputRenderer[object]) -> None: ...Simplified API for building applications with automatic UI collection and streamlined syntax.
def express.app_opts(
*,
static_assets: str | Path | Mapping[str, str | Path] | None = None,
bookmark_store: Literal["url", "server", "disable"] | None = None,
debug: bool | None = None,
) -> None: ...
def express.wrap_express_app(file: Path) -> App: ...
def express.is_express_app(app: str, app_dir: str | None) -> bool: ...
def express.expressify(fn: Callable[..., T]) -> Callable[..., T]: ...
def express.output_args(**kwargs: object) -> Callable[[Callable[..., T]], Callable[..., T]]: ...
class express.ui.hold:
def __enter__(self) -> TagList: ...
def __exit__(self, *args: object) -> bool: ...Type definitions, exception classes, and utility types used throughout the Shiny framework.
class SafeException(Exception): ...
class SilentException(Exception): ...
class SilentCancelOutputException(SilentException): ...
class FileInfo(TypedDict):
name: str
size: int
type: str
datapath: str
class ImgData(TypedDict):
src: str
width: NotRequired[str | float]
height: NotRequired[str | float]
alt: NotRequired[str]Shiny provides a module system for creating reusable components with proper namespacing:
def module.ui(fn: Callable[[], TagChild]) -> Callable[[str], TagChild]: ...
def module.server(fn: Callable[[Inputs, Outputs, Session], None]) -> Callable[[str, Inputs, Outputs, Session], None]: ...
def module.current_namespace() -> str | None: ...
def module.resolve_id(id: str) -> str: ...Utility functions for application validation and debugging:
def req(*args: object, cancel_output: bool | Literal["progress"] = False) -> None: ...
async def reactive.flush() -> None: ...
def reactive.isolate() -> Generator[None, None, None]: ...