pyodide.console#

Classes:

Console([globals, stdin_callback, ...])

Interactive Pyodide console

ConsoleFuture(syntax_check)

A future with extra fields used as the return value for Console apis.

PyodideConsole([globals, stdin_callback, ...])

A subclass of Console that uses pyodide.loadPackagesFromImports before running the code.

Functions:

repr_shorten(value[, limit, split, separator])

Compute the string representation of value and shorten it if necessary.

class pyodide.console.Console(globals=None, *, stdin_callback=None, stdout_callback=None, stderr_callback=None, persistent_stream_redirection=False, filename='<console>')#

Interactive Pyodide console

An interactive console based on the Python standard library InteractiveConsole that manages stream redirections and asynchronous execution of the code.

The stream callbacks can be modified directly by assigning to Console.stdin_callback (for example) as long as persistent_stream_redirection is False.

Parameters
buffer: list[str]#

The list of strings that have been pushed to the console.

complete(source)#

Use Python’s rlcompleter to complete the source string using the Console.globals namespace.

Finds the 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.

Return type

tuple[list[str], int]

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)
completer_word_break_characters: str#

The set of characters considered by complete to be word breaks.

formatsyntaxerror(e)#

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.

Parameters

e (Exception) –

Return type

str

formattraceback(e)#

Format the exception that just occurred.

The actual error object is stored into sys.last_value.

Parameters

e (BaseException) –

Return type

str

globals: dict[str, Any]#

The namespace used as the globals

persistent_redirect_streams()#

Redirect stdin/stdout/stdout persistently

Return type

None

persistent_restore_streams()#

Restore stdin/stdout/stdout if they have been persistently redirected

Return type

None

push(line)#

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 runsource() on the current buffer contents.

Parameters

line (str) –

Return type

ConsoleFuture

redirect_streams()#

A context manager to redirect standard streams.

This supports nesting.

Return type

Generator[None, None, None]

async runcode(source, code)#

Execute a code object and return the result.

Parameters
Return type

Any

runsource(source, filename='<console>')#

Compile and run source code in the interpreter.

Parameters
  • source (str) –

  • filename (str) –

Return type

ConsoleFuture

stderr_callback: Optional[Callable[[str], None]]#

Function to call at each write to sys.stderr.

stdin_callback: Optional[Callable[[int], str]]#

The function to call at each read from sys.stdin

stdout_callback: Optional[Callable[[str], None]]#

Function to call at each write to sys.stdout.

class pyodide.console.ConsoleFuture(syntax_check)#

A future with extra fields used as the return value for Console apis.

Parameters

syntax_check (Literal['incomplete', 'syntax-error', 'complete']) –

formatted_error: Optional[str]#

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.

syntax_check: Literal['incomplete', 'syntax-error', 'complete']#

The status of the future. The values mean the following:

‘incomplete’

Input is incomplete. The future has already been resolved with result None.

‘syntax-error’

Input contained a syntax error. The future has been rejected with a SyntaxError.

‘complete’

The input complete and syntactically correct and asynchronous execution has begun. When the execution is done, the Future will be resolved with the result or rejected with an exception.

class pyodide.console.PyodideConsole(globals=None, *, stdin_callback=None, stdout_callback=None, stderr_callback=None, persistent_stream_redirection=False, filename='<console>')#

A subclass of Console that uses pyodide.loadPackagesFromImports before running the code.

Parameters
pyodide.console.repr_shorten(value, limit=1000, split=None, separator='...')#

Compute the string representation of value and shorten it if necessary.

This is equivalent to shorten(repr(value), limit, split, separator), but a value error is raised if limit is less than 4.

Examples

>>> from pyodide.console import repr_shorten
>>> sep = "_"
>>> repr_shorten("abcdefg", limit=8, separator=sep)
"'abc_efg'"
>>> repr_shorten("abcdefg", limit=12, separator=sep)
"'abcdefg'"
>>> for i in range(4, 10):
...     repr_shorten(123456789, limit=i, separator=sep)
'12_89'
'12_89'
'123_789'
'123_789'
'1234_6789'
'123456789'
Parameters
Return type

str