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

新手上路,请多包涵

我如何放置这个 python 脚本:

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

在一个html文件里面?

原文由 newbie pisan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 313
2 个回答

像这样,如果你想创建一个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()

原文由 Jorge Guberte 发布,翻译遵循 CC BY-SA 2.5 许可协议

现在有一个解决方案,解决方案是 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>

原文由 Sten Techy 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题