1 2 Formatting Text in Streamlit
Key Concepts
- Text Elements: Understanding the basic text elements in Streamlit.
- Styling Text: Applying styles to text elements.
- Interactive Text: Creating interactive text elements.
Text Elements
In Streamlit, you can display text using various functions such as st.write
, st.markdown
, and st.text
. Each function serves a different purpose:
st.write
: A versatile function that can display text, data, and more.st.markdown
: Used for rendering Markdown text.st.text
: Displays plain text without any formatting.
Styling Text
Streamlit allows you to style text using Markdown syntax within the st.markdown
function. You can apply styles such as bold, italic, and headings:
import streamlit as st st.markdown("**Bold Text**") st.markdown("_Italic Text_") st.markdown("# Heading 1") st.markdown("## Heading 2")
Interactive Text
Streamlit provides interactive widgets that can be used to create dynamic text elements. For example, you can use a slider to change the text displayed:
import streamlit as st value = st.slider("Select a value", 0, 100) st.write(f"The selected value is {value}")
Examples
Here are some examples to illustrate the concepts:
import streamlit as st st.title("Text Formatting in Streamlit") st.markdown("## Basic Text Elements") st.write("This is a simple text using st.write.") st.markdown("**Bold Text** and _Italic Text_ using st.markdown.") st.text("Plain text using st.text.") st.markdown("## Interactive Text") name = st.text_input("Enter your name") st.write(f"Hello, {name}!")
By mastering these concepts, you can effectively format and display text in your Streamlit applications, making them more engaging and informative.
© 2024 Ahmed Baheeg Khorshid. All rights reserved.