This is a brief intro for using PyScript. For more details see the sections below.
language. All relevant buildins, and the methods of list, dict and str
are supported. Not supported are set, slicing with steps,
yield. Imports are not supported. Other than that,
most Python code should work as expected, though if you try hard enough the
as expected when correct, but error reporting may not be very Pythonic.
This resulted in the following two main goals:
Code produced by PyScript works standalone. Any (PyScript-compatible) Python snippet can be converted to JS; you don’t need another JS library to run it.
The purpose of projects like Skulpt or PyJS is to enable full Python support in the browser. This approach will always be plagued by a fundamental limitation: libraries that are not pure Python (like numpy) will not work.
This means that depending on what you want to achieve, you may still need
code can be converted (e.g.
**kwargs are not supported), and
PyScript makes writing JS more “Pythonic”. Apart from allowing Python syntax
for loops, classes, etc, all relevant Python buildins are supported,
as well as the methods of list, dict and str. E.g. you can use
The empty list and dict evaluate to false (whereas in JS it’s
isinstance() just works (whereas JS’
Deep comparisons are supported (e.g. for
in), so you can
actually compare two lists or dicts, or even a structure of nested
lists/dicts. Lists can be combined with the plus operator, and lists
and strings can be repeated with the multiply (star) operator. Class
methods are bound functions.
- JavasScript has a concept of
None), as well as
undefined. Sometimes you may want to use
if x is None or x is undefined: ....
- Accessing an attribute that does not exist will not raise an
AttributeError but yield
- Magic functions on classes (e.g. for operator overloading) do not work.
- Calling an object that starts with a capital letter is assumed to be
a class instantiation (using
new): PyScript classes must start with a capital letter, and any other callables must not.
PyScript is valid Python¶
Other than e.g. RapydScript, PyScript is valid Python. This allows creating modules that are a mix of real Python and PyScript. You can easily write code that runs correctly both as Python and PyScript. Raw JS can be included by defining a function with only a docstring.
PyScript itself (the compiler) is written in Python. Perhaps PyScript can at some point compile itself, so that it becomes possible to define PyScript inside HTML documents.
Faster than CPython, and significantly faster than Brython and friends.
This is an overview of the language features that PyScript supports/lacks.
Not currently supported:
- import (maybe we should translate an import to
setclass (JS has no set, but we could create one?)
- slicing with steps (JS does not support this)
- support for
**kwargs(maps badly to JS call mechanism)
withstatement (no equivalent in JS)
- Generators, i.e.
yield(not widely supported in JS)
- numbers, strings, lists, dicts (the latter become JS arrays and objects)
- operations: binary, unary, boolean, power, integer division,
- comparisons (
- tuple packing and unpacking
- basic string formatting
- slicing with start end end (though not with step)
- if-statements and single-line if-expressions
- while-loops and for-loops supporting continue, break, and else-clauses
- for-loops using
- for-loop over arrays
- for-loop over dict/object using
- function calls can have
- function defs can have default arguments and
- lambda expressions
- list comprehensions
- classes, with (single) inheritance, and the use of
- raising and catching exceptions, assertions
- creation of “modules”
- globals / nonlocal
Supported Python conveniences:
- use of
selfis translated to
isinstance()Just Works (for primitive types as well as user-defined classes)
- an empty list or dict evaluates to False as in Python.
- all Python buildin functions that make sense in JS are supported: isinstance, issubclass, callable, hasattr, getattr, setattr, delattr, print, len, max, min, chr, ord, dict, list, tuple, range, pow, sum, round, int, float, str, bool, abs, divmod, all, any, enumerate, zip, reversed, sorted, filter, map.
- all methods of list, dict and str are supported (except a few string methods: encode format format_map isdecimal isdigit isprintable maketrans)
- the default return value of a function is
- list concatenation using the plus operator, and list/str repeating using the star operator.
- deep comparisons.
- class methods are bound functions (i.e.
thisis fixed to the instance).
- functions that are defined in another function (a.k.a closures) that do not have self/this as a first argument, are bound the the same instance as the function in which it is defined.
The PyScript package provides a few other “utilities” to deal with JS code, such as renaming function/class definitions, and creating JS modules (AMD, UMD, etc.).