streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
34.68k stars 3.01k forks source link

st.write_stream support for code #8166

Open sircambridge opened 7 months ago

sircambridge commented 7 months ago

Checklist

Summary

there should be a st.write_code_stream, that can stream code properly formatted while fading in with the typewriter effect.

Why?

the new st.write_stream is awesome! it works very well with langchain when using chain.stream as a generator that yields chunks. However, when using an LLM to generate code - which is often the case, comments starting with "#" and indentations trigger markdown formatting and blocks, making the code generation stream a janky mess. there is no stream version of st.text either, so there doesnt seem to be a way to stream code without causing the flashing janky mess.

How?

st.write_stream_code()

Additional Context

No response

github-actions[bot] commented 7 months ago

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

Asaurus1 commented 7 months ago

I also want this! And am happy to add it if product approves.

CHerSun commented 7 months ago

Can't you just st.write to st.empty on new chunk?

Asaurus1 commented 7 months ago

Can't you just st.write to st.empty on new chunk?

Yep, I've done this in some places. You can store the result of every generator yield in a buffer and then write the entire contents of the buffer to an empty DeltaGenerator every update / newline. But that does stop working well for longer output because it means you're sending the entire buffer string over the network for each update.

sircambridge commented 7 months ago

Agree! Using write and empty periodically is a pretty good idea, especially if its smart about yielding not every single token but maybe every 0.2 seconds or n number of tokens. I'll give this a try

However the syntax highlighting would be a super nice to have.

Asaurus1 commented 7 months ago

You get the syntax highlighting if you call st.code instead of write.

haberchr commented 1 week ago

Would there be anything more to minimally support this than adding a flag argument to write_stream(), and switching markdown() to text() here according to whether the flag is activated?: https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/write.py#L156

The better solution would be to add a new function text_stream() to https://github.com/streamlit/streamlit/blob/develop/lib/streamlit/elements/text.py basically the same as write_stream(), except replacing markdown() with text() everywhere. However, I imagine support for this new function name would need to be added in many places in the code.

I believe it's currently impossible to render the streamed input as code even with custom CSS until the markdown function is turned off.

This seems like a really useful feature. Is there any movement on this?