背景

网页执行python,脚本,也就是在html中写python,或者引入python脚本代码

方法

也就引入pyscript库,调用python脚本

步骤

1、新建一个html文件testpyscript.html

<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /&gt;
      <script defer src="https://pyscript.net/alpha/pyscript.js"&gt;</script&gt;
      <py-env&gt;
        - numpy
        - matplotlib
        - paths:
          - ./data.py
      </py-env>
    </head>

  <body>
    <h1>Let's plot random numbers</h1>
    <div id="plot"></div>
    <py-script output="plot">
    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
    </py-script>
  </body>
</html>
# 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.htmldata.py文件目录执行终端,也就命令行windows

pythonm http.server 8080 —bind 127.0.0.1

3、127.0.0.1:8080/testpyscript.html

原文地址:https://blog.csdn.net/liuchenbaidu/article/details/132430086

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_11355.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注