Streamlit Text Gets Colourful

Your Streamlit text no longer has to be boring. Version 1.16 lets you colour markdown text.

Alan Jones
Data Visualization, Data Science and Python

--

Photo by Robert Katzki on Unsplash

You’ve been able to use coloured text in Streamlit Markdown strings for a long time but it has required using HTML embedded in the Markdown text.

And that can get a little clumsy:

st.markdown("The next word is <span style='color:red'>red</span>",
unsafe_allow_html=True)

All that just to make one word red.

Streamlit maintains that embedding HTML in your text can be insecure which is why you have to set a special argument to allow it. The rest of the extra code is an HTML <span>tag. This tag is a container that does nothing but enables us to make changes to the style of the content and that’s what we see in the code style='color:red'.

You can, of course, do much more with embedded HTML than just change the colour of text but that is the focus of this article.

Now, in version 1.16 of Streamlit, they have introduced some new syntax that lets you colour your text while avoiding the use of HTML. It’s not as flexible as using HTML but it is definitely neater.

st.markdown("The next word is :red[red]")

--

--