最新 PyScript 實測!讓 HTML 也能執行 Python!(附程式碼)

最近看到 Anaconda 開發了一個新的 Open source project 叫做 PyScript,是一個能在 HTML 中直接撰寫 Python 程式,當使用者開啟該檔案時,就會在背後直接開始執行 Python

感覺運用場景可以很廣,像是直接進行資料視覺化、產出 Dashboard,或是利用爬蟲直接顯示有興趣的網頁的結果

這個專案的核心是靠 Pyodide,跟之前我介紹過的 Jupyter Lite 一樣,還沒看過的可以複習一下

此篇文章主要是做一些 PyScript 的實測,並記錄試用的過程與感想


1. 不管怎樣都要先來個 Hello world

程式碼

<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body> <py-script> print('Hello, World!') </py-script> </body>
</html>

網頁呈現結果

個人感想

  • 開網頁直到顯示出 Hello World 之間,會有明顯有感的延遲時間

2. 來點簡單的數學計算

閱讀更多»