Skip to content

spec_settings

panel_flowdash.spec_settings

Curated settings bound to paths in a spec, so a form and the spec's text edit it together.

A component whose config holds a spec, e.g. a Vega-Lite chart, lists the settings worth a form control, each a path into the spec::

spec = param.Dict(
    default={},
    metadata={"flowdash": {"widget": "spec", "settings": [
        SpecSetting("title", label="Title", group="Labels"),
        SpecSetting("mark.type", label="Mark", widget="radio", options=["bar", "line"]),
        SpecSetting("encoding.x.field", label="X", choices="columns", of="pipeline"),
    ]}},
)

The inspector then edits the spec as a form over those settings beside the spec's JSON. Changing a control writes its path into the spec, and editing the text reads every path back into the form. Keys no setting covers are kept exactly as written and reported by :func:unknown_paths, so the form never drops what it cannot show. A setting whose value in the spec its control cannot show, e.g. a title given as an object, is left alone too (:meth:SpecSetting.fits).

Writing a setting's default removes its path, along with the objects that held only it, so a spec says no more than it has to.

Path = tuple[str, ...] module-attribute

SETTING_WIDGETS = ('text', 'select', 'radio', 'switch', 'integer', 'number') module-attribute

SPEC_WIDGET = 'spec' module-attribute

_MISSING = object() module-attribute

SpecPathError

Bases: ValueError

A path cannot be written because a value on the way to it is not an object.

SpecSetting dataclass

One setting of a spec that a form shows as a control bound to a path.

Parameters:

Name Type Description Default
path str | tuple[str, ...]

Where the setting lives in the spec, dotted ("encoding.x.title") or as a sequence of keys.

required
label str | None

The control's label; defaults to the last key of the path.

None
widget str

The control: "text", "select", "radio" (a row of buttons), "switch", "integer" or "number".

'text'
options list[Any] | dict[str, Any] | None

The values a select or radio offers, a list or a {label: value} dict.

None
group str | None

The heading the control is listed under.

None
default Any

What the spec means when it leaves the path out. The control shows it then, and setting it removes the path.

None
doc str | None

A sentence shown as the control's tooltip.

None
choices str | None

choices="columns" offers the columns of the node's input of, like a config param's choices.

None
of str | None

choices="columns" offers the columns of the node's input of, like a config param's choices.

None
get Callable[[dict], Any] | None

For a setting that is not one path: get(spec) returns its value and set(spec, value) writes it into a copy of the spec in place.

None
set Callable[[dict], Any] | None

For a setting that is not one path: get(spec) returns its value and set(spec, value) writes it into a copy of the spec in place.

None
covers tuple[str | tuple[str, ...], ...]

Further paths the setting controls, e.g. those its get reads. Keys under a covered path are not reported as unknown.

()
enabled Callable[[dict], bool | str] | None

enabled(spec) says whether the control can be used, e.g. a width only while the size is fixed. Returning a sentence instead of False disables it and shows the sentence under the form.

None
restrict bool

Whether a select or radio accepts only its options.

True

choices = None class-attribute instance-attribute

covers = () class-attribute instance-attribute

default = None class-attribute instance-attribute

doc = None class-attribute instance-attribute

enabled = field(default=None, compare=False) class-attribute instance-attribute

get = field(default=None, compare=False) class-attribute instance-attribute

group = None class-attribute instance-attribute

key property

The setting's dotted path, which names it, e.g. in choices.

label = None class-attribute instance-attribute

of = None class-attribute instance-attribute

options = None class-attribute instance-attribute

path instance-attribute

paths property

The paths the setting controls.

restrict = True class-attribute instance-attribute

set = field(default=None, compare=False) class-attribute instance-attribute

title property

widget = 'text' class-attribute instance-attribute

accepts(value)

Whether value fits the control and, for a restricted select or radio, its options.

disabled_reason(spec)

Return why the control cannot be used for spec, or None if it can.

enabled may return a sentence saying why instead of False.

fits(value)

Whether the control can show value without changing it.

A select or radio shows any plain value, one outside its options as an extra option.

is_enabled(spec)

labelled()

Return the options as {label: value}.

read(spec)

Return the setting's value in spec, its default if the spec leaves it out.

write(spec, value)

Return a copy of spec with the setting set to value.

Everything else is kept as it was, in its order. Setting the default removes the path.

