Brython可以替代javascript在使用python来编写浏览器脚本。
brpython的目标是用Python取代Javascript,作为Web浏览器的脚本语言。
我们先看一个示例代码
<!DOCTYPE html>你会惊奇的发现竟然输入了helloworld
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/brython.min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/brython_stdlib.js"></script>
</head>
<bodyBfwOnload="brython()">
<script type="text/python">
from browser import document
document <= "Hello World!"
</script>
</body>
</html>
再来个弹窗
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/brython.min.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/brython_stdlib.js"></script>
</head>
<bodyBfwOnload="brython()">
<script type="text/python">
import browser
browser.alert("Hello Real Python!")
</script>
</body>
</html>
其他场景用法:
建立 a linklink1 = 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
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)
网友回复
python中如何让ai接管邮箱自动回复邮件?
Neutralinojs与Electron的区别与不同?
浏览器跨域解决方案有哪些?
有没有开源的solo agent一句话描述就能开发直接运行的前后端应用源代码?
订单支付过程中部分商品库存不足如何处理?
python如何开发一个自定义域名后缀的邮箱系统及登录发送邮件管理web页面?
有没有开源的项目将图片视频声音文字转场特效编排自动生成剪映草稿json文件?
有没有摄像头捕获眼球转动操作鼠标的开源代码?
localstorage如何生成自增的键值对进行增删改查?
python有没有将python脚本与python运行环境一键打包成exe的代码?