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?

2 Likes

Hi @shawngiese

Instead of using:

$5-$10

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

\$5-\$10

Hope this helps!

1 Like

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.

1 Like

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

1 Like

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.

1 Like

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.

1 Like

Hi, actually I ran into a similar problem.
streamlit throws some syntax warnings when I use \$
invalid escape sequence ‘\$’

I’m using it inside a st.write and f’string.

st.write(f'blabla bla {functionCall(param1)} are: \$ {x} bla bla and: \$ {y} for bla bla.')

if I remove the \ my text gets formatted :frowning:
Would love to get rid of the warning :slight_smile:
Any ideas?

1 Like

I could not replicate the warning, but do you see it if passing the string as a raw-string?

1 Like

Thx for the response.
Unfortunately passing the rawstring with fR does not resolve the problem.

What is really strange for me is, that after the warning code and or comments are displayed in the terminal.
I noticed that it only happens when I have 2 $ signs in a f string

As soon as I add the next $ sign in my f string I get the weird formatting and need to escape every $ with the backslash.

1 Like

This should really get fixed because it’s making chat output from GPT and other LLMs look buggy. It’s not a markdown standard to format with $, so why does Streamlit do it?

1 Like

Markdown comes in different flavors, the $ syntax is the standard in Jupyter’s markdown.


Relevant feature request:

1 Like