All Projects → latture → json2table

latture / json2table

Licence: MIT license
Converts JSON to an HTML table

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to json2table

Nn Transfer
Convert trained PyTorch models to Keras, and the other way around
Stars: ✭ 205 (+294.23%)
Mutual labels:  converter
Mpv Webm
Simple WebM maker for mpv, with no external dependencies.
Stars: ✭ 234 (+350%)
Mutual labels:  converter
csv2vcf
🔧 Simple script in python to convert CSV files to VCF
Stars: ✭ 66 (+26.92%)
Mutual labels:  converter
Dataset Serialize
JSON to DataSet and DataSet to JSON converter for Delphi and Lazarus (FPC)
Stars: ✭ 213 (+309.62%)
Mutual labels:  converter
Kepubify
Fast, standalone EPUB to KEPUB converter CLI app / library (and a few other utilities).
Stars: ✭ 225 (+332.69%)
Mutual labels:  converter
cpc
Text calculator with support for units and conversion
Stars: ✭ 89 (+71.15%)
Mutual labels:  converter
Currency Converter Swift3.0 Viper
Calculates money quick and easy way to see live foreign exchange rates (Based on swift 4.2, viper architecture and special thanks to https://github.com/hakanensari/fixer-io for conversion API)
Stars: ✭ 198 (+280.77%)
Mutual labels:  converter
slackdown
A simple Slack message text formatting to HTML code converter.
Stars: ✭ 27 (-48.08%)
Mutual labels:  converter
Bigbash
A converter that generates a bash one-liner from an SQL Select query (no DB necessary)
Stars: ✭ 230 (+342.31%)
Mutual labels:  converter
guepard
flash to html5 converter, as3 to javascript translator
Stars: ✭ 58 (+11.54%)
Mutual labels:  converter
Jpsxdec
jPSXdec: cross-platform PlayStation 1 audio and video converter
Stars: ✭ 219 (+321.15%)
Mutual labels:  converter
Retrofit Logansquare
A Converter implementation using LoganSquare JSON serialization for Retrofit 2.
Stars: ✭ 224 (+330.77%)
Mutual labels:  converter
mdconv
A tool to convert markdown to html.
Stars: ✭ 38 (-26.92%)
Mutual labels:  converter
Htmr
Simple and lightweight (< 2kB) HTML string to React element conversion library
Stars: ✭ 214 (+311.54%)
Mutual labels:  converter
java-xml-to-json
👾 convert XML to a structure-preserving JSON representation
Stars: ✭ 15 (-71.15%)
Mutual labels:  converter
Jsonsubtypes
Discriminated Json Subtypes Converter implementation for .NET
Stars: ✭ 201 (+286.54%)
Mutual labels:  converter
BatchEncoder
BatchEncoder is an audio files conversion software.
Stars: ✭ 145 (+178.85%)
Mutual labels:  converter
fp-units
An FP-oriented library to easily convert CSS units.
Stars: ✭ 18 (-65.38%)
Mutual labels:  converter
Audiblex
Audible audio book converter which actually works!
Stars: ✭ 26 (-50%)
Mutual labels:  converter
objectify-css
CLI for converting CSS rules to JavaScript style objects
Stars: ✭ 46 (-11.54%)
Mutual labels:  converter

json2table

Build Status Coverage Status

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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].