-
I develop an app that looks like a Kanban board. It has 3 or 4 default columns. I allow users to configure the columns with a Python script in their user config directory. I would like them to be able to specify the keybindings they want without having to subclass my from kanbanthing import Column
columns = [
Column("One", bindings=[("a", "process_one", "Do something with current row")]),
Column("Two", bindings=[("b", "process_one", "Do something else with current row")]),
] ...instead of this from kanbanthing import Column
class ColumnOne(Column):
BINDINGS = [("a", "process_one", "Do something with current row")]
class ColumnTwo(Column):
BINDINGS = [("b", "process_one", "Do something else with current row")]
columns = [
ColumnOne("One"),
ColumnTwo("Two"),
] (notice how the second example has much more boilerplate code, declaring custom classes that are eventually instantiated only once) (my I tried setting bindings in def __init__(self, bindings):
self.BINDINGS = bindings ...but Textual does not seem to pick up the bindings (they don't show up in the footer, and they don't trigger anything). I suppose it gets the I suppose I could also dynamically declare the subclasses myself 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OK, I was able to dynamically create subclasses within the class Column(Container):
_count = 0
def __new__(cls, *args, **kwargs) -> Column:
Column._count += 1
bindings = kwargs.pop("bindings", [])
custom_column_class = type(f"Column{Column._count}", (Column,), {"BINDINGS": bindings})
return super().__new__(custom_column_class)
def __init__(
self,
*,
title: str,
headers: Sequence[str],
producer: Callable,
consumer: Callable | None = None,
bindings: list[tuple[str, str, str]] | None = None, # noqa: ARG002
**kwargs,
) -> None:
... That will do for now 🙂 Note that I'd still find widget instance bindings very useful 😄 |
Beta Was this translation helpful? Give feedback.
OK, I was able to dynamically create subclasses within the
__new__
method of myColumn
class, satisfying Textual's expectations: