I’m trying to display user and assistant chat on opposite sides when streaming like a conversation. Normally this is enabled by setting is_user=True in the message component. is_user=True displays the message from user on left and assistant on right
but while streaming is enabled we display content (user or assistant) message using st.markdown. In this case what is the workaround to setup something similar to the capability mentioned
This should be fairly independent of versions, and works by inserting an empty span with a specific class into each chat message, and then styling based on that class (in this case, it looks for a message that contains something the class chat-assistant)
import streamlit as st
st.write("Streamlit version", st.__version__)
messages = [
{
"author": "user",
"message": "hi",
},
{"author": "assistant", "message": "I'm a bot"},
] * 3
for message in messages:
with st.chat_message(message["author"]):
st.html(f"<span class='chat-{message['author']}'></span>")
st.write(message["message"])
st.html(
"""
<style>
.stChatMessage:has(.chat-assistant) {
flex-direction: row-reverse;
text-align: right;
}
</style>
"""
)
Do you tried any other solution as this methods takes st.emotion_cache as caches where it works only in local host. If we try it with Network URL or Deployment, the cache is changing from system-to-system…!
Make sure you are using the same version of streamlit locally and when deployed. This should ensure that you will get the same classes on everything. You can ensure this on Community Cloud by adding streamlit==<some version> in requirements.txt, and installing that same version locally.
Though my version above should still work, you can now add a class implicitly by adding a container with a key, like this: Playground app
I tried with Network URL in another system. But the emotion-cache like “st-emotion-cache-1wmy9hl e1f1d6gn1 for user content” that automatically generated in my browser as cache.
This is chaning from browsert to browser. How to make it same for all the browser. This is the problem i facing…
I don’t recommend trying to use the st-emotion classes whenever possible and instead focus on classes and data-ids that have friendlier names without hashes.
From Streamlit 1.40.2, here’s some better classes or data-ids to grab from: