File size: 4,530 Bytes
ed7ca64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import gradio as gr
import modelscope_studio.components.antd as antd
import modelscope_studio.components.antdx as antdx
import modelscope_studio.components.base as ms


def on_file_click(e: gr.EventData):
    print("file clicked:", e._data["payload"])


def on_folder_click(e: gr.EventData):
    print("folder clicked:", e._data["payload"])


def on_selected_file_change(e: gr.EventData):
    print("selected file changed:", e._data["payload"])


with gr.Blocks() as demo:
    with ms.Application():
        with antdx.XProvider():
            antd.Divider("Basic Folder Tree")
            with antdx.Folder(default_expand_all=True) as folder1:
                with antdx.Folder.TreeNode(title="src", path="/src"):
                    antdx.Folder.TreeNode(title="index.ts",
                                          path="/src/index.ts",
                                          content="export * from './app';")
                    antdx.Folder.TreeNode(
                        title="app.ts",
                        path="/src/app.ts",
                        content=
                        "// Application entry point\nconsole.log('Hello World');"
                    )
                    with antdx.Folder.TreeNode(title="components",
                                               path="/src/components"):
                        antdx.Folder.TreeNode(
                            title="Button.tsx",
                            path="/src/components/Button.tsx",
                            content=
                            "import React from 'react';\n\nexport const Button = () => <button>Click me</button>;"
                        )
                        antdx.Folder.TreeNode(
                            title="Input.tsx",
                            path="/src/components/Input.tsx",
                            content=
                            "import React from 'react';\n\nexport const Input = () => <input />;"
                        )
                    with antdx.Folder.TreeNode(title="utils",
                                               path="/src/utils"):
                        antdx.Folder.TreeNode(
                            title="helpers.ts",
                            path="/src/utils/helpers.ts",
                            content=
                            "export const add = (a: number, b: number) => a + b;"
                        )
                antdx.Folder.TreeNode(
                    title="package.json",
                    path="/package.json",
                    content=
                    '{\n  "name": "my-project",\n  "version": "1.0.0"\n}')
                antdx.Folder.TreeNode(
                    title="README.md",
                    path="/README.md",
                    content="# My Project\n\nA sample project.")

            antd.Divider("Using tree_data Prop")
            antdx.Folder(
                default_expand_all=True,
                tree_data=[{
                    "title":
                    "project",
                    "path":
                    "/project",
                    "children": [{
                        "title": "main.py",
                        "path": "/project/main.py",
                        "content": "print('Hello, World!')"
                    }, {
                        "title":
                        "utils.py",
                        "path":
                        "/project/utils.py",
                        "content":
                        "def greet(name):\n    return f'Hello, {name}!'"
                    }, {
                        "title":
                        "tests",
                        "path":
                        "/project/tests",
                        "children": [{
                            "title":
                            "test_main.py",
                            "path":
                            "/project/tests/test_main.py",
                            "content":
                            "import unittest\n\nclass TestMain(unittest.TestCase):\n    pass"
                        }]
                    }]
                }, {
                    "title": "requirements.txt",
                    "path": "/requirements.txt",
                    "content": "gradio\nmodelscope-studio"
                }])

            folder1.file_click(fn=on_file_click)
            folder1.folder_click(fn=on_folder_click)
            folder1.selected_file_change(fn=on_selected_file_change)

if __name__ == "__main__":
    demo.queue().launch()