生成目录结构的JSON供Webix树小部件使用

Generating JSON of directory structure for consumption by Webix tree widget

本文关键字:Webix 小部 JSON 结构      更新时间:2023-09-26

我正在使用Webix和Python/Flask进行一个项目,渲染树视图小部件遇到了困难。我的问题是正确构建小部件所需的JSON。我正在尝试生成给定示例的目录结构:

结构:

。├──dirone│└──文件├──dirthree│├──somedir││└──另一个文件│└──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"
    }
]

在过去的两个小时里,我一直在努力寻找一个可以渲染它的方法。有没有生成结构的方法?任何帮助都将不胜感激!

这并不是一个真正的解决方案,但Webix小部件可以配置为使用不同结构的json,或者,在case或tree中,加载数据记录的纯列表,并通过一些参数对数据进行分组来构建树结构

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

我想我在没有os.walk()的情况下就做到了,但我仍然通过递归函数(boo):

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深度。

谢谢你的建议!