question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic View and multiple Windows per App

See original GitHub issue

Related to #10 but hopefully this issue can serve as a more general discussion about handling Apps with multiple Windows.

The first issue I have encountered is that a View cannot contain exclusively Window components. See the snippet in #10 for which the App is not terminated. I think it’s because the App is creating a window for its children, however when these are Windows themselves then the root window is “invisible” and can’t be closed. Adding any other component to the View resolves this:

import edifice as ed


class MyApp(ed.Component):
    def render(self):
        return ed.View()(
            ed.Label("Hello"),
            ed.Window()(ed.Label("Hello")),
        )


if __name__ == "__main__":
    ed.App(MyApp()).start()

The second thing is that I am not quite sure how to go about dynamically managing windows. An example of spawning a window from a button click would be interesting (if it is possible). Then there is also the possibility of dynamic Views where some components could be invisible depending on a conditional…

Lastly, the snippet above creates two windows (one for MyApp, implicitly, and one for the second Label). They can be closed independently. In many cases, it would be desirable to assign one window as the root or parent window. When closed, it should close all of it’s children and terminate the App.

As time permits, I’ll dig into thee implementations some more and hopefully I can help in some way (even if it’s just testing or documentation).

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
fdingcommented, May 3, 2021

Here’s an example of how to create new windows on the click of a button, and how to close all windows once the main window is closed:

import edifice as ed

class MyApp(ed.Component):
    def __init__(self):
        super().__init__()
        self.spawned_windows = set()
        self.counter = 0
        self.main_window_open = True

    def create_window(self, e):
        with self.render_changes():
            self.spawned_windows = self.spawned_windows | {self.counter}
            self.counter += 1

    def close_window(self, i, e):
        with self.render_changes():
            self.spawned_windows = self.spawned_windows - {i}

    def close_main_window(self, e):
        with self.render_changes():
            self.spawned_windows = set()
            self.main_window_open = False

    def render(self):
        return ed.List()(
            self.main_window_open and ed.Window(on_close=self.close_main_window)(
                ed.Button("New window", on_click=self.create_window),
            ),
            *[
                ed.Window(on_close=lambda e, idx=i: self.close_window(idx, e))(
                    ed.Label(f"Window {i + 1}"),
                ).set_key(str(i))
                for i in self.spawned_windows
            ],
        )

if __name__ == "__main__":
    ed.App(MyApp()).start()

It uses the List component as you noted, which holds a list of subcomponents without mounting them into any UI element. Closing a window is done the same way you would hide an UI element, i.e. by not returning that window in the render function.

I think this solution fits well with the design of the rest of the library. However, the way the library handles windows isn’t perfect: when the user closes a window that does not have an on_close event defined, the window will disappear, but the internal state will not register that the window is gone. This means that next time render is called, the window will be resurrected. I’m not sure about the best way of handling this inconsistency. The simplest way is to block the usual window closing mechanism, and only close a window when it’s not rendered by anything. But I think this is bad default behavior and would result in overhead for almost all situations. The current version will get all the simple cases right, but could result in inconsistencies with multiple windows if the user isn’t careful. Do you have any thoughts on how this might be better designed?

0reactions
adigitoleocommented, Jul 29, 2021

Just a heads up that I’m too busy to really look at this, and my original use-case isn’t going to happen in the form that I planned. Feel free to close this, or you can leave it open in case there are any takers. I’ll definitely recommend that people check out this package if I find anyone looking for simple/reactive UI in python 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web-Based Multi-Screen Apps Including Drag & Drop - Medium
Imagine an app running on 2 screens (one fullscreen browser window on each one) and you can create an in-app dialog and drag...
Read more >
Managing data with dynamic views ‒ Qlik Sense on Windows
Multiple dynamic views can use the same template app. Each dynamic view is refreshed individually. If bind expressions are used in a dynamic...
Read more >
About App-V dynamic configuration - Windows - Microsoft Learn
Learn how to create or edit an existing Application Virtualization (App-V) dynamic configuration file.
Read more >
Dynamic View - Smartsheet Learning Center
Smartsheet Dynamic View enables business owners to share information to the right people at the right time for viewing or editing.
Read more >
Multi-window support - Android Developers
You can configure how your app handles multi-window mode by specifying your activity's minimum allowable dimensions. You can also disable multi-window mode for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found