PyScript API¶
-
flexx.pyscript.
py2js
(ob=None, new_name=None, **parser_options)¶ Convert Python to JavaScript.
パラメータ: - ob (str, module, function, class) – The code, function or class to transpile.
- new_name (str, optional) – If given, renames the function or class. This argument is ignored if ob is a string.
- parser_options – Additional options for the parser. See Parser class for details.
戻り値: jscode – The JavaScript code as a special str object that has a
meta
attribute that contains the following fields:- filename (str): the name of the file that defines the object.
- linenr (int): the starting linenr for the object definition.
- pycode (str): the Python code used to generate the JS.
- pyhash (str): a hash of the Python code.
- vars_defined (set): names defined in the toplevel namespace.
- vars_unknown (set): names used in the code but not defined in it.
- vars_global (set): names explicitly declared global.
- std_functions (set): stdlib functions used in this code.
- std_method (set): stdlib methods used in this code.
戻り値の型: str
Notes
The Python source code for a class is acquired by name. Therefore one should avoid decorating classes in modules where multiple classes with the same name are defined. This is a consequence of classes not having a corresponding code object (in contrast to functions).
-
flexx.pyscript.
evaljs
(jscode, whitespace=True, print_result=True)¶ Evaluate JavaScript code in Node.js.
パラメータ: - jscode (str) – the JavaScript code to evaluate.
- whitespace (bool) – if whitespace is False, the whitespace is removed from the result. Default True.
- print_result (bool) – whether to print the result of the evaluation. Default True. If False, larger pieces of code can be evaluated because we can use file-mode.
戻り値: result – the last result as a string.
戻り値の型: str
-
flexx.pyscript.
evalpy
(pycode, whitespace=True)¶ Evaluate PyScript code in Node.js (after translating to JS).
パラメータ: - pycode (str) – the PyScript code to evaluate.
- whitespace (bool) – if whitespace is False, the whitespace is removed from the result. Default True.
戻り値: result – the last result as a string.
戻り値の型: str
-
flexx.pyscript.
script2js
(filename, namespace=None, target=None, module_type='umd', **parser_options)¶ Export a .py file to a .js file.
パラメータ: - filename (str) – the filename of the .py file to transpile.
- namespace (str) – the namespace for this module. (optional)
- target (str) – the filename of the resulting .js file. If not given
or None, will use the
filename
, but with a.js
extension. - module_type (str) – the type of module to produce (if namespace is given), can be ‘hidden’, ‘simple’, ‘amd’, ‘umd’, default ‘umd’.
- parser_options – additional options for the parser. See Parser class for details.
-
flexx.pyscript.
js_rename
(jscode, cur_name, new_name)¶ Rename a function or class in a JavaScript code string.
パラメータ: - jscode (str) – the JavaScript source code
- cur_name (str) – the current name
- new_name (str) – the name to replace the current name with
戻り値: jscode – the modified JavaScript source code
戻り値の型: str
-
flexx.pyscript.
get_full_std_lib
(indent=0)¶ Get the code for the full PyScript standard library.
The given indent specifies how many sets of 4 spaces to prepend. If the full stdlib is made available in JavaScript, multiple snippets of code can be transpiled without inlined stdlib parts by using
py2js(..., inline_stdlib=False)
.
-
flexx.pyscript.
get_all_std_names
()¶ Get list if function names and methods names in std lib.
-
flexx.pyscript.
create_js_module
(name, code, imports, exports, type='umd')¶ Wrap the given code in an AMD module.
Note that “use strict” is added to the top of the module body. PyScript does not deal with license strings; the caller should do that.
パラメータ: - name (str) – the name of the module.
- code (str) – the JS code to wrap.
- imports (list) – the imports for this module, as string names of the dependencies. Optionally, ‘as’ can be used to make a dependency available under a specific name (e.g. ‘foo.js as foo’).
- exports (str, list) – the result of this module (i.e. what other modules get when they import this module. Can be a JS expression or a list of names to export.
- type (str) – the type of module to export, valid values are ‘hidden’, ‘simple’ (save module on root), ‘amd’ , ‘amd-flexx’ and ‘umd’ (case insensitive). Default ‘umd’.
Most users probably want to use the above functions, but you can also get closer to the metal by using and/or extending the parser class.
-
class
flexx.pyscript.
Parser
(code, pysource=None, indent=0, docstrings=True, inline_stdlib=True)¶ Parser to convert Python to JavaScript.
Instantiate this class with the Python code. Retrieve the JS code using the dump() method.
In a subclass, you can implement methods called “function_x” or “method_x”, which will then be called during parsing when a function/method with name “x” is encountered. Several methods and functions are already implemented in this way.
While working on ast parsing, this resource is very helpful: https://greentreesnakes.readthedocs.org
パラメータ: - code (str) – the Python source code.
- pysource (tuple) – the filename and line number that contain the source.
- indent (int) – the base indentation level (default 0). One indentation level means 4 spaces.
- docstrings (bool) – whether docstrings are included in JS (default True).
- inline_stdlib (bool) – whether the used stdlib functions are inlined (default True). Set to False if the stdlib is already loaded.
PyScript allows embedding raw JavaScript using the RawJS
class.
-
class
flexx.pyscript.
RawJS
(code, _resolve_defining_module=True)¶ An object to wrap verbatim code to be included in the generated JavaScript. This serves a number of purposes:
- Using code in PyScript that is not valid Python syntax, like regular
expressions or the jQuery object
$
. - Write high performance code that avoids Pythonic features like operator overloading.
- In Flexx’s module system it can be used to create a stub variable in Python that does have a value in JS. This value can imported in other modules, leading to a shared value also in JS.
PyScript does not verify the syntax of the code, so write carefully! To allow the features in the 3d point, this object has a magic touch: the
__module__
attribute of an instance refers to the module in which it was instantiated, and if it’s a global, its defining name can be obtained.Example:
# Syntax not usable in Py myre = VerbatimJS('/ab+c/') # Code that should only execute on JS foo = VerbatimJS('require("some.module")') # Performance def bar(n): res = [] VerbatimJS(''' for (var i=0; i<n; i++) { if (is_ok_num(i)) { res.push(i); } } ''')
- Using code in PyScript that is not valid Python syntax, like regular
expressions or the jQuery object
The PyScript module has a few dummy constants that can be imported and
used in your code to let e.g. pyflakes know that the variable exists. E.g.
from flexx.pyscript import undefined, window Infinity, NaN
.
Arbitrary dummy variables can be imported using
from flexx.pyscript.stubs import JSON, foo, bar
.