pyodide.console
pyodide.console#
Classes:
|
Interactive Pyodide console |
|
A future with extra fields used as the return value for |
|
A subclass of |
Functions:
|
Compute the string representation of |
- class pyodide.console.Console(globals: Optional[dict[str, Any]] = None, *, stdin_callback: Optional[collections.abc.Callable[[int], str]] = None, stdout_callback: Optional[collections.abc.Callable[[str], None]] = None, stderr_callback: Optional[collections.abc.Callable[[str], None]] = None, persistent_stream_redirection: bool = False, filename: str = '<console>')#
Interactive Pyodide console
An interactive console based on the Python standard library code.InteractiveConsole that manages stream redirections and asynchronous execution of the code.
The stream callbacks can be modified directly as long as persistent_stream_redirection isn’t in effect.
- Parameters
globals (
dict
) – The global namespace in which to evaluate the code. Defaults to a new empty dictionary.stdin_callback (
Callable[[int], str]
) – Function to call at each read fromsys.stdin
. Defaults toNone
.stdout_callback (
Callable[[str], None]
) – Function to call at each write tosys.stdout
. Defaults toNone
.stderr_callback (
Callable[[str], None]
) – Function to call at each write tosys.stderr
. Defaults toNone
.persistent_stream_redirection (
bool
) – Should redirection of standard streams be kept between calls toruncode
? Defaults toFalse
.filename (
str
) – The file name to report in error messages. Defaults to<console>
.
- globals#
The namespace used as the global
- Type
Dict[str, Any]
- stdin_callback#
Function to call at each read from
sys.stdin
.- Type
Callback[[], str]
- stdout_callback#
Function to call at each write to
sys.stdout
.- Type
Callback[[str], None]
- stderr_callback#
Function to call at each write to
sys.stderr
.- Type
Callback[[str], None]
- completer_word_break_characters#
The set of characters considered by
complete
to be word breaks.- Type
str
- complete(source: str) tuple[list[str], int] #
Use Python’s rlcompleter to complete the source string using the
globals
namespace.Finds last “word” in the source string and completes it with rlcompleter. Word breaks are determined by the set of characters in
completer_word_break_characters
.- Parameters
source (str) – The source string to complete at the end.
- Returns
completions (List[str]) – A list of completion strings.
start (int) – The index where completion starts.
Examples
>>> shell = Console() >>> shell.complete("str.isa") (['str.isalnum(', 'str.isalpha(', 'str.isascii('], 0) >>> shell.complete("a = 5 ; str.isa") (['str.isalnum(', 'str.isalpha(', 'str.isascii('], 8)
- formatsyntaxerror(e: Exception) str #
Format the syntax error that just occurred.
This doesn’t include a stack trace because there isn’t one. The actual error object is stored into sys.last_value.
- formattraceback(e: BaseException) str #
Format the exception that just occurred.
The actual error object is stored into sys.last_value.
- persistent_redirect_streams() None #
Redirect stdin/stdout/stderr persistently
- persistent_restore_streams() None #
Restore stdin/stdout/stderr if they have been persistently redirected
- push(line: str) pyodide.console.ConsoleFuture #
Push a line to the interpreter.
The line should not have a trailing newline; it may have internal newlines. The line is appended to a buffer and the interpreter’s runsource() method is called with the concatenated contents of the buffer as source. If this indicates that the command was executed or invalid, the buffer is reset; otherwise, the command is incomplete, and the buffer is left as it was after the line was appended.
The return value is the result of calling
Console.runsource
on the current buffer contents.
- redirect_streams() collections.abc.Generator[None, None, None] #
A context manager to redirect standard streams.
This supports nesting.
- async runcode(source: str, code: _pyodide._base.CodeRunner) Any #
Execute a code object and return the result.
- runsource(source: str, filename: str = '<console>') pyodide.console.ConsoleFuture #
Compile and run source code in the interpreter.
- Returns
- Return type
- class pyodide.console.ConsoleFuture(syntax_check: Literal['incomplete', 'syntax-error', 'complete'])#
A future with extra fields used as the return value for
Console
apis.- syntax_check#
One of
"incomplete"
,"syntax-error"
, or"complete"
. If the value is"incomplete"
then the future has already been resolved with result equal toNone
. If the value is"syntax-error"
, theFuture
has already been rejected with aSyntaxError
. If the value is"complete"
, then the input complete and syntactically correct.- Type
str
- formatted_error#
If the
Future
is rejected, this will be filled with a formatted version of the code. This is a convenience that simplifies code and helps to avoid large memory leaks when using from JavaScript.- Type
str
- class pyodide.console.PyodideConsole(globals: Optional[dict[str, Any]] = None, *, stdin_callback: Optional[collections.abc.Callable[[int], str]] = None, stdout_callback: Optional[collections.abc.Callable[[str], None]] = None, stderr_callback: Optional[collections.abc.Callable[[str], None]] = None, persistent_stream_redirection: bool = False, filename: str = '<console>')#
A subclass of
Console
that usespyodide.loadPackagesFromImports
before running the code.
- pyodide.console.repr_shorten(value: Any, limit: int = 1000, split: Optional[int] = None, separator: str = '...') str #
Compute the string representation of
value
and shorten it if necessary.If it is longer than
limit
then return the firstssplit
characters and the lastsplit
characters separated by ‘…’. Default value forsplit
is limit // 2.