title: How to create a tree in NetworkX and display it in D3.js
body: <p>I have to write a piece of code which takes some data from MySQL and use the data to make a graph in D3.js. My D3.js code is working when I give it static data in this format: </p>
  
  <pre><code>    {
          "name": " ",
          "children": [{
                  "name": "HSC",
                  "size": 0.20,
                  "children": [
                      [{
                          "name": "MPP",
                          "size": 15,
                          "children": [{
                              "name": "CMP",
                              "size": 8,
                              "children": [{
                                  "name": "MEP",
                                  "size": 6
                              }, {
                                  "name": "GMP",
                                  "size": 10,
                                  "children": [{
                                      "name": "early PM",
                                      "size": 2,
                                      "children": [{
                                          "name": "early PM",
                                          "size": 2,
                                          "children": []
                                      }]
                                  }]
                              }]
                          }]
                      }]
                  },
                  {
                      "name": "AML",
                      "size": 1,
                      "children": [{
                          "name": "AML t(8,21)",
                          "size": 30
                      }, {
                          "name": "AML t(11q23)",
                          "size": 10
                      }, {
                          "name": "AML inv(16)",
                          "size": 8
                      }, {
                          "name": "AML t(15,17)",
                          "size": 11
                      }]
                  }
                  ], "size": 1
          }
  </code></pre>
  
  <p>So I need a code written in python which can make the JSON structure like above using NetworkX.</p>
  
  <p>My code so far:</p>
  
  <pre><code>n = 1 # the number of children for each node 
  depth = 1 # number of levels, starting from 0
  
  G = nx.Graph()
  G.add_node(1) # initialize root
  
  ulim = 0
  for level, row in enumerate(rows): #each row contains name and a size
    print row # loop over each level
    nl = n**level # number of nodes on a given level
    llim = ulim + 1 # index of first node on a given level 
    ulim = ulim + nl # index of last node on a given level
    for i in range(nl): # loop over nodes (parents) on a given level
      parent = llim + i
      offset = ulim + i * n + 1 # index pointing to node just before first child
      for j in range(n): # loop over children for a given node (parent)
        child = offset + j
        G.add_node(child)
        G.add_edge(parent, child)
  </code></pre>