如何在HTML代码中链接到Python文件

huangapple go评论67阅读模式
英文:

How to link to python file inside html code

问题

So I have created a game (of sorts) in python that I want to embed into an html webpage so I can add UI features. I have used the <py-script> tags to do this but I am having issues with importing packages and also it clutters up the code. So is there a way to link to the python file instead, like I would a JS or CSS file?

I apologise in advance for any ambiguity or poor phrasing in my question, I am new to programming and don't really have anyone to turn to when I need help so I have to use SO for even the most minor errors.

英文:

So I have created a game (of sorts) in python that I want to embed into an html webpage so I can add UI features. I have used the <py-script> tags to do this but I am having issues with importing packages and also it clutters up the code. So is there a way to link to the python file instead, like I would a JS or CSS file?

I apologise in advance for any ambiguity or poor phrasing in my question, I am new to programming and don't really have anyone to turn to when I need help so I have to use SO for even the most minor errors.

答案1

得分: 2

如果您正在使用&lt;py-script&gt;标签,您可以使用src属性引用包含相关Python代码的URL。在这种情况下,标签内部(即在HTML页面中)编写的任何代码都将被忽略。例如:

&lt;py-script src=&quot;some/url/with/code.py&quot;&gt;&lt;/py-script&gt;

请注意,该属性是一个URL,而不是本地文件路径,因此您可能需要使用一个小型服务器程序来使Python文件在网络上可用。从命令行运行python -m http.server即可。

英文:

If you’re using a &lt;py-script&gt; tag, you can use the src attribute to reference a URL where the relevant python code is located. In this case, any code written within the tag itself (that is, in the HTML page) is ignored. For example:

&lt;py-script src=&quot;some/url/with/code.py&quot;&gt;&lt;/py-script&gt;

Note that the attribute is a URL, not a local file path, so you’ll likely want to use a small server program to make the python file available on the network. Running python -m http.server from the command line will do.

huangapple
  • 本文由 发表于 2023年2月6日 02:45:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定