PyScript fetch - thoughts #1999
Replies: 3 comments
-
This is mostly a duplicate of this one: #1996 Once that lands (I am working on it as we speak) you can have The exception already mentions the URL and the status but it doesn't include the text, although, as you said:
absolutely yes! for PoC and RAD nobody wants the double await as in the 90% of the time you just keep writing
Once again though, with the other discussion I am splitting optionally anything in two parts so you do what you want and you still get in return python objects instead of JS proxies ... I hope that's welcome. # currently
result = await fetch(url).json()
# once my changes land
response = await fetch(url)
response.ok
response.status
response.headers
result = await response.json() As example, above code will produce the same result in both cases, the first one is for beginners, the latter one is for people actually knowing more about fetch and how it works and what it returns, but in both cases you will have a Python list or dictionary, not a JS proxy, with JSON, and same goes for bytearray and arrayBuffer. |
Beta Was this translation helpful? Give feedback.
-
btw ... about this ...
you can't have response text if you don't It's true that some service returns even |
Beta Was this translation helpful? Give feedback.
-
closing as this got merged addressing all concerns and allowing |
Beta Was this translation helpful? Give feedback.
-
Hello as promised I've been testing pyscript fetch and decided to start this discussion to provide some feedback, I know this is our first interaction and let me start by saying that it makes me very happy to not having to import stuff from outside pyscript and can just add the import to my various
from pyscript
statements.Error raising
This one was a bit of a surprise, I never expect that by making a request I could get an exception in python, I would like to be able to handle
!ok
responses by own way without having to either:Now, I understand why we are raising the exception - to chain .json-like methods to the call to fetch so you only use on await. - if we do raise an exception, I would expect to have these two things implemented:
Async flag
This might be a bit of a silly idea, but initially, I forgot to add
async
to the script tag. Could we perhaps just make top-level await work so we don't have to explain to users that they need to do this if they want to use fetch?I know that we had various discussions about this, and if I recall correctly, the reason why we decided not to allow top-level async was due to the pyscript code running at unexpected turns instead of a sync-like script. But I was wondering if perhaps we would like to revisit.
Chaining
I think this one was only unusual to me, given that I am used to use libraries such as aiohttp or httpx (within async), but I was a bit surprised that I could do
await fetch().json()
, not sure if we decided on this to make it easier for new folks to utilize fetch, but given the async nature of making requests in the browser, I'm not sure we should be worried about it because people will wonder what is this magicawait
thing anyway 🤔Finally, for context, here's a very basic pyscript.com example app using fetch and getting errors .
Beta Was this translation helpful? Give feedback.
All reactions