From Python Dictionary To Html List
everyone My question is pretty similar to this one: python: serialize a dictionary into a simple html output I would like convert my python dictionary, that contain a tree, in a HT
Solution 1:
A trivial couple of tweaks to the answer that you referenced will give you the result that you want:
defprintItems(dictObj, parent, indent):
iflen(dictObj):
print'{}<ul>'.format(' ' * indent)
for k,v in dictObj.iteritems():
print'{}<li><input type="checkbox" id="{}-{}">{}</li>'.format(
' ' * (indent+1), k, parent, k)
printItems(v, k, indent+1)
print'{}</ul>'.format(' ' * indent)
The tweak was to also pass the parent key to printItems()
so that it can be included in the id
attribute for the <input type="checkbox"...>
tag. The other change was to remove the leaf node processing because your leaves are denoted by an empty dictionary.
For input:
taxonomy = {
'Animalia': {
'Chordata': {
'Mammalia': {
'Carnivora': {
'Canidae': {
'Canis': {
'coyote': {},
'dog': {}
}
},
'Felidae': {
'Felis': {
'cat': {}
},
'Panthera': {
'lion': {}
}
}
}
}
}
},
'Plantae': {
'Solanales': {
'Convolvulaceae': {
'Ipomoea': {
'sweet potato': {}
}
},
'Solanaceae': {
'Solanum': {
'potato': {},
'tomato': {}
}
}
}
}
}
The output of printItems(taxonomy, 'root', 0)
is:
<ul><li><inputtype="checkbox"id="Animalia-root">Animalia</li><ul><li><inputtype="checkbox"id="Chordata-Animalia">Chordata</li><ul><li><inputtype="checkbox"id="Mammalia-Chordata">Mammalia</li><ul><li><inputtype="checkbox"id="Carnivora-Mammalia">Carnivora</li><ul><li><inputtype="checkbox"id="Canidae-Carnivora">Canidae</li><ul><li><inputtype="checkbox"id="Canis-Canidae">Canis</li><ul><li><inputtype="checkbox"id="coyote-Canis">coyote</li><li><inputtype="checkbox"id="dog-Canis">dog</li></ul></ul><li><inputtype="checkbox"id="Felidae-Carnivora">Felidae</li><ul><li><inputtype="checkbox"id="Felis-Felidae">Felis</li><ul><li><inputtype="checkbox"id="cat-Felis">cat</li></ul><li><inputtype="checkbox"id="Panthera-Felidae">Panthera</li><ul><li><inputtype="checkbox"id="lion-Panthera">lion</li></ul></ul></ul></ul></ul></ul><li><inputtype="checkbox"id="Plantae-root">Plantae</li><ul><li><inputtype="checkbox"id="Solanales-Plantae">Solanales</li><ul><li><inputtype="checkbox"id="Convolvulaceae-Solanales">Convolvulaceae</li><ul><li><inputtype="checkbox"id="Ipomoea-Convolvulaceae">Ipomoea</li><ul><li><inputtype="checkbox"id="sweet potato-Ipomoea">sweet potato</li></ul></ul><li><inputtype="checkbox"id="Solanaceae-Solanales">Solanaceae</li><ul><li><inputtype="checkbox"id="Solanum-Solanaceae">Solanum</li><ul><li><inputtype="checkbox"id="tomato-Solanum">tomato</li><li><inputtype="checkbox"id="potato-Solanum">potato</li></ul></ul></ul></ul></ul>
Solution 2:
This gives you the result.
def func(dict1):
for name, dictionary in dict1.items():
print'<ul><li>'print'<input type=checkbox id={0}-root>'.format(name)
print name
func(dictionary)print'</ul></li>'
dict1 = {
'Animalia': {
'Chordata': {
'Mammalia': {
'Carnivora': {
'Canidae': {
'Canis': {
'coyote': {
},
'dog': {
}
}
},
'Felidae': {
'Felis': {
'cat': {
}
},
'Panthera': {
'lion': {
}
}
}
}
}
}
},
'Plantae': {
'Solanales': {
'Convolvulaceae': {
'Ipomoea': {
'sweet potato': {
}
}
},
'Solanaceae': {
'Solanum': {
'potato': {
},
'tomato': {
}
}
}
}
}
}
func(dict1)
Post a Comment for "From Python Dictionary To Html List"