Skip to content Skip to sidebar Skip to footer

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:

def printItems(dictObj, parent, indent):
    if len(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><input type="checkbox" id="Animalia-root">Animalia</li>
  <ul>
    <li><input type="checkbox" id="Chordata-Animalia">Chordata</li>
    <ul>
      <li><input type="checkbox" id="Mammalia-Chordata">Mammalia</li>
      <ul>
        <li><input type="checkbox" id="Carnivora-Mammalia">Carnivora</li>
        <ul>
          <li><input type="checkbox" id="Canidae-Carnivora">Canidae</li>
          <ul>
            <li><input type="checkbox" id="Canis-Canidae">Canis</li>
            <ul>
              <li><input type="checkbox" id="coyote-Canis">coyote</li>
              <li><input type="checkbox" id="dog-Canis">dog</li>
            </ul>
          </ul>
          <li><input type="checkbox" id="Felidae-Carnivora">Felidae</li>
          <ul>
            <li><input type="checkbox" id="Felis-Felidae">Felis</li>
            <ul>
              <li><input type="checkbox" id="cat-Felis">cat</li>
            </ul>
            <li><input type="checkbox" id="Panthera-Felidae">Panthera</li>
            <ul>
              <li><input type="checkbox" id="lion-Panthera">lion</li>
            </ul>
          </ul>
        </ul>
      </ul>
    </ul>
  </ul>
  <li><input type="checkbox" id="Plantae-root">Plantae</li>
  <ul>
    <li><input type="checkbox" id="Solanales-Plantae">Solanales</li>
    <ul>
      <li><input type="checkbox" id="Convolvulaceae-Solanales">Convolvulaceae</li>
      <ul>
        <li><input type="checkbox" id="Ipomoea-Convolvulaceae">Ipomoea</li>
        <ul>
          <li><input type="checkbox" id="sweet potato-Ipomoea">sweet potato</li>
        </ul>
      </ul>
      <li><input type="checkbox" id="Solanaceae-Solanales">Solanaceae</li>
      <ul>
        <li><input type="checkbox" id="Solanum-Solanaceae">Solanum</li>
        <ul>
          <li><input type="checkbox" id="tomato-Solanum">tomato</li>
          <li><input type="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"