如何用pyscript在网页里面写Python程式
Admin 2022-09-27 群英技术资讯 746 次浏览
今天就跟大家聊聊有关“如何用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使用LRU缓存策略进行缓存的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在使用python中,你会遇到一个词,就是堆排序。那堆排序是什么呢?堆排序看字面意思是一种排序方法,那堆是什么呢?堆是一个近似完全二叉树的结构,并同时满足堆积的性质。其实堆排序是指利用堆这种数据结构所设计的一种排序算法。认识了堆排序,那你知道堆排序在python中如何实现吗?
本文主要介绍了Python数组变形的几种实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
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备09006778号 域名注册商资质 粤 D3.1-20240008