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=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 aspersistent_stream_redirection
isFalse
.- Parameters
globals (
Optional
[dict
[str
,Any
]]) – The global namespace in which to evaluate the code. Defaults to a new empty dictionary.stdin_callback (
Optional
[Callable
[[int
],str
]]) – Function to call at each read fromsys.stdin
. Defaults toNone
.stdout_callback (
Optional
[Callable
[[str
],None
]]) – Function to call at each write tosys.stdout
. Defaults toNone
.stderr_callback (
Optional
[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>"
.
- complete(source)#
Use Python’s
rlcompleter
to complete the source string using theConsole.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
- Returns
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
.
- formattraceback(e)#
Format the exception that just occurred.
The actual error object is stored into
sys.last_value
.- Parameters
e (
BaseException
) –- Return type
- persistent_restore_streams()#
Restore
stdin
/stdout
/stdout
if they have been persistently redirected- Return type
- 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
- redirect_streams()#
A context manager to redirect standard streams.
This supports nesting.
- async runcode(source, code)#
Execute a code object and return the result.
- Parameters
source (
str
) –code (
CodeRunner
) –
- Return type
- runsource(source, filename='<console>')#
Compile and run source code in the interpreter.
- Parameters
- Return type
- 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 usespyodide.loadPackagesFromImports
before running the code.
- 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 iflimit
is less than4
.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'