-
-
Notifications
You must be signed in to change notification settings - Fork 86
now with the coding hashbang at the top #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ffb9548
bootstrap support + multi-column tables for table-like JSON
b8caf93
moved the coding hashbang to the top
8c4264c
new contributor line as suggested
461b073
use global `use_bootstrap_setting` instead of passed in args in order…
58c960f
removed use_bootstrap option, added generic table_attributes option i…
d2e1893
fixed a bug that would lead to lists within new multicolumn tables to…
8232f8e
fixed bug related to iterJson returning `None` by moving from global …
876162b
readded default behavior with table borders
7261d08
fixed something stupid
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
''' | ||
JSON 2 HTML convertor | ||
===================== | ||
|
||
(c) Varun Malhotra 2013 | ||
Source Code: https://github.com/softvar/json2html | ||
------------ | ||
Contributors: Michel Müller(@muellermichel), patch #2 - https://github.com/softvar/json2html/pull/2 | ||
|
||
LICENSE: MIT | ||
-------- | ||
''' | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import ordereddict | ||
|
||
a= '' | ||
|
||
class JSON: | ||
#def __init__(self): | ||
|
||
def convert(self,**args): | ||
''' | ||
convert json Object to HTML Table format | ||
''' | ||
global table_attributes | ||
table_attributes = '' | ||
if 'table_attributes' in args: | ||
table_attributes = args['table_attributes'] | ||
else: | ||
table_attributes = "border=\"1\"" | ||
if 'json' in args: | ||
self.json_input = args['json'] | ||
try: | ||
|
@@ -31,66 +37,77 @@ def convert(self,**args): | |
self.json_input = json.dumps(self.json_input) | ||
else: | ||
raise Exception('Can\'t convert NULL!') | ||
|
||
|
||
ordered_json = json.loads(self.json_input, object_pairs_hook=ordereddict.OrderedDict) | ||
return self.htmlConvertor(ordered_json) | ||
|
||
def columnHeadersFromListOfDicts(self,ordered_json): | ||
if len(ordered_json) < 2: | ||
return None | ||
if not isinstance(ordered_json[0],dict): | ||
return None | ||
column_headers = ordered_json[0].keys() | ||
for entry in ordered_json: | ||
if not isinstance(entry,dict): | ||
return None | ||
if len(entry.keys()) != len(column_headers): | ||
return None | ||
for header in column_headers: | ||
if not header in entry: | ||
return None | ||
return column_headers | ||
|
||
#ordered_json needs to be a dict | ||
def iterJson(self,ordered_json): | ||
global a | ||
a=a+ "<table border=\"1\">" | ||
def markup(entry, parent_is_list=False): | ||
if(isinstance(entry,unicode)): | ||
return unicode(entry) | ||
if(isinstance(entry,int) or isinstance(entry,float)): | ||
return str(entry) | ||
if(parent_is_list and isinstance(entry,list)==True): | ||
#list of lists are not accepted | ||
return '' | ||
if(isinstance(entry,list)==True) and len(entry) == 0: | ||
return '' | ||
if(isinstance(entry,list)==True): | ||
return '<ul><li>' + '</li><li>'.join([markup(child, parent_is_list=True) for child in entry]) + '</li></ul>' | ||
if(isinstance(entry,dict)==True): | ||
return self.iterJson(entry) | ||
return '' #safety: don't do recursion over anything that we don't know about - iteritems() will most probably fail | ||
|
||
a = '' | ||
global table_attributes | ||
|
||
table_init_markup = "<table %s>" %(table_attributes) | ||
a=a+ table_init_markup | ||
for k,v in ordered_json.iteritems(): | ||
a=a+ '<tr>' | ||
a=a+ '<th>'+ str(k) +'</th>' | ||
if (v==None): | ||
v = unicode("") | ||
v = unicode("") | ||
if(isinstance(v,list)): | ||
a=a+ '<td><ul>' | ||
for i in range(0,len(v)): | ||
if(isinstance(v[i],unicode)): | ||
a=a+ '<li>'+unicode(v[i])+'</li>' | ||
elif(isinstance(v[i],int) or isinstance(v,float)): | ||
a=a+ '<li>'+str(v[i])+'</li>' | ||
elif(isinstance(v[i],list)==False): | ||
self.iterJson(v[i]) | ||
a=a+ '</ul></td>' | ||
a=a+ '</tr>' | ||
elif(isinstance(v,unicode)): | ||
a=a+ '<td>'+ unicode(v) +'</td>' | ||
a=a+ '</tr>' | ||
elif(isinstance(v,int) or isinstance(v,float)): | ||
a=a+ '<td>'+ str(v) +'</td>' | ||
a=a+ '</tr>' | ||
else: | ||
a=a+ '<td>' | ||
#a=a+ '<table border="1">' | ||
self.iterJson(v) | ||
a=a+ '</td></tr>' | ||
column_headers = self.columnHeadersFromListOfDicts(v) | ||
if column_headers != None: | ||
a=a+ '<td>' | ||
a=a+ table_init_markup | ||
a=a+ '<tr><th>' + '</th><th>'.join(column_headers) + '</th></tr>' | ||
for list_entry in v: | ||
a=a+ '<tr><td>' + '</td><td>'.join([markup(list_entry[column_header]) for column_header in column_headers]) + '</td></tr>' | ||
a=a+ '</table>' | ||
a=a+ '</td>' | ||
a=a+ '</tr>' | ||
continue | ||
a=a+ '<td>' + markup(v) + '</td>' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if markup(v) return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, could you try now? |
||
a=a+ '</tr>' | ||
a=a+ '</table>' | ||
return a | ||
|
||
def htmlConvertor(self,ordered_json): | ||
''' | ||
converts JSON Object into human readable HTML representation | ||
generating HTML table code with raw/bootstrap styling. | ||
''' | ||
global a | ||
a='' | ||
try: | ||
for k,v in ordered_json.iteritems(): | ||
pass | ||
self.iterJson(ordered_json) | ||
|
||
except: | ||
for i in range(0,len(ordered_json)): | ||
if(isinstance(ordered_json[i],unicode)): | ||
a=a+ '<li>'+unicode(ordered_json[i])+'</li>' | ||
elif(isinstance(ordered_json[i],int) or isinstance(ordered_json[i],float)): | ||
a=a+ '<li>'+str(ordered_json[i])+'</li>' | ||
elif(isinstance(ordered_json[i],list)==False): | ||
self.htmlConvertor(ordered_json[i]) | ||
|
||
return a | ||
|
||
return self.iterJson(ordered_json) | ||
|
||
json2html = JSON() | ||
json2html = JSON() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about if no
table_attributes
is/are present? Default table border has been overwritten. Also when I try the simple example:I didn't get the correct output. Please re-check what's being missing. Also, do share your json, I'm curious to see the sorting as suggested by you. It would be great to see everything working perfectly on my end. Thanks for introducing sort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, just committed another fix that readds the borders as default behavior. I’ve tested your json and to me it looks ok now. About my json: Here you go, it’s a doozy ;-).
{"errors": ["invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest4.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest5.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest6.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:ategra-test.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtestjan1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:www.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-bd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-vd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest3.myproject.com", "ategra-test's metadata is invalid", "www's metadata is invalid", "acme's metadata is invalid", "myproject's metadata is invalid", "zh-bd's metadata is invalid", "acme2's metadata is invalid", "zh-vd's metadata is invalid", "no db backend defined for environment myproject-oldtestjan2. Client replication will not be available!", "no db backend defined for environment 09test1. Client replication will not be available!"], "environments": [{"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest4", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest5", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest7", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest6", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "ategra-test", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtestjan1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "www", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest8", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "myproject", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-bd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest10", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme2", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-vd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest3", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10001"], "server": "pgrid0", "environment name": "myproject-oldtestjan2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10002"], "server": "pgrid0", "environment name": "09test1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10003"], "server": "pgrid0", "environment name": "testnew20151", "db backends": ["pgrid0.myproject-old.com:11003"], "email used for registration": "[email protected]"}], "servers": ["pgrid0"]}
{"errors": ["invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest4.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest5.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest6.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:ategra-test.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtestjan1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:www.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-bd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:acme2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:zh-vd.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest1.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest2.myproject.com", "invalid frontend definition. Please check the domain and the characters in the name: frontend:myproject-oldtest3.myproject.com", "ategra-test's metadata is invalid", "www's metadata is invalid", "acme's metadata is invalid", "myproject's metadata is invalid", "zh-bd's metadata is invalid", "acme2's metadata is invalid", "zh-vd's metadata is invalid", "no db backend defined for environment myproject-oldtestjan2. Client replication will not be available!", "no db backend defined for environment 09test1. Client replication will not be available!"], "environments": [{"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest4", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest5", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest7", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest6", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "ategra-test", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtestjan1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "www", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest8", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "myproject", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-bd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest10", "db backends": [], "email used for registration": "[email protected]"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "acme2", "db backends": [], "email used for registration": "n/a"}, {"status": "undefined", "web backends": [], "server": "undefined", "environment name": "zh-vd", "db backends": [], "email used for registration": "n/a"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "no-server-assigned", "web backends": [], "server": "undefined", "environment name": "myproject-oldtest3", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10001"], "server": "pgrid0", "environment name": "myproject-oldtestjan2", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10002"], "server": "pgrid0", "environment name": "09test1", "db backends": [], "email used for registration": "[email protected]"}, {"status": "activated", "web backends": ["pgrid0.myproject-old.com:10003"], "server": "pgrid0", "environment name": "testnew20151", "db backends": ["pgrid0.myproject-old.com:11003"], "email used for registration": "[email protected]"}], "servers": ["pgrid0"]}
On 01 Feb 2015, at 22:41, Varun Malhotra [email protected] wrote: