Code Monkey home page Code Monkey logo

Comments (5)

willmcgugan avatar willmcgugan commented on May 21, 2024

It's likely that they layout system is requesting too many refreshes, that's still a WIP. But in general, you want to avoid modifying state in the render method. There are many reasons why render() may be called, and it might not correspond with the screen being updated.

from textual.

nschloe avatar nschloe commented on May 21, 2024

I see.

My problem here is that constructing the Panel is expensive, so I really only want it to happen every 5 seconds. I could outsource that into another function and set_interval(5.0, self.collect_data_and_construct_panel).
Unfortunately, the panel construction also depends on the panel size which is not available on_mount().

Perhaps, instead of this (which only shows something meaningful after the first cycle)

def on_mount(self):
    self.width_height = None
    # self.collect_data_and_construct_panel() fails because self.width_height is not available yet
    self.panel = "empty"
    self.set_interval(5.0, self.collect_data_and_construct_panel)

def render(self):
    return self.panel

async def on_resize(self, event):
    self.width_height = (event.width, event.height)

def collect_data_and_construct_panel(self):
    # use self.width, self.height
    self.panel = "fancy panel"[:self.width]

I could do

def on_mount(self):
    self.is_first_render = True
    self.set_interval(5.0, self.collect_data_and_construct_panel)

def render(self):
    if self.is_first_render:
        self.collect_data_and_construct_panel()  # self.width_heigh available
        self.is_first_render = False
    return self.panel

# ...

from textual.

willmcgugan avatar willmcgugan commented on May 21, 2024

https://github.com/Textualize/textual/wiki/Sorry-we-closed-your-issue

from textual.

github-actions avatar github-actions commented on May 21, 2024

Did we solve your problem?

Glad we could help!

from textual.

nschloe avatar nschloe commented on May 21, 2024

Yup, works better now. MWE for textual 0.2:

from textual.app import App
from textual.widget import Widget

from rich import box


class RenderCounter(Widget):
    def on_mount(self):
        self.k = [0]
        self.set_interval(2.0, self.refresh)

    def render(self):
        self.k.append(self.k[-1] + 1)
        return " ".join([str(item) for item in self.k])


class CounterApp(App):
    CSS = """.box {
        height: 1fr;
        border: solid green;
    }
    """
    def compose(self):
        yield RenderCounter(classes="box")
        yield RenderCounter(classes="box")
        yield RenderCounter(classes="box")
        yield RenderCounter(classes="box")
        yield RenderCounter(classes="box")


CounterApp().run()

screenshot

from textual.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.