JsException(TypeError: Failed to fetch)

Viewed 826

When I try to import a pyscript source code to my HTML it shows a "JsException(TypeError: Failed to fetch)" error.

helloworld.py

print("Hello World")

testPyscript.html

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <title></title>
</head>
<body>
    <py-script src="helloworld.py">
        ("Another Text Test")
    </py-script>
</body>
</html>
2 Answers

For some reason your directory which contains helloworld.py and testPyscript.html need to run in localhost, open your folder in vsCode and install live server from extensions then in your right bottom corner press on Go Live. you will be directed to the default browser with the expected output from helloworld.py

I was having the same problem and found the answer here: PyScript: Loading Python Code in the Browser

The problem is <py-script src="helloworld.py"> do not support loading local files, you need a server for the browser to load it...

Go into the folder you keep the files and run python -m http.server 80 and then, on the browser, go to localhost/testPyscript.html

Hope it helps

Related