Skip to main content

Streamline Text Display with st.markdown() Function

Streamline Text Display with st.markdown() Function

The st.markdown() function is a powerful tool in Streamlit for displaying formatted text. It allows you to use Markdown syntax to enhance the presentation of your text, making it an excellent choice for creating rich content in your application.


1. Displaying Basic Markdown

To display basic Markdown, you can pass a string containing Markdown syntax directly to st.markdown(). The text will be rendered according to Markdown rules, allowing you to include headings, lists, and other elements.

import streamlit as st

st.markdown("# This is a Heading\n## This is a Subheading")

Here is an example demonstrating how to display basic Markdown:

st.markdown("## Welcome to our application!")
st.markdown("- Item 1\n- Item 2\n- Item 3")

2. Using Markdown for Formatting

The st.markdown() function allows for various formatting options, including bold, italics, and links. You can create visually appealing text with minimal effort.

st.markdown("**This is bold text**")
st.markdown("*This is italic text*")
st.markdown("[Streamlit Documentation](https://docs.streamlit.io)")

This demonstrates how to apply formatting to your text:

  • **Bold text** appears bold.
  • *Italic text* appears italicized.
  • [Streamlit Documentation](https://docs.streamlit.io) creates a hyperlink.

3. Creating Lists and Tables

With st.markdown(), you can easily create ordered and unordered lists. You can also use Markdown to represent tables using pipes.

st.markdown("- Bullet 1\n- Bullet 2\n- Bullet 3")
st.markdown("| Column 1 | Column 2 |\n| -------- | -------- |\n| Data 1  | Data 2  |")

This will render as:

  • Bullet 1
  • Bullet 2
  • Bullet 3

And for tables:

Column 1 Column 2
Data 1 Data 2

4. Difference Between st.markdown() and Other Text Functions

While st.text() and st.write() can display text, st.markdown() provides the unique capability to format text using Markdown:

  • st.text() is for plain text, with no formatting options.
  • st.write() can display various data types, but lacks advanced formatting.
  • st.markdown() excels at rendering formatted text with Markdown.

Use st.markdown() when you want to create richly formatted content.


Example

import streamlit as st

st.markdown("# This is a Heading\n## This is a Subheading")
st.markdown("- Item 1\n- Item 2\n- Item 3")


st.markdown("**This is bold text**")
st.markdown("*This is italic text*")
st.markdown("Link: [Visit Rustcodeweb](https://www.rustcodeweb.com)")


st.markdown("- Bullet 1\n- Bullet 2\n- Bullet 3")
st.markdown("| Column 1 | Column 2 |\n| -------- | -------- |\n| Data 1  | Data 2  |")

Output:

Streamline-Text-Display-with-st.markdown()-Function

Conclusion

The st.markdown() function is an essential tool for displaying formatted text in Streamlit applications. By leveraging Markdown, you can create visually engaging content that enhances the user experience.

Comments