### Simple prompt example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/getting_started.html A basic example demonstrating how to use the prompt() function to get user input. ```python from prompt_toolkit import prompt text = prompt("Give me some input: ") print(f"You said: {text}") ``` -------------------------------- ### pip installation Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/getting_started.html Install prompt_toolkit using pip. ```bash pip install prompt_toolkit ``` -------------------------------- ### Conda installation Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/getting_started.html Install prompt_toolkit using Conda. ```bash conda install -c https://conda.anaconda.org/conda-forge prompt_toolkit ``` -------------------------------- ### Create Background Task Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of starting a background task for the running application. ```python create_background_task(_coroutine : Coroutine[Any, Any, None]_) ``` -------------------------------- ### PromptSession Basic Usage Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html A simple example demonstrating how to create a PromptSession and get user input. ```python s = PromptSession(message='>') text = s.prompt() ``` -------------------------------- ### Basic Layout Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/full_screen_apps.html An example of a layout that displays the content of the default buffer on the left, and displays "Hello world" on the right. In between it shows a vertical line. ```python from prompt_toolkit import Application from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import VSplit, Window from prompt_toolkit.layout.controls import BufferControl, FormattedTextControl from prompt_toolkit.layout.layout import Layout buffer1 = Buffer() # Editable buffer. root_container = VSplit([ # One window that holds the BufferControl with the default buffer on # the left. Window(content=BufferControl(buffer=buffer1)), # A vertical line in the middle. We explicitly specify the width, to # make sure that the layout engine will not try to divide the whole # width by three for all these windows. The window will simply fill its # content by repeating this character. Window(width=1, char='|'), # Display the text 'Hello world' on the right. Window(content=FormattedTextControl(text='Hello world')), ]) layout = Layout(root_container) app = Application(layout=layout, full_screen=True) app.run() # You won't be able to Exit this app ``` -------------------------------- ### Create Pipe Input Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example demonstrating how to create and use an input pipe, primarily for unit testing. ```python with create_pipe_input() as input: input.send_text('inputdata') ``` -------------------------------- ### Raw Mode Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example demonstrating the usage of raw_mode context manager for pseudo-terminal input. ```python with raw_mode(stdin): ''' the pseudo-terminal stdin is now used in raw mode ''' ``` -------------------------------- ### Typical usage Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to instantiate and use the Renderer class. ```python output = Vt100_Output.from_pty(sys.stdout) r = Renderer(style, output) r.render(app, layout=...) ``` -------------------------------- ### Style Class Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of creating a Style instance from a list of style rules. ```python Style([ ('title', '#ff0000 bold underline'), ('something-else', 'reverse'), ('class1 class2', 'reverse'), ]) ``` -------------------------------- ### Application Run Async Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to run the application asynchronously. ```python await app.run_async() ``` -------------------------------- ### Basic choice input Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_a_choice.html A simple example demonstrating the `choice()` function to get a selection from a list of options. ```python from prompt_toolkit.shortcuts import choice result = choice( message="Please choose a dish:", options=[ ("pizza", "Pizza with mushrooms"), ("salad", "Salad with tomatoes"), ("sushi", "Sushi"), ], default="salad", ) print(f"You have chosen: {result}") ``` -------------------------------- ### HTML formatted text examples Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Examples of creating HTML objects for styling text. ```python HTML('') ``` ```python HTML('...') ``` ```python HTML('...') ``` ```python HTML('...') ``` ```python HTML('...') ``` -------------------------------- ### PygmentsLexer Style Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of loading a Pygments compatible style. ```python from prompt_toolkit.styles.from_pygments import style_from_pygments_cls from pygments.styles import get_style_by_name style = style_from_pygments_cls(get_style_by_name('monokai')) ``` -------------------------------- ### Send keys into the processor. Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html This example demonstrates how to feed key presses into the processor and then process them. ```python p.feed(KeyPress(Keys.ControlX, '')) p.feed(KeyPress(Keys.ControlC, '')) p.process_keys() ``` -------------------------------- ### Vt100Parser Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to use the Vt100Parser to parse input streams and receive KeyPress objects. ```python def callback(key): pass i = Vt100Parser(callback) i.feed('data...') ``` -------------------------------- ### VSplit Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to create a VSplit with padding and a padding character to create visual separators between child containers. ```python VSplit(children=[ ... ], padding_char='|', padding=1, padding_style='#ffff00') ``` -------------------------------- ### Custom Completer Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_input.html An example of creating a custom completer by subclassing the Completer class and implementing the get_completions method. ```python from prompt_toolkit import prompt from prompt_toolkit.completion import Completer, Completion class MyCustomCompleter(Completer): def get_completions(self, document, complete_event): yield Completion("completion", start_position=0) text = prompt("> ", completer=MyCustomCompleter()) ``` -------------------------------- ### ProgressBar Usage Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to use the ProgressBar context manager for displaying progress. ```python with ProgressBar(...) as pb: for item in pb(data): ... ``` -------------------------------- ### Focusing a Window Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/full_screen_apps.html An example of how to focus a window using the `focus()` method. ```python from prompt_toolkit.application import get_app # This window was created earlier. w = Window() # ... # Now focus it. get_app().layout.focus(w) ``` -------------------------------- ### ConditionalKeyBindings Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of using ConditionalKeyBindings to enable/disable key bindings based on a condition. ```python @Condition def setting_is_true(): return True # or False registry = ConditionalKeyBindings(key_bindings, setting_is_true) ``` -------------------------------- ### HSplit Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to create an HSplit with padding and a padding character to create visual separators between child containers. ```python HSplit(children=[ ... ], padding_char='-', padding=1, padding_style='#ffff00') ``` -------------------------------- ### Application Usage Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Basic usage of the Application class. ```python app = Application(...) app.run() ``` -------------------------------- ### Choice selection prompt example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example usage of the choice function to ask the user to select an option from a list. ```python result = choice( message="Please select a dish:", options=[ ("pizza", "Pizza with mushrooms"), ("salad", "Salad with tomatoes"), ("sushi", "Sushi"), ], default="pizza", ) ``` -------------------------------- ### Right prompt example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_input.html Example of adding a right prompt to the prompt using a callable and a custom style. ```python from prompt_toolkit import prompt from prompt_toolkit.styles import Style example_style = Style.from_dict({ "rprompt": "bg:#ff0066 #ffffff", }) def get_rprompt(): return "" answer = prompt("> ", rprompt=get_rprompt, style=example_style) ``` -------------------------------- ### Run Application Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of a blocking run call for the application. ```python run(_pre_run : Callable[[], None] | None = None_, _set_exception_handler : bool = True_, _handle_sigint : bool = True_, _in_thread : bool = False_, _inputhook : Callable[[InputHookContext], None] | None = None_) ``` -------------------------------- ### BufferControl Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/advanced_topics/rendering_flow.html This code snippet demonstrates the creation of a Buffer and a Window containing a BufferControl. ```python from prompt_toolkit.enums import DEFAULT_BUFFER from prompt_toolkit.layout.containers import Window from prompt_toolkit.layout.controls import BufferControl from prompt_toolkit.buffer import Buffer b = Buffer(name=DEFAULT_BUFFER) Window(content=BufferControl(buffer=b)) ``` -------------------------------- ### Styling of Dialogs Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/dialogs.html Example demonstrating how to apply custom styling and HTML formatting to dialogs. ```python from prompt_toolkit.formatted_text import HTML from prompt_toolkit.shortcuts import message_dialog from prompt_toolkit.styles import Style example_style = Style.from_dict({ 'dialog': 'bg:#88ff88', 'dialog frame.label': 'bg:#ffffff #000000', 'dialog.body': 'bg:#000000 #00ff00', 'dialog shadow': 'bg:#00aa00', }) message_dialog( title=HTML(' ', ' window'), text='Do you want to continue?\nPress ENTER to quit.', style=example_style).run() ``` -------------------------------- ### Vi input mode Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_input.html Example of enabling Vi input mode for the prompt. ```python from prompt_toolkit import prompt prompt("> ", vi_mode=True) ``` -------------------------------- ### Style from Pygments class Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of creating a Style instance from a Pygments style class and a style dictionary. ```python from prompt_toolkit.styles.from_pygments import style_from_pygments_cls from pygments.styles import get_style_by_name style = style_from_pygments_cls(get_style_by_name('monokai')) ``` -------------------------------- ### Global Key Bindings Setup Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/full_screen_apps.html Demonstrates how to set up global key bindings for an application. ```python from prompt_toolkit import Application from prompt_toolkit.key_binding import KeyBindings kb = KeyBindings() app = Application(key_bindings=kb) app.run() ``` -------------------------------- ### Simplest full screen example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/full_screen_apps.html This code demonstrates the most basic full-screen application structure using prompt_toolkit. ```python from prompt_toolkit import Application app = Application(full_screen=True) app.run() ``` -------------------------------- ### Input Box Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/dialogs.html Example of displaying an input box using `input_dialog()` to get user input. ```python from prompt_toolkit.shortcuts import input_dialog text = input_dialog( title='Input dialog example', text='Please type your name:').run() ``` -------------------------------- ### PygmentsLexer Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to use PygmentsLexer with HtmlLexer. ```python from pygments.lexers.html import HtmlLexer lexer = PygmentsLexer(HtmlLexer) ``` -------------------------------- ### ConditionalProcessor Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of using ConditionalProcessor within a BufferControl. ```python BufferControl(input_processors=[ ConditionalProcessor(HighlightSearchProcessor(), Condition(highlight_enabled))]) ``` -------------------------------- ### Using an AppSession Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/advanced_topics/unit_testing.html This example shows how to use `create_app_session` to set the input and output for all applications within a context, avoiding the need to pass them individually. ```python from prompt_toolkit.application import create_app_session from prompt_toolkit.shortcuts import print_formatted_text from prompt_toolkit.output import DummyOutput def test_something(): with create_app_session(output=DummyOutput()): ... print_formatted_text('Hello world') ... ``` -------------------------------- ### Validator.from_callable example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of creating a validator from a simple callable function. ```python def is_valid(text): return text in ['hello', 'world'] Validator.from_callable(is_valid, error_message='Invalid input') ``` -------------------------------- ### Basic REPL with Auto-completion Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/tutorials/repl.html This code snippet shows how to set up a basic REPL with SQL keyword auto-completion and syntax highlighting using `WordCompleter`, `PygmentsLexer`, and `PromptSession`. ```python from prompt_toolkit import PromptSession from prompt_toolkit.completion import WordCompleter from prompt_toolkit.lexers import PygmentsLexer from pygments.lexers.sql import SqlLexer sql_completer = WordCompleter([ 'abort', 'action', 'add', 'after', 'all', 'alter', 'analyze', 'and', 'as', 'asc', 'attach', 'autoincrement', 'before', 'begin', 'between', 'by', 'cascade', 'case', 'cast', 'check', 'collate', 'column', 'commit', 'conflict', 'constraint', 'create', 'cross', 'current_date', 'current_time', 'current_timestamp', 'database', 'default', 'deferrable', 'deferred', 'delete', 'desc', 'detach', 'distinct', 'drop', 'each', 'else', 'end', 'escape', 'except', 'exclusive', 'exists', 'explain', 'fail', 'for', 'foreign', 'from', 'full', 'glob', 'group', 'having', 'if', 'ignore', 'immediate', 'in', 'index', 'indexed', 'initially', 'inner', 'insert', 'instead', 'intersect', 'into', 'is', 'isnull', 'join', 'key', 'left', 'like', 'limit', 'match', 'natural', 'no', 'not', 'notnull', 'null', 'of', 'offset', 'on', 'or', 'order', 'outer', 'plan', 'pragma', 'primary', 'query', 'raise', 'recursive', 'references', 'regexp', 'reindex', 'release', 'rename', 'replace', 'restrict', 'right', 'rollback', 'row', 'savepoint', 'select', 'set', 'table', 'temp', 'temporary', 'then', 'to', 'transaction', 'trigger', 'union', 'unique', 'update', 'using', 'vacuum', 'values', 'view', 'virtual', 'when', 'where', 'with', 'without'], ignore_case=True) def main(): session = PromptSession( lexer=PygmentsLexer(SqlLexer), completer=sql_completer) while True: try: text = session.prompt('> ') except KeyboardInterrupt: continue except EOFError: break else: print('You entered:', text) print('GoodBye!') if __name__ == '__main__': main() ``` -------------------------------- ### Read User Input Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/tutorials/repl.html Basic example of accepting user input using the prompt() function. ```python from prompt_toolkit import prompt def main(): text = prompt('> ') print('You entered:', text) if __name__ == '__main__': main() ``` -------------------------------- ### ConditionalProcessor Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of using ConditionalProcessor to apply another processor based on a condition. ```python # Create a function that returns whether or not the processor should # currently be applied. def highlight_enabled(): return true_or_false ``` -------------------------------- ### NestedCompleter from_nested_dict Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of creating a NestedCompleter from a nested dictionary data structure. ```python data = { 'show': { 'version': None, 'interfaces': None, 'clock': None, 'ip': {'interface': {'brief'}} }, 'exit': None 'enable': None } ``` -------------------------------- ### Hello world Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_input.html The most simple example using the `prompt()` function to ask the user for input and return the text, similar to `(raw_)input`. ```python from prompt_toolkit import prompt text = prompt("Give me some input: ") print(f"You said: {text}") ``` -------------------------------- ### Transform Lines Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of how to use the transform_lines function to uppercase specific lines. ```python new_text = transform_lines(range(5,10), lambda text: text.upper()) ``` -------------------------------- ### Syntax Highlighting Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/tutorials/repl.html Add syntax highlighting for SQL input using PygmentsLexer and SqlLexer. ```python from prompt_toolkit import PromptSession from prompt_toolkit.lexers import PygmentsLexer from pygments.lexers.sql import SqlLexer def main(): session = PromptSession(lexer=PygmentsLexer(SqlLexer)) while True: try: text = session.prompt('> ') except KeyboardInterrupt: continue except EOFError: break else: print('You entered:', text) print('GoodBye!') if __name__ == '__main__': main() ``` -------------------------------- ### KeyBindings Basic Usage Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Demonstrates how to create a KeyBindings instance and add key bindings with different handlers and filters. ```python kb = KeyBindings() @kb.add('c-t') def _(event): print('Control-T pressed') @kb.add('c-a', 'c-b') def _(event): print('Control-A pressed, followed by Control-B') @kb.add('c-x', filter=is_searching) def _(event): print('Control-X pressed') # Works only if we are searching. ``` -------------------------------- ### REPL with Styled Completion Menu Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/tutorials/repl.html This code snippet demonstrates how to style the completion menu in a REPL by creating a `Style` instance and passing it to the `PromptSession`. ```python from prompt_toolkit import PromptSession from prompt_toolkit.completion import WordCompleter from prompt_toolkit.lexers import PygmentsLexer from prompt_toolkit.styles import Style from pygments.lexers.sql import SqlLexer sql_completer = WordCompleter([ 'abort', 'action', 'add', 'after', 'all', 'alter', 'analyze', 'and', 'as', 'asc', 'attach', 'autoincrement', 'before', 'begin', 'between', 'by', 'cascade', 'case', 'cast', 'check', 'collate', 'column', 'commit', 'conflict', 'constraint', 'create', 'cross', 'current_date', 'current_time', 'current_timestamp', 'database', 'default', 'deferrable', 'deferred', 'delete', 'desc', 'detach', 'distinct', 'drop', 'each', 'else', 'end', 'escape', 'except', 'exclusive', 'exists', 'explain', 'fail', 'for', 'foreign', 'from', 'full', 'glob', 'group', 'having', 'if', 'ignore', 'immediate', 'in', 'index', 'indexed', 'initially', 'inner', 'insert', 'instead', 'intersect', 'into', 'is', 'isnull', 'join', 'key', 'left', 'like', 'limit', 'match', 'natural', 'no', 'not', 'notnull', 'null', 'of', 'offset', 'on', 'or', 'order', 'outer', 'plan', 'pragma', 'primary', 'query', 'raise', 'recursive', 'references', 'regexp', 'reindex', 'release', 'rename', 'replace', 'restrict', 'right', 'rollback', 'row', 'savepoint', 'select', 'set', 'table', 'temp', 'temporary', 'then', 'to', 'transaction', 'trigger', 'union', 'unique', 'update', 'using', 'vacuum', 'values', 'view', 'virtual', 'when', 'where', 'with', 'without'], ignore_case=True) style = Style.from_dict({ 'completion-menu.completion': 'bg:#008888 #ffffff', 'completion-menu.completion.current': 'bg:#00aaaa #000000', 'scrollbar.background': 'bg:#88aaaa', 'scrollbar.button': 'bg:#222222', }) def main(): session = PromptSession( lexer=PygmentsLexer(SqlLexer), completer=sql_completer, style=style) while True: try: text = session.prompt('> ') except KeyboardInterrupt: continue except EOFError: break else: print('You entered:', text) print('GoodBye!') if __name__ == '__main__': main() ``` -------------------------------- ### Cooked Mode Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example showing the usage of cooked_mode context manager for pseudo-terminal input. ```python with cooked_mode(stdin): ''' the pseudo-terminal stdin is now used in cooked mode. ''' ``` -------------------------------- ### FloatContainer Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example usage of the FloatContainer class, demonstrating how to create a container with content and floats. ```python FloatContainer(content=Window(...), floats=[ Float(xcursor=True, ycursor=True, content=CompletionsMenu(...)) ]) ``` -------------------------------- ### Default key bindings Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Load default key bindings and create an Application. ```python key_bindings = load_key_bindings() app = Application(key_bindings=key_bindings) ``` -------------------------------- ### PosixPipeInput and DummyOutput Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/advanced_topics/unit_testing.html This example demonstrates how to use `create_pipe_input` and `DummyOutput` to unit test a `PromptSession` by programmatically sending input and capturing output. ```python from prompt_toolkit.shortcuts import PromptSession from prompt_toolkit.input import create_pipe_input from prompt_toolkit.output import DummyOutput def test_prompt_session(): with create_pipe_input() as inp: inp.send_text("hello\n") session = PromptSession( input=inp, output=DummyOutput(), ) result = session.prompt() assert result == "hello" ``` -------------------------------- ### Custom key bindings Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/asking_for_input.html Example of adding custom key bindings to the prompt, including a binding to print 'hello world' and another to exit. ```python from prompt_toolkit import prompt from prompt_toolkit.application import run_in_terminal from prompt_toolkit.key_binding import KeyBindings bindings = KeyBindings() @bindings.add("c-t") def _(event): """ Say "hello" when `c-t` is pressed. """ def print_hello(): print("hello world") run_in_terminal(print_hello) @bindings.add("c-x") def _(event): """ Exit when `c-x` is pressed. """ event.app.exit() text = prompt("> ", key_bindings=bindings) print(f"You said: {text}") ``` -------------------------------- ### CheckboxList Dialog Customization Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/dialogs.html An example demonstrating how to customize the appearance of a checkbox list dialog using `Style.from_dict` to define styles for various components like dialog, button, checkbox, and labels. ```python from prompt_toolkit.shortcuts import checkboxlist_dialog from prompt_toolkit.styles import Style results = checkboxlist_dialog( title="CheckboxList dialog", text="What would you like in your breakfast ?", values=[ ("eggs", "Eggs"), ("bacon", "Bacon"), ("croissants", "20 Croissants"), ("daily", "The breakfast of the day") ], style=Style.from_dict({ 'dialog': 'bg:#cdbbb3', 'button': 'bg:#bf99a4', 'checkbox': '#e8612c', 'dialog.body': 'bg:#a9cfd0', 'dialog shadow': 'bg:#c98982', 'frame.label': '#fcaca3', 'dialog.body label': '#fd8bb6', }) ).run() ``` -------------------------------- ### get_traceback_from_context Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Get the traceback object from the context. ```python prompt_toolkit.eventloop.get_traceback_from_context(_context : dict[str, Any]) -> TracebackType | None ``` -------------------------------- ### Copy Selection Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Demonstrates how to copy selected text from a buffer and store it using the application's clipboard. ```python data = buffer.copy_selection() get_app().clipboard.set_data(data) ``` -------------------------------- ### get_traceback_from_context (utils) Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Get the traceback object from the context. ```python prompt_toolkit.eventloop.utils.get_traceback_from_context(_context : dict[str, Any]) -> TracebackType | None ``` -------------------------------- ### Load History Example Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Illustrates how to load buffer history, noting that this must be called from within the application's event loop due to its asynchronous nature. ```python This needs to be called from within the event loop of the application, because history loading is async, and we need to be sure the right event loop is active. Therefor, we call this method in the `BufferControl.create_content`. There are situations where prompt_toolkit applications are created in one thread, but will later run in a different thread (Ptpython is one example. The REPL runs in a separate thread, in order to prevent interfering with a potential different event loop in the main thread. The REPL UI however is still created in the main thread.) We could decide to not support creating prompt_toolkit objects in one thread and running the application in a different thread, but history loading is the only place where it matters, and this solves it. ``` -------------------------------- ### Prepared Toolbars Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Pre-built toolbar classes for common UI elements. ```text - SystemToolbar (Shows the 'system' input buffer, for entering system commands.) - ArgToolbar (Shows the input 'arg', for repetition of input commands.) - SearchToolbar (Shows the 'search' input buffer, for incremental search.) - CompletionsToolbar (Shows the completions of the current buffer.) - ValidationToolbar (Shows validation errors of the current buffer.) ``` -------------------------------- ### Print Text Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Example of printing formatted text to the output. ```python print_text(_text : AnyFormattedText_, _style : BaseStyle | None = None_) ``` -------------------------------- ### KeyProcessor initialization Source: https://python-prompt-toolkit.readthedocs.io/en/3.0.52/pages/reference.html Initialize a KeyProcessor with key bindings. ```python p = KeyProcessor(key_bindings) ```