Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a better error if <py-env> paths is used in a local HTML file #257

Closed
antocuni opened this issue May 6, 2022 · 5 comments · Fixed by #311
Closed

Provide a better error if <py-env> paths is used in a local HTML file #257

antocuni opened this issue May 6, 2022 · 5 comments · Fixed by #311
Labels
type: feature New feature or request

Comments

@antocuni
Copy link
Contributor

antocuni commented May 6, 2022

paths: can only be used if the page is being served out of a webserver, but not out of an HTML page on the filesystem.

This is for good reasons and it's related to #179, but currently py-script fails in a very obscure way and the page just looks broken to the user. This is the full traceback as seen in chrome's console:

Access to fetch at 'file:///home/antocuni/anaconda/pyscript/pyscriptjs/examples/utils.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.

utils.py:1          Failed to load resource: net::ERR_FAILED

pyodide.asm.js:14 Uncaught (in promise) PythonError: Traceback (most recent call last):
  File "/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception
  File "/lib/python3.10/asyncio/tasks.py", line 234, in __step
    result = coro.throw(exc)
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 500, in eval_code_async
    await CodeRunner(
  File "/lib/python3.10/site-packages/_pyodide/_base.py", line 353, in run_async
    await coroutine
  File "<exec>", line 4, in <module>
  File "/lib/python3.10/site-packages/pyodide/http.py", line 231, in pyfetch
    url, await _jsfetch(url, to_js(kwargs, dict_converter=Object.fromEntries))
  File "/lib/python3.10/asyncio/futures.py", line 284, in __await__
    yield self  # This tells Task to wait for completion.
  File "/lib/python3.10/asyncio/tasks.py", line 304, in __wakeup
    future.result()
  File "/lib/python3.10/asyncio/futures.py", line 201, in result
    raise self._exception
JsException: TypeError: Failed to fetch

    at new_error (pyodide.asm.js:14:238191)
    at pyodide.asm.wasm:0xedbcb
    at pyodide.asm.wasm:0xf1a0e
    at method_call_trampoline (pyodide.asm.js:14:238105)
    at pyodide.asm.wasm:0x134c2c
    at pyodide.asm.wasm:0x217a84
    at pyodide.asm.wasm:0x174a14
    at pyodide.asm.wasm:0x135149
    at pyodide.asm.wasm:0x135243
    at pyodide.asm.wasm:0x1352e6
    at pyodide.asm.wasm:0x1fff83
    at pyodide.asm.wasm:0x1f98b5
    at pyodide.asm.wasm:0x135329
    at pyodide.asm.wasm:0x201f1b
    at pyodide.asm.wasm:0x1ff9ff
    at pyodide.asm.wasm:0x1f98b5
    at pyodide.asm.wasm:0x135329
    at pyodide.asm.wasm:0xf16d8
    at Object.Module.callPyObjectKwargs (pyproxy.gen.ts:360:23)
    at Object.Module.callPyObject (pyproxy.gen.ts:384:17)
    at wrapper (pyodide.asm.js:14:205222)

We should intercept this error and show the user a clear message and a link to some documentation which explains the issue and how to solve/mitigate it.

There are many open issues which are all related to this problem, e.g.:
#220
#217
#221

@antocuni
Copy link
Contributor Author

antocuni commented May 6, 2022

WORKAROUD
The easiest way to work around the issue is to use a local webserver.
For example, suppose to have a file called /tmp/pyscript-test/foo.html which uses <py-env> paths: ....
If you open foo.html inside chrome, it will not work.
To start a local webserver you can do the following:

$ cd /tmp/pyscript-test/
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Now you can open http://0.0.0.0:8000 and select foo.html from there, and it should work.

@SCrocky
Copy link

SCrocky commented May 9, 2022

This error is also present whenever a library install requires additional file on the PYPI server.
Ex:

<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
      <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
      <py-env>
 - msvcrt 
      </py-env>
    </head>
  <body>
	<div id="test"></div>
    <div id="test2"></div>
	<py-script output="test2">
import msvcrt 
print("keyboard OK")
	</py-script>
  </body>
</html>

We additionally get a CORS error:

Access to fetch at 'https://pypi.org/pypi/msvcrt/json' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

As well as a 301 error (CORS error) from pyodide.asm.js line 14.

@prasun3
Copy link

prasun3 commented May 10, 2022

Do we need to update the documentation since this is not expected to work

Check out the pyscriptjs/examples folder for more examples on how to use it, all you need to do is open them in Chrome.

@formazione
Copy link

formazione commented Jul 26, 2023

WORKAROUD The easiest way to work around the issue is to use a local webserver. For example, suppose to have a file called /tmp/pyscript-test/foo.html which uses <py-env> paths: .... If you open foo.html inside chrome, it will not work. To start a local webserver you can do the following:

$ cd /tmp/pyscript-test/
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Now you can open http://0.0.0.0:8000 and select foo.html from there, and it should work.

YES, it worked this way, localhost:8000/foo.html ( I mean I used this to do , using the server it loads the external script)

@ghost
Copy link

ghost commented Aug 4, 2023

I dont know why it says "The system cannot find the path specified."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants