How to link to python file inside html code

Question:

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

Asked By: AtomProgrammer

||

Answers:

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.

Answered By: Jeff Glass
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.