File Tree

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

python 7 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 its script on the page: <script src="{% static 'djust_components/file-tree.js' %}"></script>. djust ships it; without it the markup renders but does not update in the browser.

Parameters

NameTypeDefaultDescription
nodeslist | NoneNonelist of node dicts with name, type (file/folder), children
selectedstr''name/path of currently selected file
eventstr — renames the event; identity is `name`'select_file'djust event fired on file selection
show_iconsboolTrueshow file/folder icons (default True)
custom_classstr''additional CSS classes
toggle_eventstr — renames the event; identity is `name`''djust event sent with ``{"path": [...], "expanded": bool}`` when the reader expands or collapses a folder (optional)
**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 2185, 2186, 2189, 2190, 2191, 2192, 2532, 2533, 2534. Override those rules, or the custom properties they read, in a stylesheet loaded after it.