prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.11k stars 718 forks source link

Create Layout that does not contain any Window object #1257

Open plotski opened 3 years ago

plotski commented 3 years ago

I want to initialize my layout with an empty HSplit. Children are added and removed later while the application is running. The first child is not available when I create the Layout.

>>> from prompt_toolkit.layout import Layout
>>> from prompt_toolkit.layout.containers import HSplit
>>> Layout(HSplit(children=[]))
Traceback (most recent call last):
  File "/home/ich/code/upsies/venv/lib/python3.8/site-packages/prompt_toolkit/layout/layout.py", line 61, in __init__
    self._stack.append(next(self.find_all_windows()))
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ich/code/upsies/venv/lib/python3.8/site-packages/prompt_toolkit/layout/layout.py", line 63, in __init__
    raise InvalidLayoutError(
prompt_toolkit.layout.layout.InvalidLayoutError: Invalid layout. The layout does not contain any Window object.

I tried replacing the exception with a pass in https://github.com/prompt-toolkit/python-prompt-toolkit/blob/10f5cca40f40f5dbda4c521ba72270b23f72628c/prompt_toolkit/layout/layout.py#L63 and the tests were passing, but then the current_control property broke. It looks like non-emptiness is built into the guts of the Layout class.

I could add an empty window, but that adds an empty line. I could remove that window when I've added the first real widget, but that's not very elegant or obvious why that's necessary.

bobhy commented 3 years ago

Try wrapping the window in a ConditionalContainer that is initially hidden. This will not take up any height in the layout. See prompt-toolkit\shortcuts\prompt.py for an example. Maybe even use this approach for all your windows, if there's a predictable number of them?

plotski commented 3 years ago

Hey, thank you for your reply.

But this doesn't work because there are no Windows initially when the Layout is created. The contents of the main HSplit are created dynamically while the application is running.