Skip to content

global_filters

panel_flowdash.global_filters

Global filters: a dashboard-level filter bar applied to every view that has the column.

A global filter names a column. While it has a value, every view the dashboard shows (a tile on the grid, or a component in the top bar or the sidebar) whose data has a column of that name keeps only the matching rows, without any wiring. A view gets the global filters and whatever its own inputs already filtered, e.g. by a Filter node feeding it; a view can opt out of a filter, and a view the filter cannot apply to says so ("Not filtered by State"). Sources and transforms, which have no tile, are never filtered, so a Filter node upstream of a view still sees all rows.

In the dashboard YAML the filters are a top-level list, views named as in nodes (the stored model keeps instance ids, like groups do)::

filters:
- column: t_state
  label: State
  widget: select
  value: [TX, OK]
- column: p_year
  widget: range
  value: [2010, 2020]
  exclude: [fleet_total]

widget is select (keep rows whose value is one of value) or range (keep rows between the two bounds of value, either of which may be null); left out, it follows the column's type. value is the filter's starting value, and exclude lists the views that opt out.

Taking part

A filter value is a :class:~panel_flowdash.selection.Predicate, so a component sees {column: predicate}. There are two ways to take part:

  • Do nothing. FlowDash filters the pandas DataFrames arriving at the inputs of a Viewer component itself, with :func:filter_frame, so every view reading a DataFrame takes part. Its columns are the columns of those frames.
  • Implement the protocol of :class:FilterTarget, e.g. to push the predicates into a SQL query instead of filtering rows in memory: __flowdash_columns__() returns the columns the component can filter on (None while it does not know yet), and __flowdash_apply_filters__(filters) receives the predicates that apply to it, by column, whenever they change (an empty mapping clears them). Its inputs are then left as they are. An optional __flowdash_column_values__(column) returns the values a select filter offers, or the (min, max) a range filter spans, for data FlowDash never sees in memory.

__flowdash_apply_filters__ may return the filters it could not apply, as {column: reason}, e.g. a column its query has but whose type the filter does not suit; the tile then says "Not filtered by ..." with the reason, as it does when it raises. A component whose columns change after it was placed, e.g. when a new query arrives at an input that is not a DataFrame, calls :func:request_filters to have them read again and its filters handed over anew.

Only the filters a component declares a column for reach it, and none it opted out of. Function components are not filtered: they read their inputs from the node directly.

EXCLUDED = 'excluded' module-attribute

EXTENSION_PREFIX = 'x-' module-attribute

FILTER_KEYS = ('column', 'label', 'widget', 'value', 'exclude') module-attribute

MAX_LABEL_LENGTH = 200 module-attribute

MAX_OPTIONS = 1000 module-attribute

MISMATCHED = 'mismatched' module-attribute

MISSING = 'missing' module-attribute

SKIPPED = 'skipped' module-attribute

SUMMARY = 'summary' module-attribute

WIDGETS = ('select', 'range') module-attribute

_HOLDS = {'number': 'numbers', 'datetime': 'dates', 'pydatetime': 'dates', 'date': 'dates', 'text': 'text'} module-attribute

FilterError

Bases: ValueError

A filter's data is malformed. key names the offending field, e.g. filters[1].value.

key = key instance-attribute

reason = reason instance-attribute

FilterMismatch

Bases: ValueError

A filter's values cannot be compared with a column of the same name, e.g. numbers with text.

FilterTarget

Bases: Protocol

A component that applies global filters itself, e.g. by pushing them into a query.

Predicate dataclass

Rows whose field lies in a range or is one of a set of values.

op is "range", with values the (start, end) bounds, either of which may be None, or "in", with values the values to keep. closed says which bounds of a range are included: "both", or "left" for start <= x < end.

closed = 'both' class-attribute instance-attribute

field instance-attribute

op instance-attribute

values instance-attribute

from_dict(data) classmethod

Return the predicate :meth:to_dict described.

mask(frame)

Return which rows of frame match.

Bounds and values are made comparable with the column: on a datetime column dates become midnights, and naive datetimes, which charts report in UTC, are converted to the column's timezone. A range on an unordered categorical column raises ValueError.

to_dict()

Return the predicate as a dict JSON can hold.

Dates and datetimes are written in ISO format, and types names the kind of each value that was one, e.g. [None, "datetime"].

_bound(value)

_check_value(value, widget, key)

