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

Add support for async generators in write_stream #8161

Open
2 tasks done
evertoncolling opened this issue Feb 15, 2024 · 5 comments · May be fixed by #8724
Open
2 tasks done

Add support for async generators in write_stream #8161

evertoncolling opened this issue Feb 15, 2024 · 5 comments · May be fixed by #8724
Labels
area:threading/processing feature:st.write_stream type:enhancement Requests for feature enhancements or new features

Comments

@evertoncolling
Copy link

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

Add support for async_generator in write_stream. This allows to stream data from async requests in Streamlit.

Why?

Today I can only stream data from synchronous HTTP clients while using write_stream in Streamlit.

How?

Add support for async_generator in write_stream.

Additional Context

No response

@evertoncolling evertoncolling added the type:enhancement Requests for feature enhancements or new features label Feb 15, 2024
Copy link

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@whitphx
Copy link
Contributor

whitphx commented Apr 28, 2024

I want this async support too to use write_stream on stlite because blocking code doesn't work on it.

@LukasMasuch
Copy link
Collaborator

@evertoncolling Thanks for the suggestion. We are considering adding this natively to write_stream. In the meantime, here is a workaround to transform async generators to sync generators and use them with st.write_stream:

import asyncio
from typing import AsyncGenerator

import streamlit as st

async def async_generator():
    yield "hello"
    await asyncio.sleep(2)
    yield "world"

def to_sync_generator(async_gen: AsyncGenerator):
    while True:
        try:
            yield asyncio.run(anext(async_gen))
        except StopAsyncIteration:
            break

st.write_stream(to_sync_generator(async_generator()))

@FilippTrigub
Copy link

@evertoncolling Thanks for the suggestion. We are considering adding this natively to write_stream. In the meantime, here is a workaround to transform async generators to sync generators and use them with st.write_stream:

import asyncio
from typing import AsyncGenerator

import streamlit as st

async def async_generator():
    yield "hello"
    await asyncio.sleep(2)
    yield "world"

def to_sync_generator(async_gen: AsyncGenerator):
    while True:
        try:
            yield asyncio.run(anext(async_gen))
        except StopAsyncIteration:
            break

st.write_stream(to_sync_generator(async_generator()))

This seem to work for 1 step and then break immediately.

@LukasMasuch
Copy link
Collaborator

@FilippTrigub do you have more information on how it breaks? Is there any kind of error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:threading/processing feature:st.write_stream type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants