Skip to content
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

Closed
barisione opened this issue May 16, 2023 · 7 comments
Closed

DataTable.get_cell should accept strings as keys #2586

barisione opened this issue May 16, 2023 · 7 comments

Comments

@barisione
Copy link

DataTable.get_cell is defined like this:

def get_cell(self, row_key: RowKey, column_key: ColumnKey) -> CellType:
    """[...]"""
    try:
        cell_value = self._data[row_key][column_key]
    [...]

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):

class MyApp(App):
    _FOOBAR_KEY = "foobar"

    def on_ready(self):
        [...]
        table.add_row("Foo Bar", key=self._FOOBAR_KEY)

Textual Diagnostics

Versions

Name Value
Textual 0.24.1
Rich 13.3.5

Python

Name Value
Version 3.10.1
Implementation CPython
Compiler Clang 13.0.0 (clang-1300.0.29.30)
Executable /Users/bari/.pyenv/versions/3.10.1/envs/undo-core-venv3.10/bin/python3.10

Operating System

Name Value
System Darwin
Release 21.6.0
Version Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64

Terminal

Name Value
Terminal Application iTerm.app (3.5.0beta10)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=178, height=45
legacy_windows False
min_width 1
max_width 178
is_terminal False
encoding utf-8
max_height 45
justify None
overflow None
no_wrap False
highlight None
markup None
height None
@github-actions
Copy link

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

@guystreeter
Copy link

Other functions like update_cell() accept a str for both row_key and column_key. It seems like this is an oversight.

trav-c added a commit to trav-c/textual that referenced this issue Jul 1, 2023
…ze#2586)

Added DataTable.get_cell_coordinate
Added DataTable.get_row_index (Textualize#2587)
Added DataTable.get_column_index
@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@bb-xops
Copy link

bb-xops commented Aug 21, 2024

hey this doesnt work.

row_key, _ = table.coordinate_to_cell_key(table.cursor_coordinate)
current_value = table.get_cell(row_key, "ID")

its giving me this error. I cannot simply used "ID" for my column, super frustrating. It only accepts memory like this 0x105b70100.

CellDoesNotExist: No cell exists for row_key=<textual.widgets._data_table.RowKey object at 0x105b70100>, column_key='ID'.

I took a look at your github PR here that you closed out.
#2876

But it doesn't have test cases for get_cell

@bb-xops
Copy link

bb-xops commented Aug 21, 2024

row_key, col_key = table.coordinate_to_cell_key([table.cursor_row, 0])
current_value = table.get_cell(row_key, col_key)

this was my workaround to get this to work.

@TomJGooding
Copy link
Contributor

TomJGooding commented Aug 21, 2024

@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()

@bb-xops
Copy link

bb-xops commented Aug 21, 2024

Ah thank you so much. You're correct. I've mistaken Label with the Key.
For anyone else that got lost here let me sum up a tldr on what the issue was.

table.add_column("ID", key="id-key")
row_key, _ = table.coordinate_to_cell_key([table.cursor_coordinate])
table.get_cell(row_key, 'ID') # Incorrect
table.get_cell(row_key, 'id-key') # Correct

Thank you @TomJGooding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants