背景:
在网页中执行python,脚本,也就是在html中写python,或者引入python脚本代码
方法:
也就引入pyscript库,调用python脚本
步骤
1、新建一个html文件testpyscript.html
- numpy
- matplotlib
- paths:
- ./data.py
Let's plot random numbers
import matplotlib.pyplot as plt
from data import make_x_and_y
x, y = make_x_and_y(n=1000)
fig, ax = plt.subplots()
ax.scatter(x, y)
fig
# data.py
import numpy as np
def make_x_and_y(n):
x = np.random.randn(n)
y = np.random.randn(n)
return x, y
2、在这testpyscript.html和data.py文件目录下执行终端,也就命令行(windows)
python -m http.server 8080 --bind 127.0.0.1
3、127.0.0.1:8080/testpyscript.html