textual icon indicating copy to clipboard operation
textual copied to clipboard

Sparklines do not work with collections.deque

Open rhacer63 opened this issue 1 year ago • 5 comments

from collections import deque

from textual.app import App, ComposeResult
from textual.widgets import Sparkline

data = deque([1, 2, 2, 1, 1, 4, 3, 1, 1, 8, 8, 2])


class SparklineBasicApp(App[None]):
    def compose(self) -> ComposeResult:
        yield Sparkline(
            data,
            summary_function=max,
        )


app = SparklineBasicApp()
if __name__ == "__main__":
    app.run()

I was working on creating a scrolling Sparkline and ran into this problem. When using a deque as the data element for a Sparkline you receive a TypeError: sequence index must be integer, not 'slice' exception.

rhacer63 avatar Dec 18 '23 21:12 rhacer63

Hey, thanks for raising this issue.

As a temporary workaround, which you probably thought of already, you can create the sparkline with Sparkline(list(data), ...).

rodrigogiraoserrao avatar Dec 19 '23 13:12 rodrigogiraoserrao

Seems to be a typing issue. A Sequence should accept a slice according to the docs.

willmcgugan avatar Dec 19 '23 14:12 willmcgugan

In conclusion, issubclass(deque, Sequence) should return False, right? In that case, we can close this issue, right?

rodrigogiraoserrao avatar Dec 19 '23 15:12 rodrigogiraoserrao

Hey, thanks for raising this issue.

As a temporary workaround, which you probably thought of already, you can create the sparkline with Sparkline(list(data), ...).

I, of course, had not already thought of that, though I would have gotten there eventually! Thank you very much.

rhacer63 avatar Dec 19 '23 19:12 rhacer63

Opened the bug report https://github.com/python/cpython/issues/113313 to track the underlying issue.

rodrigogiraoserrao avatar Dec 20 '23 11:12 rodrigogiraoserrao

So it looks like the Python devs have refined the definition of Sequence to not include slices.

We should look in to updating the typing, or make it work without slices if at all possible.

willmcgugan avatar Feb 19 '24 13:02 willmcgugan

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

github-actions[bot] avatar Feb 21 '24 14:02 github-actions[bot]