網站首頁 編程語言 正文
根據 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
相關推薦
- 2022-10-31 C#實現HTTP訪問類HttpHelper的示例詳解_C#教程
- 2022-09-13 教你一招完美解決vscode安裝go插件失敗問題_Golang
- 2022-03-31 Python的三個重要函數詳解_python
- 2022-08-23 .net中的DI框架AutoFac簡單介紹_實用技巧
- 2022-05-18 Python學習之自定義異常詳解_python
- 2022-05-07 Python?識別錄音并轉為文字的實現_python
- 2022-12-01 Python?async+request與async+aiohttp實現異步網絡請求探索_pytho
- 2023-08-13 ps打開之后切圖一段時間C盤存儲量變小怎么清理?
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支