Textualize / textual-plotext

A Textual widget wrapper library for Plotext
MIT License
113 stars 5 forks source link

Plot does not refresh #11

Closed FilMarini closed 4 months ago

FilMarini commented 4 months ago

Hi, I wanted to update the plot on regular time intervals.

This is what I've tried:

"""A small self-contained example of a Plotext plot in a Textual app."""

from textual.app import App, ComposeResult

from textual_plotext import PlotextPlot

class ScatterApp(App[None]):
    """Example Textual application showing a Plotext plot."""

    def compose(self) -> ComposeResult:
        """Compose the plotting widget."""
        yield PlotextPlot()

    def on_mount(self) -> None:
        """Set up the plot."""
        self.plt = self.query_one(PlotextPlot).plt
        self.plt.title("Scatter Plot")
        self.y = [1,2,3,4,5,6,7,8,9]
        self.plt.scatter(self.y)
        self.update_timer = self.set_interval(1, self.update_plotdata)

    def update_plotdata(self) -> None:
        self.plt.clear_data()
        self.y = [x+1 for x in self.y]
        self.plt.scatter(self.y)
        self.refresh()

if __name__ == "__main__":
    ScatterApp().run()

The plot though does not update, keeping the plot for the self.y = [1,2,3,4,5,6,7,8,9] list. I've tried in many other ways, but everything turned out to be useless in updating the plot.

Is there a way to do this?

Running

tipptop3d commented 4 months ago

Try refreshing the actual PlotextPlot widget

def update_plotdata(self) -> None:
    plt_widget = self.query_one(PlotextPlot)
    self.plt.clear_data()
    self.y = [x + 1 for x in self.y]
    self.plt.scatter(self.y)
    plt_widget.refresh()

Alternativly you can subclass PlotextPlot, see this example

FilMarini commented 4 months ago

Refreshing the PlotextPlot widget works.

Thanks!