Manipulated text (make bold) elements in df

Dear Readers.

I am developing my first NLP-based app with Streamlit. I work as an editor and I am putting together a basic corpus search app for my colleagues who are writers. (They write materials for English language learners.)

For this app, the user will search for a keyword or phrase and the app returns sentences from the corpus which contain the target keyword or phrase. I have completed this functionality, but I want the target text (keys) to be in bold so that they are easy to identify within the sentences that are returned by the corpus search. At the moment, the text returned is uppercase, which sticks out so is easi-er to identify, but still not very nice looking.

I tried a few things but the ANSI code (I think that’s what it is called) shows in the dataframe displayed instead of actual bold font.

Here is the piece of code that I need help with:

output =
for elem in result:

            if key1 in elem: 
                output.append(elem.replace(key1, key1.upper()))

            elif key2 in elem:
                output.append(elem.replace(key2, key2.upper()))

outputdict = {'Target Language': output}

df = pd.DataFrame(outputdict)

Do I need to make a class, or something similar?

Thank you for your help and patience.

An idea is to convert the pandas dataframe to markdown before passing it to streamlit. Here is an example where a pattern is replaced by **pattern** to make it bold in markdown (Basic Syntax | Markdown Guide).


import streamlit as st
import re 
import pandas as pd 

stack = """And this difficulty attaches itself more closely to an age in which
    progress has gained a strong ascendency over prejudice, and in which
    persons and things are, day by day, finding their real level, in lieu
    of their conventional value. The same principles which have swept away
    traditional abuses, and which are making rapid havoc among the revenues
    of sinecurists, and stripping the thin, tawdry veil from attractive
    superstitions, are working as actively in literature as in society. The
    credulity of one writer, or the partiality of another, finds as
    powerful a touchstone and as wholesome a chastisement in the healthy
    scepticism of a temperate class of antagonists, as the dreams of
    conservatism, or the impostures of pluralist sinecures in the Church.
    History and tradition, whether of ancient or comparatively recent
    times, are subjected to very different handling from that which the
    indulgence or credulity of former ages could allow. Mere statements are
    jealously watched, and the motives of the writer form as important an
    ingredient in the analysis of his history, as the facts he records.
    Probability is a powerful and troublesome test; and it is by this
    troublesome standard that a large portion of historical evidence is
    sifted. Consistency is no less pertinacious and exacting in its
    demands. In brief, to write a history, we must know more than mere
    facts. Human nature, viewed under an induction of extended experience,
    is the best help to the criticism of human history. Historical
    characters can only be estimated by the standard which human
    experience, whether actual or traditionary, has furnished. To form
    correct views of individuals we must regard them as forming parts of a
    great whole—we must measure them by their relation to the mass of
    beings by whom they are surrounded, and, in contemplating the incidents
    in their lives or condition which tradition has handed down to us, we
    must rather consider the general bearing of the whole narrative, than
    the respective probability of its details.
needle = st.text_input("What to look for?")

if needle:
    pretty_stack = re.sub(f"({needle})",r"**\1**",stack,flags=re.IGNORECASE)
    pretty_stack = stack

cols = st.columns(2)

with cols[0]:
    "## Bold text"
    st.markdown(pretty_stack, unsafe_allow_html=True)   

with cols[1]:
    "## Bold words in a table"
    stack_as_dataframe = pd.DataFrame({"Text":pretty_stack.split("\n")})

Thank you! I’ll try that and see how I get on with the development.