| [ Web Proxy ] |
| Viewing: https://pyodide.org/en/stable/usage/api/python-api/../python-api/../../api/python-api/console.html | [Back] [Original] |
Classes:
|
Interactive Pyodide console |
|
A future with extra fields used as the return value for |
|
A subclass of |
Functions:
|
Compute the string representation of |
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 (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 from sys.stdin. Defaults to None.
stdout_callback (Optional[Callable[[str], None]]) Function to call at each write to sys.stdout. Defaults to None.
stderr_callback (Optional[Callable[[str], 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 Pythons 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 doesnt include a stack trace because there isnt 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)
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 interpreters
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.
A future with extra fields used as the return value for Console APIs.
Example
>>> from pyodide.console import Console
>>> console = Console()
>>> future = console.push("print('Hello, World!')")
>>> print(future.syntax_check)
complete
>>> import asyncio
>>> result = asyncio.run(future)
Hello, World!
syntax_check (Literal['incomplete', 'syntax-error', 'complete'])
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.
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.
Example
>>> from pyodide.console import PyodideConsole
>>> console = PyodideConsole()
>>> # This will automatically load numpy before execution
>>> future = console.push("import numpy as np; print(np.array([1, 2, 3]))")
>>> print(future.syntax_check)
complete
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'
| Web Proxy Viewer | New URL | Original Page |