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.

Cannot focus DynamicContainer

See original GitHub issue

I would like to have the contents of a container update based on what is focused. I figured that using a DynamicContainer was the best way to achieve this. However, whenever I put a normally focusable widget in a DynamicContainer, it seems to be impossible to focus it. Here’s an example:

bindings = KeyBindings()
bindings.add("c-c")(lambda event: event.app.exit())
bindings.add("tab")(focus_next)
bindings.add("s-tab")(focus_previous)

regular_button = Button("Regular")
dynamic_button = DynamicContainer(lambda: Button("Dynamic"))

layout = Layout(
    HSplit([
        regular_button,
        dynamic_button,
    ]),
)

app = Application(layout=layout, key_bindings=bindings)
app.run()

In this example, I can focus regular_button but not dynamic_button. If I try to focus it using layout.focus(dynamic_button), I get the following stack trace:

Traceback (most recent call last):
  File "/home/garrett/.PyCharm2018.2/config/scratches/scratch_39.py", line 24, in <module>
    layout.focus(dynamic_button)
  File "/home/garrett/.local/share/virtualenvs/skiddie-x-macEj3/lib/python3.5/site-packages/prompt_toolkit/layout/layout.py", line 141, in focus
    raise ValueError('Invalid value. Container cannot be focused: %r' % (value, ))
ValueError: Invalid value. Container cannot be focused: <prompt_toolkit.layout.containers.DynamicContainer object at 0x7f725252e780>

If this is intended behavior, I would really appreciate it if anyone could let me know of another way to achieve what I’m trying to do.

Thanks, Garrett

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jonathanslenderscommented, Aug 13, 2018

Hi @lostatc,

I noticed the problem is on the following line:

dynamic_button = DynamicContainer(lambda: Button("Dynamic"))

This creates every a different button every single time. That doesn’t work because focus_next first tries to enumerate all widgets that are focusable, then checks in that list what the current widget is, and goes to the next one. By that time, this button is already gone. If we would set this specific instance as the focused button, the renderer also wouldn’t know where to find it.

Try to rewrite it as follows:

button = Button("Dynamic")
dynamic_button = DynamicContainer(lambda: button)

Maybe, that’s some thing that should be mentioned in the documentation of DynamicContainer.

0reactions
lostatccommented, Aug 15, 2018

Thanks for all the help @jonathanslenders! That answers my question. I’ll go ahead and close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Buttons in DynamicContainer don't seem to receive focus #1324
I've been trying to use some buttons inside a DynamicContainer, but I can't seem to get the buttons to have focus properly so...
Read more >
Solved: queue a dynamic container type - NI Community
Solved: Hi, I have several threads running, each waiting for an input queue (queue names are numbered by threads: "Queue0",
Read more >
Reference — prompt_toolkit 3.0.36 documentation
Start a background task (coroutine) for the running application. When the Application terminates, unfinished background tasks will be cancelled. Given that we ...
Read more >
Focus Reset and Guided Focus Management
Focus reset happens when an active (currently focused) element, gets removed form the DOM or the render tree. On focus reset, document.
Read more >
Adding a dynamic container to a portal - Pega
Dynamic containers display content in the main pane of the portal. You can define a default view for a container, but if the...
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