Component Libraries¶
panel_flowdash.component_library
¶
Normalize heterogeneous component declarations into a registry.
The project-directory workflow discovers components by scanning the filesystem
(:func:~panel_flowdash.registry.build_registry). The programmatic workflow
hands them over directly: decorated functions, Viewer subclasses, a mapping
of explicit ids, a directory, or any mix of those. Both funnel into the same
dict[str, RegistryEntry] that the spec builder and the editor consume.
LIBRARY_ATTRIBUTE = '__flowdash_components__'
module-attribute
¶
logger = logging.getLogger('panel_flowdash')
module-attribute
¶
ComponentLibraryError
¶
Bases: ValueError
A component library named by FlowDashApp.component_libraries cannot be used.
PanelAppMetadata
dataclass
¶
Metadata attached to a component or page by the @register decorator.
aliases = field(default_factory=list)
class-attribute
instance-attribute
¶
allow_groups = field(default_factory=list)
class-attribute
instance-attribute
¶
allow_users = field(default_factory=list)
class-attribute
instance-attribute
¶
auth_pending = False
class-attribute
instance-attribute
¶
authorize = None
class-attribute
instance-attribute
¶
check_config = None
class-attribute
instance-attribute
¶
component = False
class-attribute
instance-attribute
¶
config = field(default_factory=list)
class-attribute
instance-attribute
¶
config_choices = None
class-attribute
instance-attribute
¶
config_completions = None
class-attribute
instance-attribute
¶
config_editor = None
class-attribute
instance-attribute
¶
config_schema = None
class-attribute
instance-attribute
¶
contract = None
class-attribute
instance-attribute
¶
default_size = None
class-attribute
instance-attribute
¶
deny_groups = field(default_factory=list)
class-attribute
instance-attribute
¶
deny_users = field(default_factory=list)
class-attribute
instance-attribute
¶
description = None
class-attribute
instance-attribute
¶
extensions = field(default_factory=list)
class-attribute
instance-attribute
¶
framed = None
class-attribute
instance-attribute
¶
handles_failures = field(default_factory=list)
class-attribute
instance-attribute
¶
icon = None
class-attribute
instance-attribute
¶
max_size = None
class-attribute
instance-attribute
¶
min_size = None
class-attribute
instance-attribute
¶
page = True
class-attribute
instance-attribute
¶
palette_presets = None
class-attribute
instance-attribute
¶
permission
property
¶
Build a :class:~panel_flowdash.auth.Permission from the declared rules.
placeholder = None
class-attribute
instance-attribute
¶
port_aliases = field(default_factory=dict)
class-attribute
instance-attribute
¶
presets_pending = False
class-attribute
instance-attribute
¶
provides = field(default_factory=list)
class-attribute
instance-attribute
¶
requires = field(default_factory=list)
class-attribute
instance-attribute
¶
sidebar = False
class-attribute
instance-attribute
¶
singleton = False
class-attribute
instance-attribute
¶
tags = field(default_factory=list)
class-attribute
instance-attribute
¶
title = None
class-attribute
instance-attribute
¶
from_app(app)
classmethod
¶
Extract metadata from an app object.
is_registered(app)
staticmethod
¶
Whether @register attached metadata to app.
RegistryEntry
dataclass
¶
A registered component/page with its metadata.
app = None
class-attribute
instance-attribute
¶
app_id
instance-attribute
¶
load_error = field(default=None, repr=False, compare=False)
class-attribute
instance-attribute
¶
metadata
instance-attribute
¶
module_name
instance-attribute
¶
module_path = None
class-attribute
instance-attribute
¶
name
instance-attribute
¶
page_path
instance-attribute
¶
section
instance-attribute
¶
spec = field(default=None, repr=False, compare=False)
class-attribute
instance-attribute
¶
title
property
¶
Human-readable title.
from_app(app, *, app_id=None, section=None, name=None)
classmethod
¶
Build an entry from an already-imported app object.
Unlike :func:build_registry, which discovers modules on disk and defers
importing them, this wraps a live object so load() is a no-op. Used
by the programmatic API where components are passed in directly.
Objects without @register metadata are treated as components, since
a bare Viewer subclass handed to the editor is only ever meant to be
one.
load()
¶
Import the module and return the app object.
Caches the result on self.app. Raises on import failure.
_add(registry, entry, unique=False)
¶
_already(owner)
¶
Turn "the project offers" into "the project already offers".
_component_origin(entry)
¶
_dedupe_id(app_id, taken)
¶
Return app_id, suffixed if needed, so it is unique within taken.
Component ids are persisted as AssetItem.component_id, so a silent
collision would make two different components load each other's saved
nodes. Suffixing keeps both usable; the warning tells the author to pass
explicit ids if they care which is which.
_is_directory(obj)
¶
_library_object(name)
¶
Import the object a library name refers to: module or module:attribute.
Also returns the names of the components a module without __all__
imports from elsewhere, which it does not offer.
_normalize_one(registry, obj, *, app_id=None, unique=False)
¶
Fold a single declaration into registry.
_same_component(a, b)
¶
Whether two entries stand for the same component under the same id.
_scan_directory(path)
¶
Scan a project directory, making its modules importable.
Scanned entries are imported lazily by module name relative to the project
directory, so the directory has to be on sys.path for RegistryEntry.load
to resolve them later. flowdash serve does this itself; a programmatic
caller should not have to.
build_registry(project_dir, *, package=None)
¶
Scan project_dir for page/component modules without importing them.
Reads each .py file with the AST to extract @register metadata.
Modules are not imported at this stage; each RegistryEntry.app is
None until RegistryEntry.load() is called.
By default modules are named relative to project_dir
(<Section>.<name>), so the directory itself must be on sys.path.
Pass package when project_dir is an importable package: modules are
then named <package>.<Section>.<name>, so the project's own imports and
flowdash's lazy imports share one module object. Raises
PackageResolutionError (a ValueError) if package does not
resolve to project_dir.
library_components(name)
¶
Return the components a library offers, keyed by component id.
name is an importable module, or module:attribute naming a mapping
of component id to component, a list of components or a single one. A
module offers what its __flowdash_components__ attribute holds, in
any of those forms, and otherwise every object it exports that
@register marks as a component: those named in its __all__, or
without one, its public names defined in the module or its submodules.
Only components are taken: a library adds no pages. Every object a
library names must carry @register metadata.
Raises:
| Type | Description |
|---|---|
ComponentLibraryError
|
If name does not import, names an object without |
merge_libraries(registry, names, *, reserved=())
¶
Return registry with the components of each library in names added.
An id that is already taken, by registry, by another library or by one
of reserved (e.g. the built-in components), raises instead of letting
one component replace another: dashboards store component ids, so either
choice would silently change what saved dashboards load. The former ids
a library component declares with aliases must not be taken either,
nor may its id be a former id of another component. Adding a library
whose components are already in registry is a no-op, so a registry can
be merged again safely.
Raises:
| Type | Description |
|---|---|
ComponentLibraryError
|
If a library cannot be used (see :func: |
normalize_components(components, *, unique=False)
¶
Build a registry from any supported component declaration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
components
|
Any
|
One of, or a list mixing any of:
|
required |
unique
|
bool
|
Raise a |
False
|
Returns:
| Type | Description |
|---|---|
dict
|
Registry entries keyed by component id. |