File Tree
File browser tree with icons, expand/collapse, and selection.
Preview
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 = valuemy_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
| Name | Type | Default | Description |
|---|---|---|---|
| nodes | list | None | None | list of node dicts with name, type (file/folder), children |
| selected | str | '' | name/path of currently selected file |
| event | str — renames the event; identity is `name` | 'select_file' | djust event fired on file selection |
| show_icons | bool | True | show file/folder icons (default True) |
| custom_class | str | '' | additional CSS classes |
| toggle_event | str — 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
| File | Path | Notes |
|---|---|---|
| module | djust.components.components.file_tree | Subclass it, or pass custom_class, to change how it renders. |
| css | djust_components/components.css | Defines 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. |