prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Read from sys.stdin and then use prompt_toolkit

Open
#1,143 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

Hello! Thank you for prompt_toolkit it's awesome!

Is there a way to read the data from stdin and then open prompt?

Example:

import sys
import pandas as pd
from prompt_toolkit import PromptSession

df = pd.read_json(sys.stdin, lines=True)
print(df)

session = PromptSession()
query = session.prompt('> ')

Expected result:

$ echo '{"a":1}' | python example.py                                                                                                                                                    
   a
0  1
> 

What I've got in prompt_toolkit 3.0.5:

$ echo '{"a":1}' | python example.py 
   a
0  1
Warning: Input is not a terminal (fd=0).
>                                                                                                                                                                                   
Traceback (most recent call last):
  File "example.py", line 11, in <module>
    query = session.prompt('> ')
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/shortcuts/prompt.py", line 994, in prompt
    return self.app.run(set_exception_handler=set_exception_handler)
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 811, in run
    return loop.run_until_complete(
  File "/opt/miniconda/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 778, in run_async
    return await _run_async2()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 760, in _run_async2
    result = await _run_async()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 696, in _run_async
    result = await f
EOFError

There is a workaround to renew stdin with sys.stdin = open("/dev/tty"). In this case I get:

$ echo '{"a":1}' | python example.py 
   a
0  1
Traceback (most recent call last):
  File "example.py", line 12, in <module>
    query = session.prompt('> ')
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/shortcuts/prompt.py", line 994, in prompt
    return self.app.run(set_exception_handler=set_exception_handler)
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 811, in run
    return loop.run_until_complete(
  File "/opt/miniconda/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 778, in run_async
    return await _run_async2()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 760, in _run_async2
    result = await _run_async()
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/application/application.py", line 682, in _run_async
    with self.input.raw_mode():
  File "/opt/miniconda/lib/python3.8/site-packages/prompt_toolkit/input/vt100.py", line 262, in __enter__
    os.write(self.fileno, b"\x1b[?1l")
OSError: [Errno 9] Bad file descriptor

Thanks!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example in example.py, first reading JSON from sys.stdin and then calling PromptSession.prompt. Read the prompt_toolkit application and input/vt100.py paths shown in the traceback to understand how piped input is handled. Done means the piped data is consumed and an interactive prompt opens without EOFError or a bad file descriptor.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.