Package: json2table
This is a simple Python package that allows a JSON object to be converted to HTML. It provides aconvertfunction that accepts adictinstance and returns a string of converted HTML. For example, the simple JSON object{"key" : "value"}can be converted to HTML via:>>>fromjson2tableimportconvert>>>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 resemblekeyvalueMore complex parsing is also possible. If a list ofdict’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:menumenuitemonclickvalueCreateNewDoc()NewOpenDoc()OpenCloseDoc()CloseidfilevalueFileIt 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 thebuild_directionto"TOP_TO_BOTTOM"yields:menumenuitemidvalueonclickvaluefileFileCreateNewDoc()NewOpenDoc()OpenCloseDoc()CloseTable attributes are added via thetable_attributesparameter. This parameter should be adictof(key, value)pairs to apply to the table in the formkey="value". If in our simple example before we additionally wanted to apply a class attribute of"tabletable-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)DetailsThis module provides a singleconvertfunction. It takes as input the JSON object (represented as a Pythondict) 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)Parametersjson_input : dictJSON object to convert into HTML.build_direction :{"TOP_TO_BOTTOM", "LEFT_TO_RIGHT"}, optionalString 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, optionalDictionary of(key, value)pairs describing attributes to add to the table.
Each attribute is added according to the templatekey="value". For example,
the table{ "border" : 1 }modifies the generated table tags to includeborder="1"as an attribute. The generated opening tag would look like<tableborder="1">. Default isNone.ReturnsstrString of converted HTML.InstallationThe easiest method on installation is to usepip. Simply run:>>> pip install json2tableIf instead the repo was cloned, navigate to the root directory of thejson2tablepackage from the command line and execute:>>> python setup.py installTestsIn order to verify the code is working, from the command line navigate to thejson2tableroot directory and run:>>> python -m unittest tests.test_json2table