json2table


Namejson2table JSON
Version 1.1.5 PyPI version JSON
download
home_pagehttps://github.com/latture/json2table
SummaryConvert JSON to an HTML table
upload_time2016-10-21 22:36:28
maintainerNone
docs_urlNone
authorRyan Latture
requires_pythonNone
licenseMIT
keywords json html convert table
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            json2table
==========

|Build Status| |Coverage Status|

.. |Build Status| image:: https://travis-ci.org/latture/json2table.svg?branch=master
   :target: https://travis-ci.org/latture/json2table
.. |Coverage Status| image:: https://coveralls.io/repos/github/latture/json2table/badge.svg?branch=master
   :target: https://coveralls.io/github/latture/json2table?branch=master

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:

.. code:: python

    >>> 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.

.. code:: json

    {"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:

.. code:: python

    >>> table_attributes = {"style" : "width:100%", "class" : "table table-striped"}

and convert just as before:

.. code:: python

    >>> 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
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/latture/json2table",
    "name": "json2table",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "json,HTML,convert,table",
    "author": "Ryan Latture",
    "author_email": "ryan.latture@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fe/f6/05039bd75d6ec8a07135bf97d6d11e92ab51e2939cd065bb6f7d0be46de5/json2table-1.1.5.tar.gz",
    "platform": "UNKNOWN",
    "description": "json2table\n==========\n\n|Build Status| |Coverage Status|\n\n.. |Build Status| image:: https://travis-ci.org/latture/json2table.svg?branch=master\n   :target: https://travis-ci.org/latture/json2table\n.. |Coverage Status| image:: https://coveralls.io/repos/github/latture/json2table/badge.svg?branch=master\n   :target: https://coveralls.io/github/latture/json2table?branch=master\n\nThis 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:\n\n.. code:: python\n\n    >>> from json2table import convert\n    >>> json_object = {\"key\" : \"value\"}\n    >>> build_direction = \"LEFT_TO_RIGHT\"\n    >>> table_attributes = {\"style\" : \"width:100%\"}\n    >>> html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes)\n    >>> print(html)\n    '<table style=\"width:100%\"><tr><th>key</th><td>value</td></tr></table>'\n\nThe resulting table will resemble\n\n+---------+-------+\n| **key** | value |\n+---------+-------+\n\nMore 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.\n\n.. code:: json\n\n    {\"menu\": {\n      \"id\": \"file\",\n      \"value\": \"File\",\n        \"menuitem\": [\n          {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},\n          {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\n          {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\n        ]\n      }\n    }\n\nOutput:\n\n+----------+--------------+----------------+-----------+\n| **menu** | **menuitem** | **onclick**    | **value** |\n+          +              +----------------+-----------+\n|          |              | CreateNewDoc() | New       |\n+          +              +----------------+-----------+\n|          |              | OpenDoc()      | Open      |\n+          +              +----------------+-----------+\n|          |              | CloseDoc()     | Close     |\n+          +--------------+----------------+-----------+\n|          | **id**       | file                       |\n+          +--------------+----------------+-----------+\n|          | **value**    | File                       |\n+----------+--------------+----------------+-----------+\n\nIt 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:\n\n+----------------+-----------+-------+-----------+\n| **menu**                                       |\n+----------------+-----------+-------+-----------+\n| **menuitem**               | **id**| **value** |\n+----------------+-----------+-------+-----------+\n| **onclick**    | **value** |  file |   File    |\n+----------------+-----------+       +           +\n| CreateNewDoc() | New       |       |           | \n+----------------+-----------+       +           +\n| OpenDoc()      | Open      |       |           |\n+----------------+-----------+       +           +\n| CloseDoc()     | Close     |       |           |\n+----------------+-----------+-------+-----------+\n\nTable 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:\n\n.. code:: python\n\n    >>> table_attributes = {\"style\" : \"width:100%\", \"class\" : \"table table-striped\"}\n\nand convert just as before:\n\n.. code:: python\n\n    >>> html = convert(json_object, build_direction=build_direction, table_attributes=table_attributes)\n\nDetails\n-------\nThis 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: \n\n``convert(json_input, build_direction=\"LEFT_TO_RIGHT\", table_attributes=None)``\n\n**Parameters**\n\njson_input : dict\n\n  JSON object to convert into HTML.\n\nbuild_direction : ``{\"TOP_TO_BOTTOM\", \"LEFT_TO_RIGHT\"}``, optional\n\n  String denoting the build direction of the table. If ``\"TOP_TO_BOTTOM\"`` child\n  objects will be appended below parents, i.e. in the subsequent row. If ``\"LEFT_TO_RIGHT\"``\n  child objects will be appended to the right of parents, i.e. in the subsequent column.\n  Default is ``\"LEFT_TO_RIGHT\"``.\n\ntable_attributes : ``dict``, optional\n\n  Dictionary of ``(key, value)`` pairs describing attributes to add to the table. \n  Each attribute is added according to the template ``key=\"value\"``. For example, \n  the table ``{ \"border\" : 1 }`` modifies the generated table tags to include \n  ``border=\"1\"`` as an attribute. The generated opening tag would look like \n  ``<table border=\"1\">``. Default is ``None``.\n\n**Returns**\n\n``str``\n\n  String of converted HTML.\n\nInstallation\n------------\nThe easiest method on installation is to use ``pip``. Simply run:\n\n::\n\n    >>> pip install json2table\n\nIf instead the repo was cloned, navigate to the root directory of the ``json2table`` package from the command line and execute:\n\n::\n\n    >>> python setup.py install\n\nTests\n-----\n\nIn order to verify the code is working, from the command line navigate to the ``json2table`` root directory and run:\n\n::\n\n    >>> python -m unittest tests.test_json2table",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Convert JSON to an HTML table",
    "version": "1.1.5",
    "split_keywords": [
        "json",
        "html",
        "convert",
        "table"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c02093b122d0daec1cc728b6e6dde860615e81c2bf03fb05cad9e3c1899b3c11",
                "md5": "410dbda6a740e2c0b081cd04485197a8",
                "sha256": "d84eac9471f404f17c9ec1ca5935d7088917f669b3ba5cc5a027f865e7896356"
            },
            "downloads": -1,
            "filename": "json2table-1.1.5-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "410dbda6a740e2c0b081cd04485197a8",
            "packagetype": "bdist_wheel",
            "python_version": "2.7",
            "requires_python": null,
            "size": 8672,
            "upload_time": "2016-10-21T22:36:39",
            "upload_time_iso_8601": "2016-10-21T22:36:39.377259Z",
            "url": "https://files.pythonhosted.org/packages/c0/20/93b122d0daec1cc728b6e6dde860615e81c2bf03fb05cad9e3c1899b3c11/json2table-1.1.5-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fef605039bd75d6ec8a07135bf97d6d11e92ab51e2939cd065bb6f7d0be46de5",
                "md5": "2dc545c230a987e59df50a1aeca55858",
                "sha256": "838e4c052276252fec7b0dbd10668258ce0997605888a250307bd6b124ae6d0a"
            },
            "downloads": -1,
            "filename": "json2table-1.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "2dc545c230a987e59df50a1aeca55858",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6326,
            "upload_time": "2016-10-21T22:36:28",
            "upload_time_iso_8601": "2016-10-21T22:36:28.803583Z",
            "url": "https://files.pythonhosted.org/packages/fe/f6/05039bd75d6ec8a07135bf97d6d11e92ab51e2939cd065bb6f7d0be46de5/json2table-1.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2016-10-21 22:36:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "latture",
    "github_project": "json2table",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "json2table"
}
        
Elapsed time: 0.06259s