Generating Folder Tree Graph

This is the second article introducing my Weixin Official Account PHP Project. In this article, I will focus on my project folder structure relationship and also work out some visualization results. The first article introducing my Weixin Official Account PHP Project can be accessed via this link: Generating PHP Project Dependency Graph .

Preparation

Python Environment Check

import sys
print(sys.executable)
print(sys.version_info) # `sys.version`

Generating the Relationship

As all programmers know, there is one really handy way to generate folder tree relationship data, which is via bash tree command.

Output given by tree -J is as follows. (for the sake of space, I truncated the output.)

[{"type":"directory","name": ".","contents":[
    {"type":"directory","name":"api","contents":[
      {"type":"file","name":"articles.php"},
      {"type":"file","name":"index.php"},
      {"type":"directory","name":"timebasedgreeting","contents":[
        {"type":"file","name":"index.php"}
      ]}
    ]},
    {"type":"directory","name":"apps","contents":[
      {"type":"directory","name":"danmaku","contents":[
        {"type":"file","name":"index.html"}
      ]},
      {"type":"file","name":"index.html"},
      {"type":"directory","name":"nervecat","contents":[
        {"type":"file","name":"index.html"}
      ]}
    ]}
  ]},
  {"type":"report","directories":54,"files":236}
]

This is a nice output. However, in order to visualize this relationship data, further data transformation is required. From my point of view, to transform this data requires similar efforts comparing to generate new data from the ground using Python. So, I will be on the journey of generating folder relationship data using Python.

Since too much time spent on coding, less time left for writing. Here, I will just paste my output here. The same as yesterday's article, I like to use .csv, .json and .gv data.

My output data are in the formate of following example.

As json,

{
    "children": [
        {
          "children": [
              {
                "name": "apiinvoker.php"
              },
              {
                "name": "articles.php"
              },
              {
                "name": "config.php"
              }
          ],
          "name": "php/"
        },
        {
          "children": [
              {
                "name": "01.jpg"
              },
              {
                "name": "02.jpg"
              },
              {
                "name": "03.jpg"
              }
          ],
          "name": "images/"
        }
    ],
  "name": "/"
}

As csv,

source,target
/,/api/
/api/,/api/apiinvoker.php
/api/,/api/articles.php
/api/,/api/scan.php
/,/apps/
/apps/,/apps/danmaku/

More Collected

Using Python to list all sub-directories and files.

for root, dirs, files in os.walk(path):
    for d in dirs:
        print(root + os.sep + d) # using the plus way
    for f in files:
        print(os.path.join(root, f)) # using the join way

Generating the Graph

For visualization, since folder tree is hierarchical, so I chose d3.js tree as the graph type. Since csv and json data are identical and can be converted to each other using JavaScript. And apperently, my json data is more d3.js friendly. So I used JSON data as dataset.

d3-circle-packing

PDF version of above graph can be accessed via this link: d3-circle-packing

d3-cluster-dendrogram

PDF version of above graph can be accessed via this link: d3-cluster-dendrogram

d3-collapsible-tree

PDF version of above graph can be accessed via this link: d3-collapsible-tree

d3-radial-tidy-tree

PDF version of above graph can be accessed via this link: d3-radial-tidy-tree

d3-tidy-tree

PDF version of above graph can be accessed via this link: d3-tidy-tree

d3-treemap

PDF version of above graph can be accessed via this link: d3-treemap

d3-vertical-tree

PDF version of above graph can be accessed via this link: d3-vertical-tree

More

Since Folder Structure Tree is not the only concept in my PHP project, methods covered in this article can be used for nearly all other folders.

Afterwords

In fact, the core part of this PHP project is implemented at the first half of 2016. Since I was not out during Christmas time, I spend sometime organizing this old project. And since I will go back to Shanghai in January 2017 and have to find a new job then, I hope this project can give me some advantage for finding a good job.

I am currently searching for a job in Shanghai, Hong Kong or Taiwan, interested in Artificial Intelligence, especially Computer Vision. If I am not lucky enough to find a job in Artificial Intelligence industry, Data Analysis job is also my choice. Since I have a relatively good background as for and enjoy doing some innovation in Algorithm Programming, I would really like to do a job where innovation algorithms need to be developed.

If anyone who would like to recommend me a job, millions of thanks.


* cached version, generated at 2019-02-04 13:59:59 UTC.

Subscribe by RSS