Displaying Text with Streamlit
Streamlit provides several functions to display text in various formats. Whether you want to show simple messages, write markdown, or include code snippets, Streamlit offers an intuitive API for rendering text elements.
1. Using st.write()
for Simple Text
The st.write()
function is one of the most versatile functions in Streamlit. It can handle many types of content, including plain text, markdown, and dataframes. When you pass a string to st.write()
, it simply displays the text as a paragraph.
import streamlit as st
st.write("Hello, Streamlit!")
st.write("You can also display multiple items:", 123, 456, "text")
Output:
2. Adding Headers and Subheaders
Streamlit makes it easy to add different levels of headings to structure your app. You can use st.title()
, st.header()
, and st.subheader()
for main and subheadings in your application.
import streamlit as st
st.title("Main Title")
st.header("This is a Header")
st.subheader("This is a Subheader")
Output:
3. Displaying Markdown
Streamlit supports Markdown natively using the st.markdown()
function. This allows you to format your text with styles such as bold, italics, lists, and more.
- Headers: Use
#
for headers in markdown syntax. - Bold/Italic: Use
**text**
for bold and_text_
for italic. - Lists: Create bullet points using hyphens or asterisks.
import streamlit as st
st.markdown("# Markdown Header")
st.markdown("**Bold text** and _italic text_")
st.markdown("- Bullet point 1\n- Bullet point 2")
Output:
4. Displaying Code and Syntax Highlighting
Streamlit can also display code blocks using st.code()
and st.markdown()
with triple backticks. You can specify the language to enable syntax highlighting, making it easy to show code snippets in your app.
import streamlit as st
code = '''
def hello_world():
print("Hello, World!")
'''
st.code(code, language='python')
Output:
5. Writing LaTeX Equations
Streamlit allows you to display mathematical equations using LaTeX notation with the st.latex()
function. This is especially useful for data scientists and researchers presenting complex formulas.
import streamlit as st
# Function to display equation in a box with title
def display_equation(title, equation):
with st.container():
st.markdown(f"**{title}**")
st.latex(equation)
st.markdown("---") # Line separator for better visual structure
# Binomial Expansion
display_equation("Binomial Expansion", r"(a + b)^n = \sum_{k=0}^{n} {n \choose k} a^{n-k} b^k")
# Partial Derivative
display_equation("Partial Derivative", r"\frac{\partial f}{\partial x} = \lim_{\Delta x \to 0} \frac{f(x + \Delta x, y) - f(x, y)}{\Delta x}")
# Matrix Multiplication
display_equation("Matrix Multiplication", r"\begin{pmatrix} a & b \\ c & d \end{pmatrix} \times \begin{pmatrix} e & f \\ g & h \end{pmatrix} = \begin{pmatrix} ae+bg & af+bh \\ ce+dg & cf+dh \end{pmatrix}")
Output:
Conclusion
Displaying text in Streamlit is straightforward and flexible. Whether you're showing simple strings, formatting text with markdown, or rendering code and LaTeX, Streamlit provides a wide range of options to suit your needs. These text-display functions are essential for building informative and user-friendly applications.
Comments
Post a Comment