Open sircambridge opened 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.
I also want this! And am happy to add it if product approves.
Can't you just st.write to st.empty on new chunk?
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.
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.
You get the syntax highlighting if you call st.code
instead of write
.
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?
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