Skip to content

Commit 001e9a0

Browse files
author
Bob Hyman
committed
Modify ThreadedHistory example(s) for new API
1 parent 1c3056d commit 001e9a0

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

examples/prompts/history/slow-history.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
without blocking any user interaction.
77
"""
88
import time
9+
import asyncio
910

1011
from prompt_toolkit import PromptSession
1112
from prompt_toolkit.history import History, ThreadedHistory
@@ -32,11 +33,22 @@ def main():
3233
"Even when the input is accepted, loading will continue in the "
3334
"background and when the next prompt is displayed.\n"
3435
)
35-
our_history = ThreadedHistory(SlowHistory())
36+
37+
my_loop = asyncio.get_event_loop() # creates loop if needed
38+
39+
# Inform ThreadedHistory which event loop to use
40+
# when passing lines of history to the prompt.
41+
42+
our_history = ThreadedHistory(SlowHistory(), my_loop)
3643

3744
# The history needs to be passed to the `PromptSession`. It can't be passed
3845
# to the `prompt` call because only one history can be used during a
3946
# session.
47+
# Note that PromptSession runs on the thread's current event loop because
48+
# it was created above and is therefore in synch with ThreadedHistory.
49+
# PromptSession would create and event loop if it didn't find one
50+
# already running, but then ThreadedHistory would not work.
51+
4052
session = PromptSession(history=our_history)
4153

4254
while True:

prompt_toolkit/history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class ThreadedHistory(History):
103103
"""
104104

105105
def __init__(
106-
self, history: History, event_loop: asyncio.BaseEventLoop = None
106+
self, history: History, event_loop: Optional[asyncio.BaseEventLoop] = None
107107
) -> None:
108108
"""Create instance of ThreadedHistory
109109

prompt_toolkit/shortcuts/prompt.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,7 @@ def display_placeholder() -> bool:
556556
# Users can insert processors here.
557557
DynamicProcessor(lambda: merge_processors(self.input_processors or [])),
558558
ConditionalProcessor(
559-
AfterInput(lambda: self.placeholder),
560-
filter=display_placeholder,
559+
AfterInput(lambda: self.placeholder), filter=display_placeholder,
561560
),
562561
]
563562

0 commit comments

Comments
 (0)