Modulo, String, Import Bugs

UPDATE: Most (if not all) of these bugs have been fixed since I started using Pyjamas. This is here mainly for reference.

Pyjamas has a very useful migration guide which summarizes the current issues/differences between JS and Python that the developers did not yet resolve. It’s a very useful read for anyone using Pyjamas, both with JS and Python background and a must-read for anyone wishing to make an app that works both in browser and in Pyjamas-Desktop. I did find a few issues, however, that weren’t yet documented. So I decided to post them here, including the work-arounds.

Importing radians function from math


For some reason, the following code will cause an ImportError in JS:

import math

However, doing the same with math.pi, math.sin, math.cos, math.sqrt works fine


Either use the following:

from math import radians


from math import pi

Concatenating two strings using +


The following works fine in Python and Pyjamas-Desktop, but throws an exception (that Pyjamas catches, and gives “inform the webmaster” error):


Ironically, passing the same into a function as an argument works fine in both:



The following alternative works in both:


Alternatively, if you want a cleaner-looking concatenation:

def strcat(*args):
  return ''.join(args) b="jamas"

Different modulo implementation


When performing the modulo command, Python uses the sign of the divisor for the result. JS, however uses the sign of the dividend. The following code in Pyjamas-Desktop will set a to 0.6, while in JS/browser it will set it to -0.4:



So far, my solution is to do the following:

a=dividend%divisor if a<0:

I also tried using math.fmod() instead, but ran into the same ImportError issue as described above for radians, unfortunately using the work-around described above only works for radians(). For fmod() Pyjamas will throw “$module.math.fmod is not a function” exception

About Pyjamas

I’ll start with a sentence about myself. My undergrad degree is in Computer Engineering from Syracuse University. I then got a master’s in Systems Engineering from Cornell. I started off as a C++ developer, but after seeing Python I immediately fell in love. So you can imagine how excited I was when I found out that I could write JavaScript (JS) web apps directly in Python using Pyjamas. I won’t bore you with the rest of my background, the blog is about Pyjamas, so I’ll keep it that way.

For those unfamiliar with Pyjamas, the quickest way to describe it is as a Python port of GWT. It allows you to manipulate HTML, CSS, and JS directly through Python, without writing a line of JS. Pyjamas then “compiles” the code in cross-browser JS. Unlike GWT, which is fairly stable and known by now, Pyjamas is still bleeding-edge, and has quite a few issues to address before mainstream adoption. Even now, however, Pyjamas offers several advantages over GWT:

  • Python is easier to code in than Java (shorter development time)
  • Pyjamas can run as a stand-alone application outside the browser in pure Python using Pyjamas-Desktop, GWT’s hosted mode on the other hand is only suitable for debugging
  • Python is more similar to JS than Java is (dynammic typing, etc.) and therefore it’s already more compatible with JS, before you even write any code. If you want proof, just compare the source code. Pyjamas accomplishes similar functionality in several thousand lines of code that GWT needs hundreds of thounsands lines for.

Yes, GWT is definitely ahead right now, but Pyjamas has higher velocity. Pyjamas is a community effort, employing “if you need a feature, add it yourself” methodology. While I appreciate the flexibility, I think this is the kind of approach that was hurting Linux until the user-friendly distributions like Ubuntu came along, and is likely to drive away beginners before they have a chance to understand what Pyjamas is. It’s hard to employ different methodology in a small project with no funding (although I do wish working proprietary functions like google groups wouldn’t be replaced with buggy non-fully tested Pyjamas-based ones). So I urge you to stick through the learning curve, it’s definitely worth it.

At this point I should probably give a quick tour by showing you a “Hello World” app, but I think a link to already written Pyjamas examples would be a lot more convincing.