如何在HTML文件中包含python脚本?

new*_*san 6 html python

我该如何放置此python脚本:

a = ['f','d','s','a']
x = -1
scope = vars()
for i in a:
    scope['x']+=1
    print a[x]
Run Code Online (Sandbox Code Playgroud)

在html文件中?

小智 6

现在有一个解决方案,解决方案是PyScript。这是一个 python 框架,使您能够在 HTML 中嵌入 python 脚本。查看下面的示例代码。

<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>

Run Code Online (Sandbox Code Playgroud)


Jor*_*rte 5

像这样的东西,如果要创建一个html,不一定要显示它:

 html_file = open('namehere.html','w')
 a = ['f','d','s','a']
 x = -1
 scope = vars()
 data = ''
 for i in a: #TIP: use a generator
     scope['x']+=1
     data += a[x]
     data += '\n'
 html_file.write(data)
 html_file.close()
Run Code Online (Sandbox Code Playgroud)