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