之前看到的一個有趣的 Open Source 專案(Brython), 雖然我覺得沒什麼實用性~ XD
不過, 既然是蛇年, 就讓 瀏覽器 也 蛇化 吧~
- Brython 的說明: Brython's goal is to replace Javascript with Python, as the scripting language for web browsers.
Brython - 在瀏覽器用 Python 取代 JavaScript
此專案載入一隻 js 檔, 然後就可以用 Python 語法來操作 DOM、AJAX... 等等的事情. (寫在 <script type="text/python"> 裡面)
- 專案首頁: Brython - browser python
- Google code: brython - Browser Python
Brython 使用方式(範例)
說明文件: Brython document, 下述範例取自此篇:
<html> <head> <script src="/brython.js"></script> </head> <body onLoad="brython()"> <script type="text/python"> def echo(): alert(doc["zone"].value) </script> <input id="zone"><button >Brython Syntax 筆記
- Brython Syntax 說明: Brython Syntax
建立 a link
- link1 = A('Brython', href='http://www.brython.info')
- link2 = A(B('Python'), href='http://www.python.org')
建立 a 並 附加屬性
- link = A()
- link <= B('connexion')
- link.href = 'http://example.com'
AJAX
- req = ajax()
- req.on_complete = on_complete
- req.set_timeout(timeout, err_msg)
- req.open('POST', url, True)
- req.set_header('content-type', 'application/x-www-form-urlencoded')
- req.send(data)
Local storage
- local_storage['foo'] = 'bar'
- log(local_storage['foo'])
- del local_storage['foo']
- log(local_storage['foo']) # prints None
text/python 寫法範例
<script type='text/python'>
def mouse_move(ev):
doc["trace"].value = '%s %s' %(ev.x,ev.y)
doc["zone"].onmousemove = mouse_move
</script>
Brython 相關 Library (函式庫)
- 函式庫的套件可見: Brython - Compiling and running
這邊有很多必須的 Library, 在這邊列一些應用類的.
- py2js.js : does the conversion between the tokens and the Javascript code
- py_dom.js : interaction with the HTML document (DOM)
- py_ajax.js : Ajax implementation
- py_local_storage.js : implementation of the HTML5 local storage
- py_svg.py : SVG support (vector graphics)