stlite
stlite copied to clipboard
Running Async code
Hi,
Is there a way to run async code (e.g. pyodide.http) in stlite? I tried asyncio.get_event_loop and it did work (nothing was executed). Is there a working example of it?
I tried asyncio.get_event_loop and it did work (nothing was executed)
I can't get the point.
asyncio.get_event_loop just returns the current event loop which is on the stlite env an instance of WebLoop, and does not execute anything.
Apart that, there is a blocking API to access remote resources, pyodide.http.open_url
import pyodide
import streamlit as st
url = "https://raw.githubusercontent.com/fivethirtyeight/data/master/airline-safety/airline-safety.csv" # Sample from streamlit-aggrid https://github.com/PablocFonseca/streamlit-aggrid
res = pyodide.http.open_url(url)
text = res.getvalue()
st.write(text)
Async, non-blocking API, pyodide.http.pyfetch is also there, but it is not good to use with stlite or Streamlit (#125 ) because Streamlit does not support the top-level await so the response cannot be passed to st.* such as st.write() in a normal way.
import pyodide
import streamlit as st
import asyncio
loop = asyncio.get_event_loop()
async def access_remote_test():
url = "https://raw.githubusercontent.com/fivethirtyeight/data/master/airline-safety/airline-safety.csv" # Sample from streamlit-aggrid https://github.com/PablocFonseca/streamlit-aggrid
res = await pyodide.http.pyfetch(url)
data = await res.bytes()
text = data.decode("utf8")
print(text)
loop.create_task(access_remote_test())
I'm not sure I understood you, sorry. Do you mean there is no way to run an async function in (normal) Streamlit, e.g. by waiting for the result or something? Sorry I have very limited experience in Python async/await
Do you mean there is no way to run an async function in (normal) Streamlit,
Yes.
Precisely, just running async function is possible as the second code block above, but using its result in a normal Streamlit script is difficult (maybe impossible).