transforms¶
panel_flowdash.transforms
¶
Transform steps that reshape the value carried along an edge.
A transform is stored as a list of plain step dicts such as
{"op": "aggregate", "how": "sum"}, never as code, so loading a dashboard
cannot execute anything arbitrary. Each operation is a Parameterized class
whose params declare and validate the arguments of its step.
MAX_FORMAT_PRECISION = 100
module-attribute
¶
MAX_FORMAT_WIDTH = 1000
module-attribute
¶
TRANSFORMS = {}
module-attribute
¶
_COMPARISONS = {'==': operator.eq, '!=': operator.ne, '<': operator.lt, '<=': operator.le, '>': operator.gt, '>=': operator.ge, 'in': lambda series, values: series.isin(values)}
module-attribute
¶
_STANDARD_SPEC = re.compile('(?:.?[<>=^])?[+\\- ]?z?#?0?(?P<width>\\d*)[,_]?(?:\\.(?P<precision>\\d*))?[,_]?[a-zA-Z%]?\\Z', re.DOTALL)
module-attribute
¶
Aggregate
¶
Bases: Transform
Reduce a Series or list to a single value.
count counts the values that are set, size every value including
missing ones (the rows of a column), and last takes the last value,
None if empty.
how = param.Selector(default='sum', objects=['sum', 'mean', 'min', 'max', 'count', 'size', 'nunique', 'last'])
class-attribute
instance-attribute
¶
label = 'Aggregate'
class-attribute
instance-attribute
¶
op = 'aggregate'
class-attribute
instance-attribute
¶
accepts(value)
classmethod
¶
apply(value)
¶
summary()
¶
Filter
¶
Bases: Transform
Keep the rows of a DataFrame whose column compares true against a value.
Without a value every row is kept, so a filter that was just added to an edge does not empty it before a value has been entered.
column = param.String(default='', doc='Column to compare.')
class-attribute
instance-attribute
¶
comparison = param.Selector(default='==', objects=list(_COMPARISONS))
class-attribute
instance-attribute
¶
label = 'Filter rows'
class-attribute
instance-attribute
¶
op = 'filter'
class-attribute
instance-attribute
¶
value = param.Parameter(default=None, doc="Value to compare against; a list for 'in'.")
class-attribute
instance-attribute
¶
accepts(value)
classmethod
¶
apply(value)
¶
summary()
¶
Format
¶
Bases: Transform
Format a value as a string with a format spec, prefix and suffix.
Only a format spec is accepted, not a template, because a template
such as {value.__class__} could reach attributes of the value.
label = 'Format'
class-attribute
instance-attribute
¶
op = 'format'
class-attribute
instance-attribute
¶
prefix = param.String(default='')
class-attribute
instance-attribute
¶
spec = FormatSpec(default='', doc="Format spec, for example ',.2f' or '.0%'.")
class-attribute
instance-attribute
¶
suffix = param.String(default='')
class-attribute
instance-attribute
¶
apply(value)
¶
FormatSpec
¶
Bases: String
A format spec whose width and precision are bounded.
Group
¶
Bases: Transform
Total a column of a DataFrame per value of another, largest first, e.g. capacity per state.
by = param.String(default='', doc='Column whose values become the rows.')
class-attribute
instance-attribute
¶
column = param.String(default='', doc='Column to total per row.')
class-attribute
instance-attribute
¶
how = param.Selector(default='sum', objects=['sum', 'mean', 'min', 'max', 'count', 'size'])
class-attribute
instance-attribute
¶
label = 'Group and total'
class-attribute
instance-attribute
¶
op = 'group'
class-attribute
instance-attribute
¶
top = param.Integer(default=0, bounds=(0, None), doc='Keep the largest this many; 0 keeps all.')
class-attribute
instance-attribute
¶
accepts(value)
classmethod
¶
apply(value)
¶
summary()
¶
Sort
¶
Bases: Transform
Sort a DataFrame by a column, or a Series or list by its values.
ascending = param.Boolean(default=True)
class-attribute
instance-attribute
¶
column = param.String(default=None, allow_None=True, doc='Column to sort a DataFrame by.')
class-attribute
instance-attribute
¶
label = 'Sort'
class-attribute
instance-attribute
¶
op = 'sort'
class-attribute
instance-attribute
¶
accepts(value)
classmethod
¶
apply(value)
¶
summary()
¶
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.
Unique
¶
_copy_on_write()
¶
_detached(value)
¶
Return a pandas value that edits to the original cannot reach, and vice versa.
Selecting a column or the first rows hands out a view of the input, and a
filter without a value hands out the input itself, so a consumer editing
the result in place would silently change the source and every other
consumer of it. Under copy-on-write (always on in pandas 3) a shallow copy
is already isolated and costs nothing until written to. Without it, on
pandas < 3, this is a deep copy, so every hop through such a transform
costs time and memory proportional to the size of the data; enable
pd.options.mode.copy_on_write to avoid that.
_is_frame(value)
¶
_is_number(value)
¶
_is_sequence(value)
¶
_scalar(value)
¶
Unwrap a numpy scalar, so downstream ports receive plain Python values.
apply_transform(steps, value)
¶
Run value through steps; an unset value stays unset.
available_transforms(value)
¶
Return the operations that can follow a step producing value.
is_failure(value)
¶
Whether value is a failure carried along an edge as a param.ReactiveError.
parse_transform(steps)
¶
Build the steps of a stored transform, raising ValueError if one is invalid.
summarize_transform(steps, max_length=40)
¶
Describe steps in a few words, or return None for an empty transform.