Pyjs - Python Javascript Compiler, Desktop Widget Set and RIA Web Framework

本文介绍了Pyjs,一种将Python应用及其UI组件和DOM类转换为JavaScript应用的工具。它通过遍历Python抽象语法树生成JavaScript代码,并提供了额外的UI组件和DOM库。此外,Pyjs Desktop提供了更高级的控制,用于测试和调试应用。通过简单的步骤和示例代码,如Hello World应用和Pyjs Desktop应用,展示了如何将Python应用部署到Web环境。

Pyjs - Python Javascript Compiler, Desktop Widget Set and RIA Web Framework

Overview

Like GWT, Pyjs translates a Python application and
libraries
(including UI widgets and DOM classes) into a
Javascript application and libraries,
and packages it all up:

Overview of Pyjs' components

  1. pyjs translates Python code to Javascript by walking
    the Python abstract syntax tree and generating Javascript.
  2. pyjslib takes care of Python types that require
    a custom Javascript implementation for pyjs to work. For example,
    even though Python lists are similar to Javascript arrays, Python
    lists are converted to custom objects that implement methods such
    as append.
  3. UI widgets and a DOM library are provided as a
    convenience. They are written in Python and, like everything else,
    they are translated to Javascript for deployment. These are based
    on those in GWT.
  4. build manages the overall translation of
    individual components and creates a set of .html
    and .js files that can be served up by a Web server.

Hello World

The omnipresent hello world example, let's call it 'Hello Pyjs':

from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.Label import Label

l = Label('Hello Pyjs')
RootPanel().add(l)

Now run pyjsbuild hello.py to generate a plain Javascript
application: pyjs creates an ./output folder containing all files
you need to move to your webspace later. Of course you can test locally,
too.

Add 3 additional lines in total and your application can run with both
Pyjs (i.e. plain Javascript) and Pyjs Desktop:

import pyjd # this is dummy in pyjs
from pyjamas.ui.RootPanel import RootPanel
from pyjamas.ui.Label import Label

pyjd.setup('public/hello.html')
l = Label('Hello Pyjs')
RootPanel().add(l)
pyjd.run()

Execute pyjd hello.py, or python hello.py, to run
the very same application native on Python, giving you a Python stack trace on
errors. Pyjs Desktop gives you much more control making testing and debugging
a piece of a cake. No more web development pain – hooray!  
Note the hello.html file referenced above: this is a container
for your application generated by Pyjs that you can adjust to your needs.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值