#πŸ”’ What modules exist for simultaneous input and output?

5 messages Β· Page 1 of 1 (latest)

soft pond
#

Hi. The prompt_toolkit module allows displaying output while simultaneously waiting for input without breaking the input line. Here's an example from the developer:

import asyncio

from prompt_toolkit.patch_stdout import patch_stdout
from prompt_toolkit.shortcuts import PromptSession


async def print_counter():
    """
    Coroutine that prints counters.
    """
    try:
        i = 0
        while True:
            print(f"Counter: {i}")
            i += 1
            await asyncio.sleep(3)
    except asyncio.CancelledError:
        print("Background task cancelled.")


async def interactive_shell():
    """
    Like `interactive_shell`, but doing things manual.
    """
    # Create Prompt.
    session = PromptSession("Say something: ")

    # Run echo loop. Read text from stdin, and reply it back.
    while True:
        try:
            result = await session.prompt_async()
            print(f'You said: "{result}"')
        except (EOFError, KeyboardInterrupt):
            return


async def main():
    with patch_stdout():
        background_task = asyncio.create_task(print_counter())
        try:
            await interactive_shell()
        finally:
            background_task.cancel()
        print("Quitting event loop. Bye.")


if __name__ == "__main__":
    asyncio.run(main()) 

I want to know if there are other modules capable of something similar? So far, I only know this module as the only one capable

barren meteorBOT
#

@soft pond

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

maiden vigil
# soft pond Hi. The prompt_toolkit module allows displaying output while simultaneously wait...

So there are a few other modules that can do something similar, but honestly none of them are as clean as prompt_toolkit for the specific thing you are trying to do. rich has a Live display that lets you update output, but it's really more for showing live updates rather than keeping an interactive prompt going smoothly. It gets the job done but feels a bit clunky. curses is a low-level approach if you want total control over the terminal, but it's a pain to work with and doesn't really play nice with Windows anyway. You'd have to manually handle keeping input and output separate, which is tedious. blessed is basically a nicer wrapper around curses and works better across platforms, but you're still doing a lot of manual work. The learning curve is steeper too. Then there's textual. it's a full TUI framework that's pretty modern and can definitely handle this kind of thing, but it's overkill if all you want is a simple interactive shell with background output. It's more for building whole terminal applications. asyncio with aioconsole is simpler and async-friendly, but it doesn't give you that seamless feel of output appearing without messing up your input line.

Honestly, prompt_toolkit just nails your use case. It was literally built for the exact problem with patch_stdout(). Yeah, the alternatives work, but they either need way more code or just feel more awkward lol

barren meteorBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.