File Tree

File browser tree with icons, expand/collapse, and selection.

python 6 params

Preview

src
app.py
util.py
README.md
Arguments
file_tree(nodes=[{'name': 'src', 'type': 'folder', 'children': [{'name': 'app.py', 'type': 'file'}, {'name': 'util.py', 'type': 'file'}]}, {'name': 'README.md', 'type': 'file'}], selected='src/app.py')

Usage

views.py
python
from djust import LiveView
from djust.decorators import event_handler
from djust.components import FileTree


class MyView(LiveView):
    template_name = "my_template.html"

    def mount(self, request, **kwargs):
        self.component = FileTree(nodes=[{'name': 'src', 'type': 'folder', 'children': [{'name': 'app.py', 'type': 'file'}, {'name': 'util.py', 'type': 'file'}]}, {'name': 'README.md', 'type': 'file'}], selected='src/app.py')

    @event_handler()
    def select_file(self, value, **kwargs):
        self.component.selected = value
my_template.html
django
{{ component }}

Events this component sends to the view: select_file.

Needs a client hook djust does not ship: dj-hook="FileTree". The markup renders, but the interaction described above is yours to add — register window.djust.hooks.FileTree = { mounted() { … } } before client.js loads (see the hooks guide).

Parameters

NameTypeDefault
nodeslist | NoneNone
selectedstr''
eventstr — renames the event; identity is `name`'select_file'
show_iconsboolTrue
custom_classstr''
**kwargs— passed to `Component.__init__`: `name=` identifies the instance in the events it sends, `id=` sets `component.id`, and any other keyword is kept as state
Source & styles
FilePathNotes
moduledjust.components.components.file_treeSubclass it, or pass custom_class, to change how it renders.
cssdjust_components/components.cssDefines 9 of this component's classes at line 2129, 2130, 2133, 2134, 2135, 2136, 2429, 2430, 2431. Override those rules, or the custom properties they read, in a stylesheet loaded after it.