Brython (Browser Python)是執行在瀏覽器中的 Python 3 的一個實現,具有處理DOM 元素的方法和事件的介面。
下面是一個執行 Python 的 HTML 頁面的簡單例子:
<html>
<head>
<script type="text/javascript" src="/path/to/brython.js"></script>
</head>
<body onload="brython()">
<script type="text/python">
from browser import document, alert
def echo(event):
alert(document["zone"].value)
document["mybutton"].bind("click", echo)
</script>
<input id="zone"><button id="mybutton">click !</button>
</body>
</html>
要使用 Brython,你只需要
- 載入脚本brython.js.
- 在網頁載入的時候執行函数
brython()
例如<body onload="brython()">
. - 在標籤中編寫 Python 程式碼<script type=”text/python”>
想要了解 Brython 的工作原理可以參考 – How Brython works
✍ 專案網址 https://github.com/brython-dev/brython
Contents
相關線上課程
✍ 搜尋結果太多?可參考 Soft & Share 搜尋引擎使用技巧
幫我們個小忙!
Image by Mudassar Iqbal from Pixabay