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.
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)
else:
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")})
st.markdown(stack_as_dataframe.to_markdown(None))
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.