2015-03-19 66 views
0

我正在使用Webix和Python/Flask處理項目,並且正在渲染樹視圖構件時觸擊了一面磚牆。我的問題是正確構建小部件所需的JSON。我想產生一個給定的例子的目錄結構:生成供Webix樹構件使用的目錄結構的JSON

結構:

 
. 
├── dirone 
│   └── file 
├── dirthree 
│   ├── somedir 
│   │   └── anotherfile 
│   └── somefile 
├── dirtwo 
└── somefile 

所需的輸出:

[ 
    { 
     "value": "dirone", 
     "path": "dirone", 
     "type": "folder", 
     "children": [ 
      { 
       "value": "file", 
       "path": "dirone/file", 
       "type": "file" 
      } 
     ] 
    }, 
    { 
     "value": "dirtwo", 
     "path": "dirtwo", 
     "type": "folder", 
     "children": [] 
    }, 
    { 
     "value": "dirthree", 
     "path": "dirthree", 
     "type": "folder", 
     "children": [ 
      { 
       "value": "somefile", 
       "path": "dirthree/somefile", 
       "type": "file" 
      }, 
      { 
       "value": "somedire", 
       "path": "dirone/file", 
       "type": "folder", 
       "children": [ 
        { 
         "value": "anotherfile", 
         "path": "dirthree/somedir/anotherfile", 
         "type": "file" 
        } 
       ] 
      } 
     ] 
    }, 
    { 
     "value": "somefile", 
     "path": "somefile", 
     "type": "file" 
    } 
] 

我一直在敲我的頭這在過去兩年數小時試圖獲得一個可以呈現它的方法。有沒有一種方法可以生成結構?任何幫助將不勝感激!

回答

0

這不是一個真正的解決方案,但可以將Webix小部件配置爲使用具有不同結構的json,或者在樹或樹的情況下加載數據記錄的普通列表並通過按參數分組數據來構建樹結構

檢查例如 http://docs.webix.com/samples/17_datatree/01_loading/07_load_group.html

+0

很酷,那會派上用場。我正試圖擺脫PyQt;仍然試圖感受一些js構件框架。 – user1777667 2015-03-20 22:48:53

0

我想我它得到我所需要的無os.walk(),但我仍然可以通過遞歸函數做(噓):

def pathTree(path,id=0): 
    id += 1 
     tree = {'value': os.path.basename(path)} 
     tree['path'] = path 
    tree['id'] = id 
     if os.path.isdir(path): 
      tree['type'] = "folder" 
      tree['data'] = [pathTree(os.path.join(path,x),id) for x in os.listdir(path)] 
    else: 
      tree['type'] = "file" 
    return(tree) 

我唯一的東西現在想現在(雖然不是真正的受益者)是讓它記錄水平/ iter深度。

感謝您的建議!