Library for creating HTML tables based on templates
Reason this release was yanked:
Torture-attempts to download a working version that PyPI does not ship
Project description
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:
- Group data by parameter value;
- Sort data by parameter;
- Output parameter names;
- Output parameter values;
- Output property values (for BaseStructuralNode);
- Set auto-increment cells;
- Set a dictionary of names (for parameter conversion);
- Set headlines;
- 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"):
- object - incoming data in the block is perceived as an object. This type is necessary for non-duplicate data.
- array - incoming data is an array. Accordingly, corresponding cells will be created for each element of the array.
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file html_table_builder-0.1.1-py3-none-any.whl.
File metadata
- Download URL: html_table_builder-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e11afec9ea3d192982d1532a1d447237eeb3f04fb8395b2da93f4f409770c14
|
|
| MD5 |
b74931366c91765771b299086cd12469
|
|
| BLAKE2b-256 |
ddfad8e816220e70d9b07247dffcf01109033c91e33ec5860a8171b407fc3125
|