1

Pyscript allows one to run python inside a web browser. I have two python scripts I wrote that I’d like to use. One way to do this is to copy and paste the python code held in these files directly into the index.html file where the index file is part of a GitHub.io page. If possible however, I would rather load/Import them from a remote location. Currently, they reside in the gh-page branch on GitHub alongside the index.html file.

My question is whether this is possible? Most tutorials show how to load and import a local python file which I don’t want to do.

Update: This is my current attempt which I add to the index.html file:

<py-config>
  [[fetch]]
  from = "https://github.com/etc/blob/gh-pages/"
  files = ["myadd.py"]
</py-config>

When I try this I get the error message:

(PY0001): PyScript: Access to local files (using "Paths:" in ) is not available when directly opening a HTML file; you must use a webserver to serve the additional files. See this reference on starting a simple webserver with Python.

I want to avoid starting a server because this is meant to be client-side only approach with only a dumb file repo at the other end.

1
  • For the time being, I have put all the python code into the HTML file, which works. One issue I had was having to escape < and > chars (I"m writing out mathml), but once that was done it worked correctly. Commented Dec 21, 2022 at 4:54

1 Answer 1

0

There is a solution, and it's very simple.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you confirm this solution? The last time I checked, GitHub did not add the CORS headers to the response, which then causes an error in the browser. I might be wrong. As written your answer does not reference GitHub, just a file on the web server.
I’m using GitHub as my source of files, seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.