Is there an example for this use-case? #1898
Unanswered
lfvjimisola
asked this question in
Q&A
Replies: 2 comments
-
I'm going through issues and can see that my question is mentioned in several issues, e.g. #672 (comment) Is there a best practise solution? Perhaps an example or, if prompt_toolkit does not support it, an entry in the FAQ to help new users? |
Beta Was this translation helpful? Give feedback.
0 replies
-
So, something like this except:
#!/usr/bin/env python
from prompt_toolkit import Application
from prompt_toolkit.application import Application
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout import Layout
from prompt_toolkit.layout.containers import HSplit, VSplit
from prompt_toolkit.widgets import Frame, SearchToolbar, TextArea
help_text = """
Type 'help' for help.
Press Control-C to exit.
"""
# Function to handle user input from the input_area
def handle_input(b: Buffer):
output = f"\nOut: {b.text}\n"
output_area.buffer.insert_text(output)
output_area.buffer.cursor_position = len(output)
# Create TextAreas
output_area = TextArea(
scrollbar=True, text=help_text, read_only=False
)
input_area = TextArea(
height=1,
multiline=False,
wrap_lines=False,
read_only=False,
focusable=True,
accept_handler=handle_input,
)
log_area = TextArea(read_only=True, scrollbar=True, text="Log etc here")
status_area = TextArea(read_only=True, focusable=True, text="Status etc here")
# Layout
layout = VSplit(
[
HSplit(
[
Frame(output_area, title="Output"),
Frame(input_area, title="Shell"),
SearchToolbar(), # Added search toolbar
]
),
HSplit(
[
Frame(log_area, title="Log"),
Frame(status_area, title="Status"),
]
),
]
)
def main():
# Key bindings
kb = KeyBindings()
@kb.add("c-c")
@kb.add("c-q")
def exit_(event):
"Pressing Ctrl-Q or Ctrl-C will exit the user interface."
event.app.exit()
# Create Application
app = Application(
layout=Layout(layout, focused_element=input_area),
key_bindings=kb,
mouse_support=True,
full_screen=True,
)
app.run()
if __name__ == "__main__":
main() |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First of all, really happy to have found this toolkit as it seems to be just what we need.
Want to create a full-screen application that has two other thread (one parses files and the other makes api calls to a server) as well an UI to control/query the application.
We need three windows for the UI:
The challenge seems to be the shell window. We need it to work similar to a bash shell, i.e. with completion, history etc.
I've been looking at so many examples that my head is full/spinning and didn't find one that really matched but perhaps I missed it.
The calculator example is close as a starter but it is has input in one TextArea and output in another while I want it in the same.
Also, is there a way for prompt_toolkit to parse input and provide help for commands (similar to argparse) or what is the recommended way?
Could really use some pointers here or even an MWE to build on.
Thanks in advance.
Jimisola
Beta Was this translation helpful? Give feedback.
All reactions