json2table
This is a simple Python package that allows a JSON object to be converted to HTML. It provides a convert function that accepts a dict instance and returns a string of converted HTML. For example, the simple JSON object {"key" : "value"} can be converted to HTML via:
>>> from json2table import convert
>>> json_object = {"key" : "value"}
>>> build_direction = "LEFT_TO_RIGHT"
>>> table_attributes = {"style" : "width:100%"}
>>> html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes)
>>> print(html)
'<table style="width:100%"><tr><th>key</th><td>value</td></tr></table>'
The resulting table will resemble
key |
value |
More complex parsing is also possible. If a list of dict’s provides the same list of keys, the generated HTML with gather items by key and display them in the same column.
{"menu": {
"id": "file",
"value": "File",
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}
Output:
menu |
menuitem |
onclick |
value |
CreateNewDoc() |
New |
||
OpenDoc() |
Open |
||
CloseDoc() |
Close |
||
id |
file |
||
value |
File |
||
It might, however, be more readable if we were able to build the table from top-to-bottom instead of the default left-to-right. Changing the build_direction to "TOP_TO_BOTTOM" yields:
menu |
|||
menuitem |
id |
value |
|
onclick |
value |
file |
File |
CreateNewDoc() |
New |
||
OpenDoc() |
Open |
||
CloseDoc() |
Close |
||
Table attributes are added via the table_attributes parameter. This parameter should be a dict of (key, value) pairs to apply to the table in the form key="value". If in our simple example before we additionally wanted to apply a class attribute of "table table-striped" we would use the following:
>>> table_attributes = {"style" : "width:100%", "class" : "table table-striped"}
and convert just as before:
>>> html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes)
Details
This module provides a single convert function. It takes as input the JSON object (represented as a Python dict) and, optionally, a build direction and a dictionary of table attributes to customize the generated table:
convert(json_input, build_direction="LEFT_TO_RIGHT", table_attributes=None)
Parameters
json_input : dict
JSON object to convert into HTML.
build_direction : {"TOP_TO_BOTTOM", "LEFT_TO_RIGHT"}, optional
String denoting the build direction of the table. If "TOP_TO_BOTTOM" child objects will be appended below parents, i.e. in the subsequent row. If "LEFT_TO_RIGHT" child objects will be appended to the right of parents, i.e. in the subsequent column. Default is "LEFT_TO_RIGHT".
table_attributes : dict, optional
Dictionary of (key, value) pairs describing attributes to add to the table. Each attribute is added according to the template key="value". For example, the table { "border" : 1 } modifies the generated table tags to include border="1" as an attribute. The generated opening tag would look like <table border="1">. Default is None.
Returns
str
String of converted HTML.
Installation
The easiest method on installation is to use pip. Simply run:
>>> pip install json2table
If instead the repo was cloned, navigate to the root directory of the json2table package from the command line and execute:
>>> python setup.py install
Tests
In order to verify the code is working, from the command line navigate to the json2table root directory and run:
>>> python -m unittest tests.test_json2table
Metadata
Release files for json2table 1.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json2table-1.1.5.tar.gz | 6.3 kB | Without provenance | Details for json2table-1.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json2table-1.1.5-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for json2table-1.1.5-py2.py3-none-any.whl |
Total release size: 15.0 kB
Release files / json2table-1.1.5.tar.gz
| Download URL | json2table-1.1.5.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
838e4c052276252fec7b0dbd10668258ce0997605888a250307bd6b124ae6d0a
|
|
BLAKE2b-256 checksum How to use checksums |
fef605039bd75d6ec8a07135bf97d6d11e92ab51e2939cd065bb6f7d0be46de5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / json2table-1.1.5-py2.py3-none-any.whl
| Download URL | json2table-1.1.5-py2.py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
d84eac9471f404f17c9ec1ca5935d7088917f669b3ba5cc5a027f865e7896356
|
|
BLAKE2b-256 checksum How to use checksums |
c02093b122d0daec1cc728b6e6dde860615e81c2bf03fb05cad9e3c1899b3c11
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for json2table 1.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.