Skip to content

Platform

The Platform class exposes runtime information about the host platform and a select helper for writing platform-aware code without scattering if IS_IOS / if IS_ANDROID branches throughout the codebase.

Platform-aware constants and selectors.

A small, RN-style helper for branching app code on the host platform. The public surface is the Platform class exposing OS, Version, is_ios, is_android, and select so user code can write Platform.select({"ios": ..., ...}) without importing the IS_* flags directly.

Example
import pythonnative as pn

title_size = pn.Platform.select({"ios": 17, "android": 16, "default": 16})

@pn.component
def App():
    return pn.Text(
        f"Running on {pn.Platform.OS} {pn.Platform.Version}",
        style={"font_size": title_size},
    )

Classes:

Name Description
Platform

Platform-aware constants and the select dispatcher.

Functions:

Name Description
get_platform

Return the active platform name.

Platform

Platform-aware constants and the select dispatcher.

All attributes are read at import time. OS is one of "ios", "android", or "test" (the latter when running off-device, e.g., in unit tests).

Methods:

Name Description
select

Pick the value matching the current platform.

Attributes:

Name Type Description
OS str

"ios", "android", or "test".

Version str

Best-effort OS version string ("17.4", "14", "python-3.11").

is_ios bool

True when running inside an iOS app bundle.

is_android bool

True when running inside an Android process.

is_test bool

True when running off-device (no native runtime).

OS class-attribute instance-attribute

OS: str = _detect_os()

"ios", "android", or "test".

Version class-attribute instance-attribute

Version: str = _detect_version()

Best-effort OS version string ("17.4", "14", "python-3.11").

is_ios class-attribute instance-attribute

is_ios: bool = IS_IOS

True when running inside an iOS app bundle.

is_android class-attribute instance-attribute

is_android: bool = IS_ANDROID

True when running inside an Android process.

is_test class-attribute instance-attribute

is_test: bool = OS == 'test'

True when running off-device (no native runtime).

select staticmethod

select(spec: Dict[str, Any], default: Any = None) -> Any

Pick the value matching the current platform.

Looks up spec[Platform.OS], then falls back to spec["native"] (matches both iOS and Android), then to spec["default"], then to the explicit default argument.

Parameters:

Name Type Description Default
spec Dict[str, Any]

Mapping from platform name to value. Recognized keys: "ios", "android", "test", "native", "default".

required
default Any

Value returned when spec has no matching key and no "default" entry.

None

Returns:

Type Description
Any

The matching value, or default when nothing matches.

Example
font = pn.Platform.select(
    {"ios": "Helvetica", "android": "Roboto", "default": None}
)

get_platform

get_platform() -> str

Return the active platform name.

Equivalent to reading Platform.OS, exposed as a function for introspection from non-component code.

Quick reference

Attribute Values
Platform.OS "ios", "android", or "test"
Platform.Version Best-effort OS version string
Platform.is_ios / Platform.is_android / Platform.is_test Booleans
Platform.select(spec, default=None) Pick a value matching the current platform

See also