kiara.operations.pretty_print¶
PrettyPrintModuleConfig
pydantic-model
¶
PrettyPrintOperationType
¶
This operation type renders values of any type into human readable output for different targets (e.g. terminal, html, ...).
The main purpose of this operation type is to show the user as much content of the data as possible for the specific rendering target, without giving any guarantees that all information contained in the data is shown. It may print out the whole content (if the content is small, or the available space large enough), or just a few bits and pieces from the start and/or end of the data, whatever makes most sense for the data itself. For example, for large tables it might be a good idea to print out the first and last 30 rows, so users can see which columns are available, and get a rough overview of the content itself. For network graphs it might make sense to print out some graph properties (number of nodes, edges, available attributes, ...) on the terminal, but render the graph itself when using html as target.
Currently, it is only possible to implement a pretty_print
renderer if you have access to the source code of the value
type you want to render. To add a new render method, add a new instance method to the ValueType
class in question,
in the format:
def pretty_print_as_<TARGET_TYPE>(value: Value, print_config: typing.Mapping[str, typing.Any]) -> typing.Any:
...
...
kiara will look at all available ValueType
classes for methods that match this signature, and auto-generate
operations following this naming template: <SOURCE_TYPE>.pretty_print_as.<TARGET_TYPE>
.
Currently, only the type renderables
is implemented as target type (for printing out to the terminal). It is planned to
add output suitable for use in Jupyter notebooks in the near future.
PrettyPrintValueModule
¶
create_input_schema(self)
¶
Abstract method to implement by child classes, returns a description of the input schema of this module.
If returning a dictionary of dictionaries, the format of the return value is as follows (items with '*' are optional):
{
"[input_field_name]: {
"type": "[value_type]",
"doc*": "[a description of this input]",
"optional*': [boolean whether this input is optional or required (defaults to 'False')]
"[other_input_field_name]: {
"type: ...
...
}
Source code in kiara/operations/pretty_print.py
def create_input_schema(
self,
) -> typing.Mapping[
str, typing.Union[ValueSchema, typing.Mapping[str, typing.Any]]
]:
value_type = self.get_config_value("value_type")
if value_type == "all":
input_name = "value_item"
doc = "A value of any type."
else:
input_name = value_type
doc = f"A value of type '{value_type}'."
inputs: typing.Mapping[str, typing.Any] = {
input_name: {"type": value_type, "doc": doc},
"print_config": {
"type": "dict",
"doc": "Optional print configuration.",
"optional": True,
},
}
return inputs
create_output_schema(self)
¶
Abstract method to implement by child classes, returns a description of the output schema of this module.
If returning a dictionary of dictionaries, the format of the return value is as follows (items with '*' are optional):
{
"[output_field_name]: {
"type": "[value_type]",
"doc*": "[a description of this output]"
"[other_input_field_name]: {
"type: ...
...
}
Source code in kiara/operations/pretty_print.py
def create_output_schema(
self,
) -> typing.Mapping[
str, typing.Union[ValueSchema, typing.Mapping[str, typing.Any]]
]:
outputs: typing.Mapping[str, typing.Any] = {
"printed": {
"type": self.get_config_value("target_type"),
"doc": f"The printed value as '{self.get_config_value('target_type')}'.",
}
}
return outputs
retrieve_module_profiles(kiara)
classmethod
¶
Retrieve a collection of profiles (pre-set module configs) for this kiara module type.
This is used to automatically create generally useful operations (incl. their ids).
Source code in kiara/operations/pretty_print.py
@classmethod
def retrieve_module_profiles(
cls, kiara: Kiara
) -> typing.Mapping[str, typing.Union[typing.Mapping[str, typing.Any], Operation]]:
all_metadata_profiles: typing.Dict[
str, typing.Dict[str, typing.Dict[str, typing.Any]]
] = {}
for value_type_name, value_type_cls in kiara.type_mgmt.value_types.items():
for attr in dir(value_type_cls):
if not attr.startswith("pretty_print_as_"):
continue
target_type = attr[16:]
if target_type not in kiara.type_mgmt.value_type_names:
log_message(
f"Pretty print target type '{target_type}' for source type '{value_type_name}' not valid, ignoring."
)
continue
op_config = {
"module_type": cls._module_type_id, # type: ignore
"module_config": {
"value_type": value_type_name,
"target_type": target_type,
},
"doc": f"Pretty print a value of type '{value_type_name}' as '{target_type}'.",
}
all_metadata_profiles[
f"pretty_print.{value_type_name}.as.{target_type}"
] = op_config
return all_metadata_profiles