textual
textual copied to clipboard
Sparklines do not work with collections.deque
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.
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), ...)
.
Seems to be a typing issue. A Sequence should accept a slice according to the docs.
In conclusion, issubclass(deque, Sequence)
should return False
, right?
In that case, we can close this issue, right?
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.
Opened the bug report https://github.com/python/cpython/issues/113313 to track the underlying issue.
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.