Skip to content

Parameters

panel_flowdash.parameters

Parameters: the named config fields a dashboard can be run with, and their contracts.

A parameter points at one field of one node's config, e.g. the file a source node reads or a widget's default. Its default is whatever that field holds; binding a value writes it into the field before the dashboard computes, so a saved dashboard reruns on other data or with other inputs, with no template syntax inside config values.

A component can declare what a value bound to one of its fields must satisfy with register(contract=...), a :class:ParameterContract. Its expects records what the dashboard relies on, e.g. the columns and types the nodes downstream of a data source read, and is stored on the parameter. Its check runs whenever the parameter is rebound and refuses values that break the record, naming the nodes that would fail, before anything is applied or computed.

In the dashboard YAML they form a top-level parameters mapping by name, whose entries look like this::

orders:
  node: warehouse
  field: ref
  type: source
  description: Order lines, one row per order
  expects: {...}         # written by the component's contract
since: {node: since, field: value, type: date}

ContractLookup = Callable[[str], t.Any] module-attribute

NAME_PATTERN = re.compile('^[A-Za-z_][A-Za-z0-9_-]{0,63}$') module-attribute

PARAMETER_KEYS = ('node', 'field', 'type', 'description', 'expects') module-attribute

PARAMETER_TYPES = ('string', 'number', 'integer', 'boolean', 'date', 'datetime', 'value', 'source', 'table', 'file', 'uri') module-attribute

RESTRICTED_COMPONENT = 'flowdash:restricted' module-attribute

TEXT_TYPES = ('string', 'source', 'table', 'file', 'uri') module-attribute

_FALSE = ('false', 'no', 'off', '0') module-attribute

_MISSING = object() module-attribute

_SEGMENT = re.compile('^(?:[A-Za-z_][A-Za-z0-9_-]*|[0-9]+)$') module-attribute

_TRUE = ('true', 'yes', 'on', '1') module-attribute

__all__ = ['PARAMETER_TYPES', 'ContractContext', 'ContractError', 'ContractProblem', 'DashboardParameter', 'ParameterContract', 'ParameterError', 'bind_document', 'check_contracts', 'coerce', 'compute_expects', 'decode_parameters', 'get_field', 'parameters_schema', 'set_field'] module-attribute

logger = logging.getLogger('panel_flowdash') module-attribute

ContractContext dataclass

What a :class:ParameterContract gets to compute or check a parameter's contract.

Attributes:

Name Type Description
name str

The parameter's name.

parameter DashboardParameter

The parameter, with node as the node's name in document.

value Any

The value the parameter's field holds, the bound one when checking.

document dict[str, Any]

The whole dashboard in the YAML format, with the value bound, as plain data.

context Mapping[str, Any]

The editor's context (see FlowDash.context), where hosts put what nodes may use, e.g. data sources.

context = field(default_factory=dict) class-attribute instance-attribute

document instance-attribute

name instance-attribute

parameter instance-attribute

value instance-attribute

downstream()

Return the names of the nodes that read the parameter's node, directly or not, nearest first.

ContractError

Bases: ValueError

A bound value breaks what the dashboard relies on; nothing was applied.

problems maps each failing parameter's name to its problems.

nodes property

The nodes the problems name, in order, each once.

problems = {name: list(items) for name, items in problems.items()} instance-attribute

ContractProblem dataclass

Something a bound value lacks that the dashboard relies on.

node names the node (as in the YAML) that would fail, or is None for a problem with the value as a whole, e.g. a source that cannot be read. A warning does not refuse the value, e.g. a node whose query could not be analysed, so the contract does not cover it.

message instance-attribute

node instance-attribute

warning = False class-attribute instance-attribute

DashboardParameter dataclass

A named pointer to one config field of one node.

Attributes:

Name Type Description
node str

The node: its instance id in a :class:~panel_flowdash.AssetModel, its name in the YAML.

field str

A dotted path into the node's config, e.g. ref or spec.tables.orders; a number indexes a list.

type str

One of :data:PARAMETER_TYPES.

description str | None

What the parameter is, for whoever binds it.

expects dict[str, Any] | None

What the dashboard relies on of a bound value, as the node's component's :class:ParameterContract recorded it. Opaque to FlowDash.

description = None class-attribute instance-attribute

expects = None class-attribute instance-attribute

field instance-attribute

node instance-attribute

type = 'value' class-attribute instance-attribute

from_dict(data) classmethod

Read a parameter, raising :class:ParameterError if it is malformed.

to_dict()

ParameterContract

Bases: Protocol

What a component requires of a value bound to a parameter on its config.

Pass one to register(contract=...). Both methods are synchronous and should not fetch data beyond metadata.

check(context, expects)

Return the problems of the value in context against expects, none when it fits.

expects(context)

Return what the dashboard relies on of the parameter's current value, as JSON data.

None when it cannot tell, e.g. without access to the data; the parameter then keeps what it recorded before.

ParameterError

Bases: ValueError

A parameter, or a value bound to one, is malformed. key names the offending part.

key = key instance-attribute

reason = reason instance-attribute

_check_json(value)

_child(value, key, path)

_contexts(doc, names, lookup, context)

_problems(result)

bind_document(doc, values)

Return a copy of the YAML document doc with values written into their parameters' fields.

Each value is read as its parameter's type first (see :func:coerce), so values given as text, e.g. on a command line, can be passed as is. Nothing is computed and no contract is checked; see :func:check_contracts.

Raises:

Type Description
ParameterError

If a name is not a parameter of doc, a value is not of its parameter's type, or a parameter's node or field does not exist. The key is parameters.<name>.

check_contracts(doc, names, lookup, context=None)

Check the parameters names of the YAML document doc, values bound, against their contracts.

lookup returns the :class:ParameterContract of a component id, or None. A parameter without a recorded expects or without a contract is not checked. A contract that raises fails its parameter with the error as the problem. Returns the problems, warnings included, of the parameters that have any.

check_name(name)

coerce(kind, value)

Return value as a value of parameter type kind, reading text as that type.

Raises :class:ParameterError if it is not one.

compute_expects(doc, names, lookup, context=None)

Return what the contracts of the parameters names of doc record now.

Parameters whose contract returns None, or raises, are left out, so they keep what they recorded before.

decode_parameters(value)

Return stored parameters, which may be missing (None), by name.

Raises TypeError unless they are a mapping, and :class:ParameterError for a malformed parameter. Parameters are optional, so the stores catch this and drop just the parameters, with a warning.

error_text(exc)

Return an exception's message with its credentials masked, to show a user or an agent.

failures(problems)

Return the problems that refuse a value, leaving out warnings.

field_path(path)

Split a parameter's field into its keys, numbers for list items.

Raises :class:ParameterError unless it is a dotted path of names and numbers that starts with a name, e.g. spec.layers.0.mark.

get_field(config, path)

Return the value at path in config, or raise :class:ParameterError.

parameters_schema(parameters, values=None)

Return the JSON Schema of a form that binds parameters, e.g. for panel-agents' SchemaForm.

Each parameter is a property titled by its name, described by its description, with the value it holds in values as its default.

set_field(config, path, value)

Return a copy of config with value at path.

Mappings on the way that do not exist yet are created; a list item must exist.

suggest_name(name, names)