您现在的位置是:亿华云 > 人工智能
什么?可以在 HTML 中直接插入 Python 代码?
亿华云2025-10-08 18:52:47【人工智能】9人已围观
简介PyScript 由来自 Anaconda 的团队开发,是一个用于在 HTML 中插入 Python 代码的工具,这意味着你可以在 HTML 中编写和运行 Python 代码,在
PyScript 由来自 Anaconda 的中直团队开发,是接插一个用于在 HTML 中插入 Python 代码的工具,这意味着你可以在 HTML 中编写和运行 Python 代码,中直在 PyScript 中调用 Javascript 库,接插并在 Python 中进行所有的中直 Web 开发,是接插不是很神奇!
有了 PyScript,我们现在可以在 HTML 中编写 Python 代码并构建 Web 应用了,中直PyScript 可以让更多的接插前端开发者接触到 Python 的强大功能。有了 PyScript,中直我们不再需要担心部署问题,接插因为一切都将在你的中直浏览器中执行,比如作为数据科学家,接插你可以在一个 html 文件中分享模型,中直只要其他人在浏览器中打开该文件,接插就会运行代码了。中直
PyScript 是基于 Pyodide(https://pyodide.org/) 开发的,云服务器它是 CPython 到 WebAssembly/Emscripten 的入口,PyScript 支持在浏览器中编写和运行 Python 代码,未来还将提供对其他语言的支持。
使得用 Python 编写网站成为可能的基础技术是 WebAssembly。最初开发 WebAssembly 时,Web 浏览器仅支持 Javascript。WebAssembly 于 2017 年首次发布,到 2019 年迅速成为了 W3C 的官方标准,它包括一种人类可读的 .wat 文本格式语言,然后将其转换为浏览器可以运行的二进制 .wasm 格式,这就使得我们可以用任何语言编写代码,将其编译为 WebAssembly,然后在网络浏览器中运行。
如何使用 PyScript?PyScript 的 alpha 版本可以在 pyscript.net 上找到,代码可在 https://github.com/pyscript 获得。PyScript 允许你使用以下三个主要组件在 html 中编写 Python:
定义了运行 Python 代码所需的源码库 Python 包。是你编写在网页中执行的 Python 代码的地方。创建一个 REPL 组件,用于评估用户输入的代码并显示结果。我们先创建一个最简单的示例,代码如下所示:
Hello world!
This is the current date and time, as computed by Python:
from datetime import datetime
now = datetime.now()
now.strftime("%m/%d/%Y, %H:%M:%S")
</html>只需要这页面上显示当前时间即可,在浏览器中打开后就可以看到结果了,如下所示:
又比如我们创建一个具有流式数据的 Panel 仪表盘,代码如下所示,先通过 引入需要的包,然后在 中编写 Python 代码,如果你不喜欢直接在下编写 Python 代码,也可以使用 Python 文件作为源代码,例如。
Bokeh.set_log_level("info");
- bokeh
- numpy
- pandas
- panel
Panel Streaming Demo
import asyncio
import panel as pn
import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from panel.io.pyodide import show
df = pd.DataFrame(np.random.randn(10, 4), columns=list(ABCD)).cumsum()
rollover = pn.widgets.IntInput(name=Rollover, value=15)
follow = pn.widgets.Checkbox(name=Follow, value=True, align=end)
tabulator = pn.widgets.Tabulator(df, height=450, width=400)
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `color: red` for negative
strings, black otherwise.
"""
color = red if val < 0 else green
return color: %s % color
tabulator.style.applymap(color_negative_red)
p = figure(height=450, width=600)
cds = ColumnDataSource(data=ColumnDataSource.from_df(df))
p.line(index, A, source=cds, line_color=red)
p.line(index, B, source=cds, line_color=green)
p.line(index, C, source=cds, line_color=blue)
p.line(index, D, source=cds, line_color=purple)
def stream():
data = df.iloc[-1] + np.random.randn(4)
tabulator.stream(data, rollover=rollover.value, follow=follow.value)
value = { k: [v] for k, v in tabulator.value.iloc[-1].to_dict().items()}
value[index] = [tabulator.value.index[-1]]
cds.stream(value)
cb = pn.state.add_periodic_callback(stream, 200)
controls = pn.Row(cb.param.period, rollover, follow, width=400)
await show(controls, controls)
await show(tabulator, table)
await show(p, plot)
</html>上面的代码在浏览器中打开后就可以直接显示对应的效果了:
关于 PyScript 的更多使用方法可以查看 Git 仓库示例 https://github.com/pyscript/pyscript/tree/main/pyscriptjs/examples 了解更多。
站群服务器很赞哦!(292)
相关文章
- Status、Creation Date、Expiration Date
- Python中的十大图像处理工具
- 五个让日常编码更简单的 Python 库
- 超全面的前端工程化配置指南
- 新手可以注册cc域名吗?cc域名有什么特点?
- 用 Python 绘制几张有趣的可视化图表
- 如何移除你项目中 99% 的 JS 代码
- 接口流量突增,如何做好性能优化?
- 为了避免将来给我们的个人站长带来的麻烦,在选择域名后缀时,我们的站长最好省略不稳定的后缀域名,比如n,因为我们不知道策略什么时候会改变,更不用说我们将来是否还能控制这个域名了。因此,如果站长不是企业,或者有选择的话,如果不能选择域名的cn类,最好不要选择它。
- 快速掌握 TypeScript 新语法:Infer Extends