Classes:
Functions:
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 stdin_callback
(for example) as long as persistent_stream_redirection
is False
.
globals (dict
[str
, Any
] | None
) – The global namespace in which to evaluate the code. Defaults to a new empty dictionary.
stdin_callback (Callable
[[int
], str
] | None
) – Function to call at each read from sys.stdin
. Defaults to None
.
stdout_callback (Callable
[[str
], None
] | None
) – Function to call at each write to sys.stdout
. Defaults to None
.
stderr_callback (Callable
[[str
], None
] | None
) – Function to call at each write to sys.stderr
. Defaults to None
.
persistent_stream_redirection (bool
) – Should redirection of standard streams be kept between calls to runcode()
? Defaults to False
.
filename (str
) – The file name to report in error messages. Defaults to "<console>"
.
dont_inherit (bool
) – Whether to inherit __future__
imports from the outer code. See the documentation for the built-in compile()
function.
optimize (int
) – Specifies the optimization level of the compiler. See the documentation for the built-in compile()
function.
list
[str
]#
The list of lines of code that have been the argument to push()
.
This is emptied whenever the code is executed.
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
.
source (str
) – The source string to complete at the end.
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)
str
#
The set of characters considered by complete()
to be word breaks.
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
.
Format the exception that just occurred.
The actual error object is stored into sys.last_value
.
e (BaseException
)
dict
[str
, Any
]#
The namespace used as the globals
Restore stdin
/stdout
/stdout
if they have been persistently redirected
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.
line (str
)
A context manager to redirect standard streams.
This supports nesting.
Execute a code object and return the result.
source (str
)
code (CodeRunner
)
Compile and run source code in the interpreter.
Callable
[[str
], int
| None
] | None
#
Function to call at each write to sys.stderr
.
Callable
[[int
], str
] | None
#
The function to call at each read from sys.stdin
Callable
[[str
], int
| None
] | None
#
Function to call at each write to sys.stdout
.
A future with extra fields used as the return value for Console
apis.
syntax_check (Literal
['incomplete'
, 'syntax-error'
, 'complete'
])
str
| None
#
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.
Literal
['incomplete'
, 'syntax-error'
, 'complete'
]#
The status of the future. The values mean the following:
Input is incomplete. The future has already been resolved with result None
.
Input contained a syntax error. The future has been rejected with a SyntaxError
.
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.
A subclass of Console
that uses pyodide.loadPackagesFromImports()
before running the code.
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'
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4