日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用pyscript在網頁中撰寫Python程式的方法_python

作者:Yehchitsai ? 更新時間: 2022-07-03 編程語言

根據 Anaconda 的項目 pyscript,可以將 python 的代碼直接寫在網頁中,目前只支援兩種標簽,分別是<py-script><py-repl>,以下是簡單的示例。

使用這兩行導入 pyscript

<link       />
<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  type="image/png" href="favicon.png"   />
		<link       />
		<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

原文鏈接:https://blog.csdn.net/m0_50614038/article/details/124566090

欄目分類
最近更新