Dataflow Engine¶
panel_flowdash.dataflow_engine
¶
Dataflow wiring engine with runtime validation.
Each node in the graph gets a NodeState (a dynamic Parameterized subclass)
whose parameters correspond to the node's declared input and output ports.
Every edge is a param.rx expression on its source port, extended by one
pipe per step of the edge's transform, and a connected input is bound to the
expressions feeding it as a param reference, so param itself propagates
values. All edges leaving a port follow one relay of it, which forwards each
value inside the graph's hold context, so a change reaches every consumer
before the hold releases anything. The binding checks each value against the
target first; a rejected value, like a failing transform step, is reported via
the error callback and skipped, which leaves the target on its last good value.
A change entering the graph opens a propagation pass, see
:meth:DataflowGraph.propagation. Within it the values reaching a node's inputs
are held and delivered together once every node upstream of it in the pass has
run, in topological order, so a node runs once per pass and never sees a mix
of new and old inputs.
With propagate_failures a failure is carried along the edge instead, as a
param.ReactiveError value. An input that handles failures receives it on
its port; any other input keeps its last good value, records the failure in the
node's :class:InputFailures, and its node passes the failure on to every
output so that nothing downstream keeps presenting stale data as current.
EdgeKey = tuple[str, str, str, str]
module-attribute
¶
RESTRICTED_ERROR_TEXT = 'Restricted: you do not have access to this data.'
module-attribute
¶
_MAX_DELIVERIES = 100
module-attribute
¶
_NOT_FORWARDED = object()
module-attribute
¶
_NO_LITERAL = object()
module-attribute
¶
_RESERVED_PARAMS = set(param.Parameterized.param)
module-attribute
¶
_STACK_MARGIN = 100
module-attribute
¶
logger = logging.getLogger('panel_flowdash')
module-attribute
¶
ComponentSpec
dataclass
¶
Full specification of a component's ports and metadata.
check_config = None
class-attribute
instance-attribute
¶
component_id
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_state_class = None
class-attribute
instance-attribute
¶
contract = None
class-attribute
instance-attribute
¶
default_size
instance-attribute
¶
description
instance-attribute
¶
framed = None
class-attribute
instance-attribute
¶
icon
instance-attribute
¶
inputs
instance-attribute
¶
max_size = None
class-attribute
instance-attribute
¶
min_size = None
class-attribute
instance-attribute
¶
outputs
instance-attribute
¶
port_aliases = field(default_factory=dict)
class-attribute
instance-attribute
¶
tags
instance-attribute
¶
title
instance-attribute
¶
resolve_name(name)
¶
Return the current name of a port or config field that may have been renamed.
A name that is still a port or config field is never redirected, so an alias cannot shadow a live name.
DataflowGraph
¶
Manages node state instances and wires edges with runtime validation.
edges
property
¶
All current edges.
node_ids
property
¶
All current node instance IDs.
propagate_failures = propagate_failures
instance-attribute
¶
wiring
property
¶
Whether an edge or literal is being changed, so the inputs it sets change because of an edit.
add_edge(source_id, source_port, target_id, target_port, transform=None)
¶
Wire an edge, returning True on success or a rejection reason.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
list[dict[str, Any]] | None
|
Steps applied to every value carried along the edge, each a dict
naming an |
None
|
add_node(instance_id, component_id)
¶
Create a new node state instance.
batch()
¶
Defer binding list inputs until the block exits.
Every edge into a list input rebinds it to all of its edges, so wiring N edges into one list input one by one costs O(N^2). Inside this block each list input is bound once, when the block exits, with all of its edges. Wiring an edge into any other input from a node downstream of a pending list input binds the pending inputs first, since validating the edge reads the value that node produces.
clear_literal(instance_id, port)
¶
Remove the literal value of an input; an unwired input goes back to its default.
That is the port's default, or for a port without one the default of the component's Parameter, the value the component had before.
get_config_state(instance_id)
¶
Get the config state instance for a node, if it has config.
get_edge_expression(source_id, source_port, target_id, target_port, stage=-1)
¶
Return the reactive expression carrying values along an edge, if it is wired.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
int
|
Number of transform steps applied: 0 is the source port's own value.
The default -1 is what the target receives, which with
|
-1
|
get_edge_transform(source_id, source_port, target_id, target_port)
¶
Return the transform steps of an edge as stored, or None if it is not wired.
get_failures(instance_id)
¶
Get the failures held by a node's inputs, if the node exists.
get_state(instance_id)
¶
Get the state instance for a node.
is_wired(instance_id, port)
¶
Whether an edge feeds the input port of a node.
literals(instance_id)
¶
Return a copy of the literal values set on a node's inputs, by port.
propagation()
¶
Deliver the values reaching node inputs inside this block as one pass.
Every value an edge carries to an input is held until the block exits, then each node receives all of its held values in one update, upstream nodes first, so a node whose inputs change together runs once with all of them. Values the nodes produce meanwhile join the same pass. A change of an output opens a pass of its own; wrap a change of several nodes at once, such as a refresh that runs every source again, in this block. Nested blocks join the outer pass.
register_specs(specs)
¶
Add component specs to the graph, keeping existing nodes intact.
Components are imported on demand, so specs arrive after the graph is constructed. Registering them incrementally avoids rebuilding the graph and losing the nodes and edges already placed on it.
remove_edge(source_id, source_port, target_id, target_port)
¶
Remove an edge and reset the target input to its literal or its default, see _default.
remove_node(instance_id)
¶
Remove a node and any edges connected to it.
The node's state is detached from everything it was bound to, both the edges feeding its inputs and any references its outputs follow, so it stops receiving values but keeps the ones it last held.
set_edge_transform(source_id, source_port, target_id, target_port, transform)
¶
Replace the transform of an existing edge, returning True or a rejection reason.
A rejected transform leaves the edge on its previous one.
set_literal(instance_id, port, value)
¶
Give the input port of a node a literal value, used while no edge feeds it.
The value is checked against the input first; a wired input keeps the value of its edge and takes the literal once the edge is removed.
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input does not exist, cannot hold a literal or rejects value. |
validate_connection(source_id, source_port, target_id, target_port, transform=None, *, types=True)
¶
Return a rejection reason without changing graph state, or None if allowed.
With types False the ports' types and the value the edge would carry are not checked, only the wiring rules: ports that exist, a free input, no duplicate and no cycle.
Edge
dataclass
¶
The transform steps of an edge and the expression after each of them.
stages[0] is the source port, as forwarded by its relay, and
stages[i] is the value after i steps. expr is what the target
receives, which is stages[-1] unless failures propagate, when
carried replaces it with the source node's failure.
InputFailure
dataclass
¶
InputFailures
¶
Bases: Parameterized
The inputs of one node that currently hold a failure, keyed by port name.
failed = param.Dict(default={}, doc='\n Failures that reached inputs which do not handle them, by input port.\n Replaced, never mutated, so a watcher sees every change.')
class-attribute
instance-attribute
¶
StepInputError
¶
Bases: TypeError
Raised when a step is applied to a kind of value it does not accept.
Transform
¶
Bases: Parameterized
One step of an edge transform.
Subclasses set op to register themselves under that name and implement
:meth:apply. accepts tells the editor which values a step can follow.
label = ''
class-attribute
¶
op = ''
class-attribute
¶
accepts(value)
classmethod
¶
Return whether this step can be applied to value.
apply(value)
¶
Return value transformed by this step.
arguments()
classmethod
¶
Names of the arguments a step of this operation stores.
summary()
¶
Return a few words describing the step, for the label on an edge.
to_dict()
¶
Serialize the step to the dict it is stored as.
_Relay
¶
Bases: Parameterized
The value of one output port, as followed by every edge leaving it.
value = param.Parameter()
class-attribute
instance-attribute
¶
_check_item(port, value)
¶
Return why a list input would reject value as one of its items, or None.
_is_list_port(port)
¶
Return True if a port's declared type indicates a list/multi-connection input.
_item_class(port)
¶
Return the class each item of a list input's Parameter must be, or None.
_release_refs(state)
¶
Replace every reference a state follows with the value it currently resolves to.
_rewires(method)
¶
Mark the graph as wiring while method changes what feeds an input, see DataflowGraph.wiring.
_stack_deeper_than(depth)
¶
apply_transform(steps, value)
¶
Run value through steps; an unset value stays unset.
build_node_state_class(spec)
¶
Create a Parameterized subclass with one param per port (inputs + outputs).
check_literal(port, value)
¶
Return why value cannot be the literal of port, or None if it can.
connection_type(port)
¶
Return the type one edge into port must carry.
Each edge into a list input delivers one item, so the item type applies:
the declared type of a multiple input that is not itself a list, or
the item_type of a List Parameter when it names a registered port
type. A plain List input says nothing about its items and accepts
anything, as does an item_type of a class with no registered port
type, whose subclasses only the check of each value can tell apart.
error_text(exc)
¶
Return an exception's message with its credentials masked, to show a user or an agent.
exception_text(exc)
¶
Describe an exception as Type: message, or a refusal (see is_restricted) neutrally.
The neutral text names nothing the viewer may not see; the server log keeps the error.
failure_text(error)
¶
Describe a failure as Type: message.
is_failure(value)
¶
Whether value is a failure carried along an edge as a param.ReactiveError.
is_registered_port_type(name)
¶
Return whether name is a registered port type or one of its aliases.
is_restricted(error)
¶
Return whether error is a refusal of access: a PermissionError, or one caused by it.
Takes the param.ReactiveError a failed output carries too. A component
that wraps a failure of its input in an error of its own keeps the refusal
recognisable by raising it from the original; an error raised while
handling one with the context suppressed (raise ... from None) is not a
refusal. A PermissionError the OS raised, whose errno is a real OS
error number (EACCES, EPERM), is a fault of the deployment, such as
an unreadable file, and not a refusal either; one whose first argument is
some other number, e.g. PermissionError(403, "Forbidden"), is a refusal.
An exception group is a refusal when any of its members is.
normalize_port_type(name)
¶
Return the canonical name of a port type, or None for an untyped port.
Unregistered names normalize to themselves, lowercased, so two ports with the same custom type name still connect.
parse_transform(steps)
¶
Build the steps of a stored transform, raising ValueError if one is invalid.
port_literal(port, value)
¶
Return a stored literal in the form port takes, e.g. a list read from a file as a tuple.
port_types_compatible(source, target)
¶
Return whether an output of type source may connect to an input of type target.
An untyped port, or one typed any (object, a bare Parameter,
a Selector or ClassSelector whose value type the name does not
tell), connects to anything; otherwise the types must be equal or source
a registered subtype of target.