如何用pyscript在网页里面写Python程式
Admin 2022-09-27 群英技术资讯 1009 次浏览
今天就跟大家聊聊有关“如何用pyscript在网页里面写Python程式”的内容,可能很多人都不太了解,为了让大家认识和更进一步的了解,小编给大家总结了以下内容,希望这篇“如何用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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了详解Python+opencv裁剪/截取图片的几种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
之前我们也了解很多python函数,今天给大家分享的函数是os.path.join()是拼接文件路径函数,那么os.path.join()函数怎样使用呢?接下来跟随小编来学习一下吧。
对List进行排序,Python提供了两个方法方法1用List的内建函数listsort进行排序listsort(func=None,key=None,reverse=False)Python实对List进行排序,Python提供了两个方法方法1.用List的内建函数list.sort进行排序list.sort(func=None,key=None,reverse=False
Python文件路径乱码的解决方法是怎么,代码怎么写
用python如何实现求解微分线性方程?python的功能还是比较强大的,这篇文章我们就来看看用Python编写稀疏线性方程组的求解方法,下文有实例代码供大家参考,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008