Raises:

Type Description
SpecPathError

If the path runs through a value that is not an object.

SpecTextError

Bases: ValueError

Spec text that is not a JSON object or YAML mapping, with the line and column (from 1) at fault.

column = column instance-attribute

line = line instance-attribute

_as_json(spec)

Return a copy of spec holding JSON values only, as :func:format_spec writes them.

_covered(path, covered)

_dump(value)

_leads_to(path, covered)

_merged(node, value, shared)

Return value written into the YAML node, keeping the comments of what stays.

A node is changed in place, unless shared says other paths reach it, through an alias or a merge key: then a copy is, so only this path changes. A value that did not change keeps its node, alias and all.

_plain(value)

Return what YAML read as plain JSON values: dicts, lists, strings, numbers, booleans and None.

A date, which YAML reads from an unquoted 2024-01-31, stays the string it was written as.

_same(a, b)

Whether two plain values are the same as JSON, so 1 and True differ and key order counts.

_shared(doc)

Return the ids of the YAML nodes doc reaches more than once, through an alias or a merge key.

_yaml()

Return a YAML 1.2 round-trip reader and writer, so on or no stay strings and comments are kept.

as_path(path)

Return path, dotted like "encoding.x.title" or a sequence of keys, as a tuple of keys.

as_setting(value)

Return value, a :class:SpecSetting or a dict of its fields, as a SpecSetting.

changed_paths(old, new, prefix=())

Return the paths whose value differs from old to new, innermost, in new's order.

Removed keys are not listed, since they have no place in new.

declared_settings(parameter)

Return the settings a param's FlowDash metadata declares for a spec form, or None.

A param.Dict with metadata={"flowdash": {"widget": "spec", "settings": [...]}} declares them.

delete_path(spec, path)

Remove path from spec in place, and the objects on the way it leaves empty.

format_spec(spec, indent=2)

Return spec as JSON text, as json.dumps(spec, indent=indent) writes it, and its lines.

The lines map each path to the first and last line (from 0) its key and value take, so a form can point at the lines a setting changed.

format_spec_yaml(spec, doc=None)

Return spec as YAML 1.2 text, its lines per path, and the document written.

With the doc of earlier text, see :func:parse_spec_yaml, the spec is written into it, so the comments and layout of the keys that stay are kept. An empty spec is blank text.

get_path(spec, path, default=None)

Return the value at path in spec, or default if a key on the way is missing.

has_path(spec, path)

Whether spec holds a value at path.

parse_spec(text)

Return the spec JSON text holds, {} for blank text.

Raises:

Type Description
SpecTextError

Saying where the text is not valid JSON, or that it is not an object.

parse_spec_yaml(text)

Return the spec YAML 1.2 text holds, {} for blank text, and the document it was read from.

The document keeps the text's comments and layout, see :func:format_spec_yaml.

Raises:

Type Description
SpecTextError

Saying where the text is not valid YAML, or that it is not a mapping.

path_text(path)

Return path dotted, as it is reported to people.

problems(spec, settings)

Return what is wrong with the values of spec at restricted settings, for a config check.

Only a select or radio restricted to its options is checked, since a value the other controls cannot show may still be valid in the spec.

set_path(spec, path, value)

Set path in spec to value in place, adding the objects on the way.

A key that exists keeps its place among its siblings.

Raises:

Type Description
SpecPathError

If a value on the way is not an object, e.g. "mark": "line" when setting mark.point; it is not replaced.

shown_in_spec(spec, settings)

Return the settings whose value in spec their control cannot show, kept as written.

text_spans(text)

Return the first and last line (from 0) each path takes in spec text, JSON or YAML as written.

A path ends on the line before the next key or item at its level, so comments after it count as its own, leaving out closing brackets and blank lines. Text that does not parse has no lines.

unknown_paths(spec, settings)

Return the paths of spec no setting covers, outermost first, in the spec's order.

An object holding both covered and other keys is looked into, so encoding.x.axis is reported for an encoding.x whose field a setting covers. A plain value where a setting expects an object, such as Vega-Lite's "mark": "bar" for a setting of mark.type, is that setting's shorthand and not reported.

unrepresented_paths(spec, settings)

Return the paths of spec the form cannot show, which are kept as written.

Those are the paths no setting covers and those holding a value their setting's control cannot show, e.g. a list of colours where a scheme is picked.