Sunday, February 9, 2014

Python everywhere: #Brython 2.0

 It's alive


Wow, 15 months later and we are at Brython 2.0!

Changes in Brython version 2.0-20140209-164925



Backwards-incompatible change

=============================
For the sake of namespace isolation, by default, the name of functions and classes defined in Brython are no more available in Javascript global namespace. As a consequence, functions can't be called as event callbacks from HTML tags. For instance, if a function echo() is defined in a Brython script, the following inline code :

    <button onclick="echo()">

will result in a Javascript error such as "name echo is not defined" when the button is clicked

The solution is to define the event callback inside Brython code. The button must have an id :

    <button id="echo">

and the Brython code must include the line

    doc["echo"].bind("click",echo)

Alternatively, to force the name "echo" into the global Javascript namespace, add it as an attribute of the window object :

    from browser import window
    window.echo = echo


Features

========
  • - namespace isolation : in previous versions, the names defined in Brython core scripts were included in the global Javascript namespace, raising risks of conflict with names defined in Javascript programs or libraries. In this version, the only names included in the global Javascript namespace are __BRYTHON__ (used internally) and brython(), the function called on page load
  • - implement sys.implementation with values adapted for Brython
  • - allow syntax del (x, y, z)
  • - use Dan McDougall's pyminifier to reduce size of py_VFS.js
  • - make_dist generates a script brython_dist.js that can be used as a standalone distribution on a centralised server.

Demos

=====
  • - add drop files demo (by Glenn Linderman) and French Python course (using module slideshow) to the gallery

Imports

=======
  • - improved version of py_VFS.js by Billy Earney. py_VFS compiles the standard library to make imports faster. Now packs Python source with JSON instead of base64

Built-in modules

================
  • - browser : add attributes "window" and "document" as alias of respectively "win" and "doc"
  • - add a module browser/slideshow for PowerPoint-like slideshows in Brython, using a custom file format

Bug fixes

=========
  • - issue #174 : string format character % has wrong precedence
  • - issue #175 : Functions should return None by default
  • - issue #183 : re.findall() broken
  • - issue #198 : operator precedence not defined for << and >>
  • - issue #206 : add <noscript> in case Javascript is disabled
  • - issue #208 : bug with try / else
  • - issue #209 : bug in ternary operator
  • - function definions must be evaluated only once, upon function definition
  • - bug with del a[:]
  • - bug with dir() called without arguments
  • - bug in map()
  • - bug with "func(x=1,)" ; "return +x" ; "func(10*-x)"
  • - bug in divmod and round
  • - bug in local_storage method keys()
  • - add attribute __doc__ to class and instance methods

Documentation

=============
  • - updated with the changes introduced in this version
  • - add documentation for the built-in module javascript
  • - translation in Spanish updated by Kiko Correoso

No comments: