1

I receive this error: "JsException: TypeError: NetworkError when attempting to fetch resource" when opening my html file that renders py-script code.

I have visited the numerous other posts on here about this error but none of them (that I've seen) seem relevant to my issue.

Here is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WeatherAPP</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
   <h1>Dash</h1>
   <p>bottom_line</p>
   <p>getting python code...</p>
   <div id="request_status"></div>
        <div id="request_text"></div>
   <py-script>
from pyodide.http import pyfetch
import asyncio
import json
response = await pyfetch(url="https://api.avalanche.org/v2/public/product?type=weather&center_id=MWAC&zone_id=1307", method="GET")
status = f"Request status: {response.status}"
response_dict = await response.json()

text = f"Text: {response_dict['id']}"

pyscript.write('request_status', status)
pyscript.write('request_text', text)
   </py-script>
</body>
</html>

Oddly, when I swap this API for a different, unrelated API, the pyscript succeeds and displays the result of the API that I am requesting. I've been trying to figure out why the API(s) I need do not work, and wonder if it's a matter of security, maybe.

I expected that this API would work smoothly as the other did.

TIA

Colter
  • 11
  • 2
  • For what it's worth, PyScript has moved along quite a bit since the `alpha` version you're using. If you're developing something new, I'd recommend shifting to the latest release, which is currently at `https://pyscript.net/releases/2023.03.1/pyscript.js`. (The CSS link is similar.) See the official docs for changes, but two major ones: `display()` is now favored over `pyscript.write()` and implicit coroutines are no longer allowed (use `async def` and `asyncio.ensure_future(...)`) – Jeff Glass Mar 20 '23 at 02:43
  • @JeffGlass thank you. I'll likely eventually move over to the latest version, but it'll require me to change a lot in my code. Could you list some advantages of it over alpha? I'd mainly be interested if they're speed or efficiency related. At the moment I've spent the last few weeks working on a weather project and alpha pyscript works flawlessly. – Colter Mar 20 '23 at 15:28
  • There have been several improvements to how local files are loaded, how content is written to the page, logging and error handling, and numerous bug fixes. The most recent version uses Pyodide 0.22.1 instead of Pyodide 0.20.0, which contains significant improvements in terms of functionality, proxy management, and available libraries. None of those in particular will make Python code run faster, but keep an eye for an upcoming release which moves the interpreter to a separate Worker thread, which frees up the main thread for other UI/events for free. – Jeff Glass Mar 21 '23 at 00:03

1 Answers1

1

I suspect you need to replace & to &amp; in your URL, otherwise you will try to fetch different URL.

When I save this snippet into page.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>WeatherAPP</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
   <h1>Dash</h1>
   <p>bottom_line</p>
   <p>getting python code...</p>
   <div id="request_status"></div>
        <div id="request_text"></div>
   <py-script>
from pyodide.http import pyfetch
import asyncio
import json

response = await pyfetch(url="https://api.avalanche.org/v2/public/product?type=weather&amp;center_id=MWAC&amp;zone_id=1307", method="GET")
status = f"Request status: {response.status}"

response_dict = await response.json()

text = f"Text: {response_dict['id']}"

pyscript.write('request_status', status)
pyscript.write('request_text', text)
   </py-script>
</body>
</html>

and load it into Firefox I get this response:

enter image description here

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 1
    Whoa, that easy! Thanks, works for me!! – Colter Mar 11 '23 at 23:54
  • I have another API that I'd like to get data from- it also gives me a network error, but it's a lot more simple. No ampersands.. www.jacksonhole.com/api/all.json – Colter Mar 12 '23 at 01:36
  • @Colter This has probably something to do with CORS: https://stackoverflow.com/questions/48362093/cors-request-blocked-in-locally-opened-html-file (You can open Web Developer Tools -> Console and reload the page. You should see the error there) – Andrej Kesely Mar 12 '23 at 01:45