readme.md

The library for generating html (vertical) tables

This library is necessary for creating html tables from prepared data contained in json formats, directories, arrays, and structural nodes (an additional library is more-structural-nodes). The library allows you to perform the following operations: 1. Group data by parameter value; 2. Sort data by parameter; 3. Output parameter names; 4. Output parameter values; 5. Output property values (for BaseStructuralNode); 6. Set auto-increment cells; 7. Set a dictionary of names (for parameter conversion); 8. Set headlines; 9. Set table properties;

Template formation rules

At the very top of the hierarchy is a table. The table is not allocated in a separate block. The next level is multiblock. Combines an array of blocks. For example, there may be a header block and a data block. In fact, the number of blocks is not limited.

"multiblock": {}

There is an array of blocks inside the multi block.

"multiblock": { "block_drivers": [] }

The block can be one of the following types (“type”): 1. object - incoming data in the block is perceived as an object. This type is necessary for non-duplicate data. 2. array - incoming data is an array. Accordingly, corresponding cells will be created for each element of the array. 3. array_by_object - all parameters will be extracted from the incoming object and processed as an array. This can be useful if the data is decomposed as parameters rather than an array.

The block consists of lines.

"rows": []

The strings themselves consist of containers.

"containers": []

A container either describes the behavior for displaying data, or allows you to output a more complex data structure, starting with a multiblock. The nesting level is unlimited.

An example of a container that allows you to output a multiblock:

[
    {
        "type": "multiblock",
        "source": "multi_test",
        "multiblock": {
            "block_drivers": [
                {
                    "type": "array",
                    "rows": [
                        {
                            "containers": [
                                {
                                        "type": "value",
                                        "source": "a"
                                },
                                {
                                        "type": "value",
                                        "source": "b"
                                }
                             ]
                        }
                    ]
                }
            ]
        }
    }
]

To reduce the nesting, it is allowed to specify the name of the multiblock, in accordance with the example:

[
    {
        "type": "multiblock",
        "multiblock": "arrays_cena"
    }
]

The “multiblocks” section in the root directory is used to describe the multiblock.:

"multiblocks": {
    "{multiblock name}": {
        "block_drivers": [
            {
            ...
            }
        ]
    }
}

The following options are available to specify the filling of the cell data.

Filling in the attribute name:

{
    "type": "name"
}

Filling in with a constant:

{
    "type": "constant",
    "value": "{value}"
}

Filling in an attribute value along an arbitrary path relative to the incoming object:

{
    "type": "value",
    "source": "{attribute path relative to the incoming object}"
}

Filling in the attribute value:

{
    "type": "value"
}

Filling in the auto-increment value:

{
    "type": "auto-increment"
}

Filling in the node property value:

{
    "type": "parameter",
    "source": "parameter name"
}

Example

Template

{
    "parameters": {
        "table": {
            "border": "2"
        }
    },
    "dictionaries": {
        "main": {
            "True": "Firm and clear",
            "description": "Amazing narrative",
            "value": "Meaningful",
            "visible": "Look",
            "victory": "Live!!!" 
        }
    },
    "multiblock": {
        "block_drivers": [
            {
                "type": "object",
                "rows": [
                    {
                        "containers": [
                            {
                                "type": "constant",
                                "value": "Parameter"
                            },
                            {
                                "type": "constant",
                                "value": "Name"
                            },
                            {
                                "type": "constant",
                                "value": "Value"
                            }
                        ]
                    }
                ]
            },
            {
                "type": "array",
                "rows": [
                    {
                        "containers": [
                            {
                                "type": "value",
                                "source": "name"
                            },
                            {
                                "type": "multiblock",
                                "multiblock": "info"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    "multiblocks": {
        "info": {
            "block_drivers": [
                {
                    "source": "info",
                    "type": "object_as_array",
                    "rows": [
                        {
                            "containers": [
                                {
                                    "type": "name",
                                    "dictionary": "main"
                                },
                                {
                                    "type": "value",
                                    "dictionary": "main"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    }
}

Data

[
    {
        "name": "First",
        "info": {
            "description": "first one",
            "value": 3,
            "visible": true
        }
    },
    {
        "name": "Second",
        "info": {
            "description": "Difficult second",
            "value": "Something smart",
            "visible": true,
            "victory": "Yes"
        }
    }
]

Result

Parameter
Name
Value
The first
Amazing narration
Just the first
Significant value
3
Visibility
Firmly and clearly
The second
Amazing narration
The second one is difficult
Significant value
Something smart
Visibility
Firmly and clearly
Vivat!!!
Yes

Usage

If you need to create only one table using a specific template, you can use the create_table function.

Example

from html_table_builder import create_table
import json

template_file_name = "template.json"
table_file_name = "test_table.json"
result_file_name = "result.html"

if __name__ == "__main__":
    with open(template_file_name, mode="r", encoding="utf-8") as f:
        template = json.load(f)
    with open(table_file_name, mode="r", encoding="utf-8") as f:
        table_json = json.load(f)
    result = create_table(table_json, template)
    with open(result_file_name, mode="w", encoding="utf-8") as f:
        f.write(result)

If you need to create several tables with different data sources for the same template, it is better to create a table generator object and create tables through it.

Example

from html_table_builder import TableGenerator
import json


if __name__ == "__main__":
    with open(template_file_name, mode="r", encoding="utf-8") as f:
        template = json.load(f)
    table_generator = TableGenerator(template)
    with open(table_file_name, mode="r", encoding="utf-8") as f:
        table_json = json.load(f)
    result = table_generator.create_table(table_json)
    with open(result_file_name, mode="w", encoding="utf-8") as f:
        f.write(result)

About data conversion for table cells

An object of the converter class inherited from the ConverterInterface interface is used to convert data from the source into data from table cells. A method must be implemented inside the interface.:

  • def convert(self, base_value: Any) -> str

The converter object is passed by the last (optional) parameter in the TableGenerator constructor: TableGenerator(template, new_converter). Or when using the function: create_table(table_json, template, new_converter). If you don’t pass your own object, then use your own one, which converts the received value to a string and wraps it in a div tag.

Описание
Данная библиотека неоходима для создания html таблиц из подготовленных данных, содержащихся в форматах json, справочники, массивы, структурных узлов (дополнительная библиотека more-structural-nodes)
Конвейеры
0 успешных
0 с ошибкой
Разработчики