It used only standart python libraries for python 3.6+, except pytest module. But it will be working with earlie python 3 versions also if you change f-strings to string.format() method.
If you need to run any tests install pytest. Check the documentation in the current tests modules. If you no need default test from it project you may delete all folders that names 'tests'.
Be shure that you have all dependencies from requirements.txt. Usually it all includes in standart python3 libraries (except pytest).
Put your json-file (filename should be 'source.json') to folder json_files (default folder). Also you can keep you json files in any place. Read below hot to do it.
Execute commands below in terminal:
$ cd you_project_path/json_to_html
$ python3 project/__init__.py
Module takes 'source.json' file from folder: json_files, converts it to html-string and printouts the converted data to console.
Flow depends from input data format:
- JSON is array (python list)
2.1 JSON is obj (python dict)
2.2 JSON is obj (python dict, dict keys contain html classes or id)
[
{
"h3": "Title #1",
"div": "Hello, World 1!"
},
{
"h3": "Title #2",
"div": "Hello, World 2!"
}
]
'<ul><li><h3>Title #1</h3><div>Hello, World 1!</div></li><li><h3>Title #2</h3><div>Hello, World 2!</div></li></ul>'
[
{
"span": "Title #1",
"content": [
{
"p": "Example 1",
"header": "header 1"
}
]
},
{
"div": "div 1"
}
]
'<ul><li><span>Title #1</span><content><ul><li><p>Example 1</p><header>header 1</header></li></ul></content></li><li><div>div 1</div></li></ul>'
{
"p": "hello1"
}
'<p>hello1</p>'
{
"p.my-class#my-id": "hello",
"p.my-class1.my-class2": "example<a>asd</a>"
}
<p id="my-id" class="my-class">hello</p><p class="my-class1 my-class2">example<a>asd</a></p>
$ cd json_to_html/project
>>> from json_to_html import converter
>>> file_path = '/you_project_path/json_to_html/json_files/source.json'
>>> conv = converter.Converter(file_path)
>>> html_string = conv.convert()
>>> print(html_string)
<ul><li><h3>Title #1</h3><div>Hello, World 1!</div></li><li><h3>Title #2</h3><div>Hello, World 2!</div></li></ul>
If you need get current filepath use:
conv.get_file_path()
If you need set or change current filepath use:
conv.set_file_path(file_path)
Please contact with me for any question by e-mail: maiyashik@gmail.com ...