-
Notifications
You must be signed in to change notification settings - Fork 826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataTable.get_cell
should accept strings as keys
#2586
Comments
Other functions like |
…ze#2586) Added DataTable.get_cell_coordinate Added DataTable.get_row_index (Textualize#2587) Added DataTable.get_column_index
Don't forget to star the repository! Follow @textualizeio for Textual updates. |
hey this doesnt work.
its giving me this error. I cannot simply used "ID" for my column, super frustrating. It only accepts memory like this
I took a look at your github PR here that you closed out. But it doesn't have test cases for |
this was my workaround to get this to work. |
@bb-xops I think the problem in your example is that you are mistaking the column key for the column label. See: https://textual.textualize.io/widgets/data_table/#keys Here's a quick example demonstrating this: from textual.app import App, ComposeResult
from textual.widgets import DataTable, Footer
from textual.widgets.data_table import CellDoesNotExist
class GetCellApp(App):
BINDINGS = [
("b", "bad_get_cell", "Bad get_cell"),
("g", "good_get_cell", "Good get_cell"),
]
def compose(self) -> ComposeResult:
yield DataTable()
yield Footer()
def on_mount(self) -> None:
table = self.query_one(DataTable)
table.add_column("ID", key="id-key")
table.add_column("Name", key="name-key")
table.add_row(1, "John Smith")
def action_bad_get_cell(self) -> None:
table = self.query_one(DataTable)
row_key, _ = table.coordinate_to_cell_key(table.cursor_coordinate)
try:
self.notify(f"{table.get_cell(row_key, 'ID')=}")
except CellDoesNotExist:
self.notify(
"CellDoesNotExist: No cell exists with column key 'ID'",
severity="error",
)
def action_good_get_cell(self) -> None:
table = self.query_one(DataTable)
row_key, _ = table.coordinate_to_cell_key(table.cursor_coordinate)
try:
self.notify(f"{table.get_cell(row_key, 'id-key')=}")
except CellDoesNotExist:
self.notify(
"CellDoesNotExist: No cell exists with column key 'id-key'",
severity="error",
)
if __name__ == "__main__":
app = GetCellApp()
app.run() |
Ah thank you so much. You're correct. I've mistaken Label with the Key.
Thank you @TomJGooding |
DataTable.get_cell
is defined like this:Which means that passing strings as keys actually works (even if mypy doesn't like it) as keys' hashes are identical to the hashes of the strings they wrap.
Being able to use a string key is convenient otherwise code like this is not useful as you need to also save the return value of
add_row
(which defeats the point of passing a key in):Textual Diagnostics
Versions
Python
Operating System
Terminal
Rich Console options
The text was updated successfully, but these errors were encountered: