Skip to content

Conversation

@uriyyo
Copy link
Contributor

@uriyyo uriyyo commented Oct 10, 2025

Make Sparkline be able to use not only 1 as a height value.

Demo app:

import random
from statistics import mean

from textual.containers import Grid

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

random.seed(73)
data = [random.expovariate(1 / 3) for _ in range(1000)]


class SparklineAnyHeightApp(App[None]):
    DEFAULT_CSS = """
    SparklineAnyHeightApp {
        Sparkline {
            height: 1fr;
            border: round white;
        }
    }
    """

    def compose(self) -> ComposeResult:
        with Grid():
            yield Sparkline(data, summary_function=max)
            yield Sparkline(data, summary_function=mean)
            yield Sparkline(data, summary_function=min)
            yield Sparkline([*range(len(data))])
            yield Sparkline([1])
            yield Sparkline([])


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

Result:
image

Please review the following checklist.

  • Docstrings on all new or modified functions / classes
  • Updated documentation
  • Updated CHANGELOG.md (where appropriate)

@uriyyo uriyyo marked this pull request as ready for review October 10, 2025 12:44
Copy link
Member

@willmcgugan willmcgugan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

Can I request a snapshot test?

See test_snapshots.py for examples. Can be very simple, just a single sparkline is all that is needed.

Copilot AI review requested due to automatic review settings November 10, 2025 20:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the Sparkline widget to support multi-line rendering at any height, not just a single line. The implementation adds a height parameter that controls how many terminal lines the sparkline uses, with taller sparklines providing more granular vertical resolution for data visualization.

Key Changes:

  • Added height parameter to Sparkline renderable with appropriate default of None (defaulting to 1)
  • Updated rendering logic to distribute bar segments across multiple lines
  • Modified edge case handling (empty data, single datapoint) to respect the height parameter

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/textual/renderables/sparkline.py Core implementation: added height parameter, updated rendering algorithm to distribute bar segments across multiple lines, and fixed edge cases
src/textual/widgets/_sparkline.py Widget integration: passes widget height to the sparkline renderable
tests/renderables/test_sparkline.py Test coverage: added parametrized tests for heights 1, 2, and 3
CHANGELOG.md Documentation: added changelog entry for the new feature

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@uriyyo uriyyo requested a review from willmcgugan November 10, 2025 20:23
@uriyyo
Copy link
Contributor Author

uriyyo commented Nov 10, 2025

@willmcgugan Added snapshot test, hope I did it right 🙏

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

Successfully merging this pull request may close these issues.

2 participants