build
AJAX apps in Python (like Google did for Java)
Getting Started with
pyjamas
Programming with pyjamas needs a little bit of rethinking about
the way that you do Web application development. Primarily,
you need to forget virtually everything you've ever learned and
come to expect Web development to be. The reason for this is
very simple: Pyjamas is pure javascript. Although written
in python, not javascript, it is essential to bear in mind
that virtually 100% of your web application will be javascript -
not HTML. The programming style you may be accustomed to for
HTML programming involves placing as much HTML into one page
as you can stand, and making the minimum number of exceptions
and allowances for dynamic content that you can manage, without
making the HTML page "too complicated" to be readable.
Pyjamas makes it possible for you to break pages down into concepts.
classes. widgets. maybe some CSS styling is added, almost as an
afterthought, on top of the "real" functionality. In other words, Pyjamas
is actually more like Desktop application development than it is
Web development.
With that in mind, the best possible starting point has to be the
examples, most of which were ported from
Google Web Toolkit. They will make it really clear just how "not"
Web that pyjamas - and incidentally GWT - really are.
Examples
The simplest example is of course the traditional
Hello World
or, in this case, Hello AJAX. If you've downloaded pyjamas, you
will be able to browse, with your browser, to the examples directory
and see this in action for yourself. Type
"file://home/yourusername/pyjamas/examples" into your URL bar - or
wherever you have unpacked pyjamas to and continue to browse to
the helloworld output directory.
Once you have played with the example, online, try it on your local
machine. Remember to run the "build.sh" script (if you have linux
or MacOS, or execute python.exe ../../build/build.py Hello.py if
you have windows). Then, take a look at the source code
that generated it, which is shown here:
from pyjamas import Window from pyjamas.ui import RootPanel, Button def greet(sender): Window.alert("Hello, AJAX!") b = Button("Click me", greet) RootPanel().add(b)
The most important thing to note is that everything gets added to
RootPanel. RootPanel() gives you access to the Browser's
DOM model (starting of course at body). To illustrate,
try adding this, and see what happens:
RootPanel().add(HTML("Hello <b>World</b>"))
You should get nothing - and if you look in your Javascript
console for an error message, you should find an error indicating
that "HTML" does not exist. This is correct - because you needed to
add this to the top of the module, along with the other imports:
from pyjamas.ui import HTML
Now if you re-run build.sh, you should see both a button and next
to it the words "Hello World". Congratulations, you've just
successfully developed your first pyjamas application.