英文:
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
如果您正在使用<py-script>
标签,您可以使用src
属性引用包含相关Python代码的URL。在这种情况下,标签内部(即在HTML页面中)编写的任何代码都将被忽略。例如:
<py-script src="some/url/with/code.py"></py-script>
请注意,该属性是一个URL,而不是本地文件路径,因此您可能需要使用一个小型服务器程序来使Python文件在网络上可用。从命令行运行python -m http.server
即可。
英文:
If you’re using a <py-script>
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:
<py-script src="some/url/with/code.py"></py-script>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论