gpt4 book ai didi

How do you handle requests in Pyodide / Stlite?(您如何处理Pyodide/Stlite中的请求?)

转载 作者:bug小助手 更新时间:2023-10-25 15:08:11 26 4
gpt4 key购买 nike



From what I've read so far Pyodide does not work with requests library. Usage of pyodide.http.pyfetch is recommended. But as it is async it really trips me and I can't figure it out (pyodide.http.open_url won't work as I'm sending get request to an api and I need to add some headers).

据我所知,到目前为止,Pyodide不能与请求库一起工作。建议使用pyodide.http.pyfetch。但是因为它是异步的,所以它真的让我绊倒了,我搞不懂(pyodide.http.open_url不起作用,因为我正在向API发送GET请求,并且我需要添加一些头)。


So the question is - how do make a request to an api and stop the further execution of a function until coroutine finishes?

所以问题是-如何向API发出请求并停止函数的进一步执行,直到协程结束?


I'm trying to get the data from the API, and use it to create an object. In regular Streamlit it works flawlessly.

我正在尝试从API获取数据,并使用它来创建对象。在普通的Streamlight中,它运行得天衣无缝。


class FabmanData:
LINKS: Final = {
"members": "members?orderBy=name&order=asc",
"resource": "resources?orderBy=name&order=asc",
"bookings": "bookings?order=desc&limit=50&summary=false",
}

def __init__(self) -> None:
self.members: pd.DataFrame = self.get_data("members")[
["id", "firstName", "lastName", "memberNumber"]
]
self.resources: pd.DataFrame = self.get_data("resource")[
["id", "name", "state"]
]
self.latest_bookings: pd.DataFrame = self.get_data("bookings")[
["id", "resource", "fromDateTime", "untilDateTime", "member"]
]

@staticmethod
def get_data(category) -> pd.DataFrame:
url = f"{BASEURL}{FabmanData.LINKS[category]}"
return pd.DataFrame(requests.get(url=url, headers=HEADERS).json())

def get_resources_dict(self):
return {
resource: resource_id
for resource, resource_id in zip(
self.resources["name"], self.resources["id"]
)
}

But I can't figure it out with pyfetch . I get TypeError: coroutine object is not subscriptable . Thus I assume that get_data method returns coroutine and I don't know what to do to make it return a value.
Currently it looks like this:

但我不能用PYFETCH解决这个问题。我收到TypeError:协程对象不可订阅。因此,我假设get_data方法返回协程,而我不知道如何使其返回值。目前它看起来是这样的:


    @staticmethod
async def get_data(category) -> pd.DataFrame:
url = f"{BASEURL}{FabmanData.LINKS[category]}"
response = await pyodide.http.pyfetch(url=url, headers=HEADERS)
await asyncio.wait_for(response, timeout=10)
data = pd.DataFrame(response.json())
return data

Any suggestions?

有什么建议吗?


更多回答
优秀答案推荐

Check out the pyodide-http package. It monkey-patches requests and urllib to work in a (mostly) browser-friendly way.

查看pyodide-http包。它用猴子修补请求和urllib,以(主要)浏览器友好的方式工作。



pyodide.http.pyfetch is the solution as you wrote.

Pyodide.http.pyfetch就是您所编写的解决方案。


You have to use it with await as the Pyodide document says because it's an async function and it returns a coroutine object that must be awaited to get the actual result. Note that the awaited result is a FetchResponse object, so you also have to use its method such as .string() or .bytes() to get the actual content (See the doc).
Here is an example:

正如Pyodide文档所说,您必须将其与等待一起使用,因为它是一个异步函数,并且它返回一个必须等待才能获得实际结果的协程对象。请注意,等待的结果是一个FetchResponse对象,因此您还必须使用它的方法,如.string()或.bytes()来获取实际内容(请参见文档)。下面是一个例子:


from pyodide.http import pyfetch

url = "https://example.com/some_file"
res = await pyfetch(url)
data = await res.bytes()

stlite allows to use await at the top level, so you can write code like above.

stlite允许在顶层使用await,所以你可以像上面那样编写代码。


Also, as usual, you can use pyodide.http.pyfetch in an async function with await. In that case, you would await the outer async function with a top level await like this:

此外,像往常一样,您可以在带有AWAIT的异步函数中使用pyodide.http.pyfetch。在这种情况下,您将等待外部异步函数,顶级等待如下所示:


from pyodide.http import pyfetch

async def main():
url = "https://example.com/some_file"
res = await pyfetch(url)
data = await res.bytes()

await main()

This section of stlite's README explains these things with the example of pyfetch.

Stlite自述文件的这一节以pyfetch为例解释了这些事情。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com