使用pyscript在网页中撰写Python程式的方法
根据 Anaconda 的项目 pyscript,可以将 python 的代码直接写在网页中,目前只支援两种标签,分别是<py-script>
与 <py-repl>
,以下是简单的示例。
使用这两行导入 pyscript
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" rel="external nofollow" rel="external nofollow" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
使用<py-repl>
标签,会显示原始码,可以单击左下角的按键,开始运行。
<py-repl> from datetime import datetime now = datetime.now() print("目前日期时间", now.strftime("%m/%d/%Y, %H:%M:%S")) list1 = list("Pythony在网页中执行") for i in list1: print("{}".format(i)) </py-repl>
效果如下:
使用<py-repl>
标签的结果,原则上是使用非同步处理,所以与主画面渲染比较会有延迟。
使用<py-script>
标签
<py-script> from datetime import datetime now = datetime.now() print("目前日期时间", now.strftime("%m/%d/%Y, %H:%M:%S")) list1 = list("Pythony在网页中执行") for i in list1: print("{}".format(i)) </py-script>
效果如下:
使用<py-script>
标签的结果
完整程式范例如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width,initial-scale=1" /> <title>第一个 PyScript 练习</title> <link rel="icon" type="image/png" href="favicon.png" rel="external nofollow" /> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" rel="external nofollow" rel="external nofollow" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> 在网页中撰写 Python 程式<br> 显示目前时间与印出一个列表: 使用 py-repl 标签<br/> <py-repl> from datetime import datetime now = datetime.now() print("目前日期时间", now.strftime("%m/%d/%Y, %H:%M:%S")) list1 = list("Pythony在网页中执行") for i in list1: print("{}".format(i)) </py-repl> 使用 py-script 标签<br/> <py-script> from datetime import datetime now = datetime.now() print("目前日期时间", now.strftime("%m/%d/%Y, %H:%M:%S")) list1 = list("Pythony在网页中执行") for i in list1: print("{}".format(i)) </py-script> </body> </html>
参考资料
pyscript,https://pyscript.net/
PyScript,https://github.com/pyscript/pyscript
到此这篇关于使用pyscript在网页中撰写Python程式的方法的文章就介绍到这了,更多相关pyscript Python程式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python shapefile转GeoJson的2种方式实例
geojson是地图可视化系统中最常用的地理数据格式,几乎所有主流地图可视化库或框架都支持geojson数据的加载,下面这篇文章主要给大家介绍了关于Python shapefile转GeoJson的2种方式的相关资料,需要的朋友可以参考下2023-03-03Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
这篇文章主要介绍了Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法,结合具体实例形式较为详细的分析了Python面向对象程序设计中类的定义、实例化、封装、私有变量、私有方法等相关使用技巧,需要的朋友可以参考下2019-02-02
最新评论