Cross origin requests error

Loading an external Python script gives this error

   <py-script src="index.py" output="mpl"> </py-script>

Access to fetch at 'file:///Users/derrickmwiti/PycharmProjects/Layer-videos/pyscript/index.py' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
base.ts:67          GET file:///Users/derrickmwiti/PycharmProjects/Layer-videos/pyscript/index.py net::ERR_FAILED
getSourceFromFile @ base.ts:67
await in getSourceFromFile (async)
evaluate @ base.ts:109
await in evaluate (async)
initializePyodide @ App.svelte:38
load (async)
listen @ index.mjs:412
listen_dev @ index.mjs:2015
mount @ App.svelte:53
mount_component @ index.mjs:1799
init @ index.mjs:1893
App @ App.svelte:48
(anonymous) @ main.ts:21
(anonymous) @ main.ts:23
pyodide.asm.js:14 Element.write: TypeError: Failed to fetch --> True
pyodide.asm.js:14 APPENDING: True ==> mpl --> TypeError: Failed to fetch
pyscript.ts:134 Collecting nodes...

How are you serving your HTML file? Since double-clicking it in a file browser?

Right-click and open in Chrome. I get this error

JsException(TypeError: Failed to fetch)

javascript - Allow Google Chrome to use XMLHttpRequest to load a URL from a local file - Stack Overflow provides information. This is a security feature of Chrome, you need to make extra files available via HTTP, and to have those servers CORS-accept from your page.

You could do this simply with python’s http module. “But pyscript needs no server” you may say; well it’s a little complicated. We will probably provide tooling to bundle python source files into a single download at some point; but for now use a development server.

1 Like