_is_number(value)

_kind(series)

Return what series holds, as far as comparing it with a filter's values goes.

_looks_like_range(value)

_moment(value)

_scalar(value)

_scalar_value(value)

_span_kind(value)

_to_moment(value, kind)

_to_number(value)

_to_text(value)

accepts_filters(component)

Whether component applies global filters itself, see :class:FilterTarget.

check_filter(value, key='filter')

Return a copy of a filter without extension keys, raising :class:FilterError if malformed.

The views in exclude are checked for their form only: whether they exist is up to the caller.

check_filters(value)

Return a checked copy of a dashboard's filters, raising :class:FilterError if malformed.

check_value(value, widget, key='value')

Return a filter value as a list, raising :class:FilterError if it is not one widget takes.

coerce_predicate(p, series)

Return p with its values made comparable with series, or raise :class:FilterMismatch.

Columns are matched by name only, so two views can hold a column of the same name with different types. Values are converted where that is safe: ISO strings to dates for a column of dates, numeric strings to numbers for a column of numbers, numbers to strings for a column of text. A range on text or on unordered categories has nothing to compare and is refused.

column_options(frames, column)

Return the distinct values of column across frames, sorted, as plain values.

column_span(frames, column)

Return the smallest and largest value of column across frames, or None if it has none.

Frames whose column cannot be compared with the others' are left out, see :func:comparable_span.

comparable_span(spans)

Return the overall span of spans, keeping those of the kind most of them have.

Spans of numbers and of dates cannot be compared with each other, nor those of text with anything a range slider shows, so the others are dropped.

decode_filters(value)

Return the filters a stored dashboard holds, empty if none; ValueError when malformed.

filter_frame(frame, predicates)

Return the rows of frame matching every predicate on a column it has.

The reference implementation of global filters, applied to the DataFrame inputs of components that do not filter themselves. Anything that is not a DataFrame, and a frame without any of the columns, is returned as is, and a predicate whose values cannot be compared with its column is skipped, see :func:filter_frame_checked.

filter_frame_checked(frame, predicates)

Return the rows of frame matching every predicate it can apply, and why it could not apply the others.

Like :func:filter_frame, and the reasons are by column, e.g. {"year": "holds numbers, and 'n/a' is not one"}. A predicate that cannot be compared with its column is left out rather than failing the whole frame.

filter_label(entry)

Return the name a filter is shown by: its label, else its column.

frame_columns(values)

Return the columns of the DataFrames among values.

hint_detail(filters, skipped, mismatched=None, reported=None)

Return why each filter in skipped does not reach a view, for its tooltip.

hint_text(filters, skipped)

Return the line a view shows for the active filters that do not reach it, e.g. Not filtered by State.

map_filters(filters, nodes)

Return filters with the views they exclude renamed by nodes, dropping views not in it.

plain_value(value)

Return a column value as JSON can hold it: dates and datetimes as ISO strings.

predicate(entry, value, widget=None)

Return the predicate a filter with value applies, or None while it keeps every row.

A select keeps the rows whose column is one of the values; an empty selection keeps them all. A range keeps the rows between its bounds; ISO date strings are read as datetimes, and a range without either bound keeps every row.

request_filters(component)

Have the dashboards showing component read its columns again and hand it its filters anew.

A :class:FilterTarget calls this when __flowdash_columns__ would now answer differently, e.g. once a query arriving at one of its inputs has run. Returns whether a dashboard shows component.

resolve_view(filters, active, node, columns, mismatched=None, reported=None, summary=False)

Return the predicates that reach the view node, and why each other active filter does not.

active are the predicates of the filters with a value, by column, and columns the view's columns, None while unknown. A filter the view opts out of is excluded, one on a column it lacks is missing, and one whose values its column cannot be compared with, as mismatched says by column, is mismatched. One the view itself could not apply, as reported says by column, is skipped. Every filter a summary view, one showing numbers worked out upstream, does not opt out of is summary.

skipped_filters(result, filters)

Return what __flowdash_apply_filters__ reported it skipped, by column, as text.

result is a mapping {column: reason} or an iterable of (column, reason) pairs; columns not among filters, the ones handed over, are dropped.

widget_for(entry, sample=None)

Return a filter's widget: its own, else range for numbers and dates, else select.

without_nodes(filters, instance_ids)

Return filters no longer excluding the nodes instance_ids.