Using st.write with $ sign gives formatted text

Hi, trying to write to st.chat_message but if there are 2 dollar signs in the text the text appears formatted. Any suggestions?

For example, if I have text like “from $5 - $10” the “5 -” part comes out formatted. Is there any way to avoid that being read as formatting?

Hi @shawngiese

Instead of using:

$5-$10

you can add a delimiter \ in front of the $ symbol:

\$5-\$10

Hope this helps!

Hi @dataprofessor, thanks but I am getting this from an online chat API or from user input. I guess I can go through and cleanup the input but I was hoping there was a way to disable certain format rendering within the text method. Or to use a better font and word wrap for st.text.

You can escape all dollar signs before rendering with str.replace("$", "\$"). Check this other post for an example:

Can you? You would have to deal with all Markdown syntax: square brackets for links, number symbols # for headers, pipes and dashes for tables, asterisks and (again) dashes for lists, numbers for numbered lists, allowed HTML tags (is there a list of them somewhere?), colons for emoji and colors…

st.text doesn’t do any formatting, it assumes the text is already formatted and displays it as is. For a better font I guess you can use CSS, but I would argue that for preformatted text monospaced fonts are better. For text wrapping use textwrap.

Thanks, I was starting to think that might become a bigger challenge than I expected… not always sure what content will be delivered from the api. Thanks for the tip on textwrap. Might get challenging since I am targeting desktop and mobile.