I am having issues with text display

Ocassionally streamlit will display some parts of text in a weird manner. Does anyone know if there is a way to minimize this risk? Thank you

Hey @text2sql,

Thanks for sharing this question! Can you share the code snippet that resulted in this?

these are the parts that i think are the most relevant to the issue, it’s from a search_and_display function in my code. β€œresults” is taken from a database, so i don’t think there is any formatting issue originating from there

res = index.query(xq, top_k=20, include_metadata=True)

  result_strings = [match['metadata']['text'] for match in res['matches']]
  results = co.rerank(...) 
  for result in results:
    if result.relevance_score >= relevance_score_slider:
      display_result(result)

Is result definitely a string? What happens when you add st.write(type(result)) right before the display_result(result) line?

Also, what method is display_result() using? Is it just writing the text with st.write?

main()classcohere.responses.rerank.RerankResult(document: Dict[str, Any] = None, index: int = None, relevance_score: float = None, *args, **kwargs) β†’ None

display_result uses st.markdown

shall i just use st.write instead? there is an embedded url though [Read full opinion]({url}

What’s obvious , is that this weird font display happens to the parts of the text located between any numbers. If that helps.

According to the documentation, st.markdwn() supports

LaTeX expressions, by wrapping them in β€œ$” or β€œ$$” (the β€œ$$” must be on their own lines). Supported LaTeX functions are listed at Supported Functions Β· KaTeX.

Hi @text2sql, before displaying the string (result), you could pass it through a function that strips the result of all known special characters, OR only allows numbers and alphabets in the result string.

Cheers

but why to use LaTex ? there is none. it is just a text

there are no unusual characters in the source text. the example above is β€œβ€¦ 500,000. The trial court reduced the award to 400,000.” Because that text is between two numbers, streamlit converts it to some weird format.

As explained in the above quote from the docs, text wrapped by β€œS” or β€œSS” signs is rendered as LaTeX.

Got it now. Thanks. But this is rule is wrong. There is a special language st.latex and if I don’t use one, and use st.write, it should not be overwritten by anything else. Are you saying any text that contains more than one dollar sign will automatically turn into LaTex?

btw, that rule applies to st.markdown, i am using st.write

Hi @text2sql

Unfortunately, a pair of $ encompassing text would be seen as Latex. You could use an escape character \$ so that this does not happen.

Perhaps you can do the following to add the escape character:

your_string.replace(β€œ$”, β€œ\$”)

Hope this helps!

this fix worked. thank you. but why not to have that rule apply to st.markdown only, but not st.write?

If you use st.write(), then yes. st.write() will call st.markdown() if it is passed a string. Unfortunately, streamlit support for displaying just text is quite limited. You can use st.text() or st.code() but you cannot even have text wrapping without injecting custom CSS. You can also escape special characters but I don’t think it would solve every problem. With markdown indented text is rendered as <blockquote>, consecutive blanks and newlines are collapsed into one, etc.

I have seen discussions on how to improve this situation in some github issue but I don’t know if there are any actual plans.

Thank you! This temporary fix helped for now your_string.replace(β€œ$”, β€œ\$”)