Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting theme doesn't work a second time #2583

Closed
ankraft opened this issue May 16, 2023 · 6 comments · Fixed by #2585
Closed

Setting theme doesn't work a second time #2583

ankraft opened this issue May 16, 2023 · 6 comments · Fixed by #2585

Comments

@ankraft
Copy link

ankraft commented May 16, 2023

This is a rather strange one. I have a textual app that may be run multiple times from a parent app (all in the same Python runtime), but not in parallel.
What I am doing is to instantiate a new App instance every time and run() it. Now, when I set the theme mode (self.dark = False) in the __init()__ method, from the second instantiation on the theme doesn't change. However, if I assign to self.dark in the compose() method everything works fine.

The example code:

from textual.app import App, ComposeResult
from textual.widgets import Static, Footer

class TestApp(App):

    BINDINGS = [
        ("q", "quit", "Quit"),
    ]

    def __init__(self) -> None:
        super().__init__()
        self.dark = False   # This doesn't work the second time
    
    def compose(self) -> ComposeResult:
        # self.dark = False     # This works fine
        yield Static('Hello, world!')
        yield Footer()

    def action_quit(self) -> None:
        self.exit()

if __name__ == "__main__":

    # First run
    app = TestApp()
    app.run()

    # Second run
    app = TestApp()
    app.run()
@TomJGooding
Copy link
Contributor

TomJGooding commented May 16, 2023

App.dark is a class attribute set outside of init and shared by all instances. I confess I can't explain exactly what is happening here with your Textual apps, but essentially the issue is that you have a conflict with two attributes with the same name. It looks like you've already found that setting the theme outside of init works as expected?

class Test:
    dark = True

    def __init__(self) -> None:
        self.dark = False


test = Test()
print(test.dark)  # False
print(Test.dark)  # True

@Textualize Textualize deleted a comment from github-actions bot May 16, 2023
@willmcgugan
Copy link
Collaborator

dark is actually a descriptor, so it should behave like an instance attribute.

Generally I'd recommend setting reactives in a message handler. If you set dark to False in on_mount everything works as expected.

That said, I don't have an explanation for the behaviour in the issue. Does look like something is being associated with the class rather than the instance.

@ankraft
Copy link
Author

ankraft commented May 16, 2023

@TomJGooding , @willmcgugan Thanks for the explanations.
Even if it is a class attribute it is not clear (at least to me) why the behaviour is different between the first and the second instances. I moved the dark setting to compose(), and this works fine, though I am not happy to do the assignment there.

@TomJGooding
Copy link
Contributor

Sorry ankraft if I've mislead you - Will seems to have put me straight and already opened a PR for this.

@ankraft
Copy link
Author

ankraft commented May 16, 2023

@TomJGooding No problem. Thanks for taking the time. And it's great to see a PR so quickly!

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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 a pull request may close this issue.

3 participants