Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

streamlit
python

How to add the text elements to the Streamlit Web Interface

Educative Team

Overview

We’ll import streamlit to use the text elements. To import, we’ll use the following command:

import streamlit as st

A Streamlit web interface has the following text elements:

1. Title

It usually appears at the start of a Streamlit web app.

Syntax

st.title(body, anchor=None)

Parameters

  • body: This is the text to be displayed.
  • anchor: This is an optional parameter that can be accessed with an anchor in the URL.

2. Header

It displays the text in header formatting.

Syntax

st.header(body, anchor=None)

Parameters

  • body: This is the text to be displayed.
  • anchor: An optional parameter can be accessed with an #anchor in the URL.

3. Subheader

It displays the text in subheader formatting.

Syntax

st.subheader(body, anchor=None)

Parameters

  • body: It’s the text to be displayed.
  • anchor: An optional parameter can be accessed with an #anchor in the URL.

4. Markdown

It displays the string formatted as Markdown.

Syntax

st.markdown(body, unsafe_allow_html=False)

Parameters

  • body: This is the text to be displayed. It also supports Emoji shortcodes and LaTeX expressions.
  • unsafe_allow_html: Any HTML tags encountered in the body are automatically escaped and handled as plain text. By setting this option to True, you may disable this behavior.

5. Caption

It displays the text in small font. It should be used for captions, footnotes, asides, and sidenotes.

Syntax

st.caption(body, unsafe_allow_html=False)

Parameters

  • body: This is the text to be displayed.
  • unsafe_allow_html: Any HTML tags encountered in the body are automatically escaped and handled as plain text. By setting this option to True, you may disable this behavior.

6. Code block

It displays a code block with optional syntax highlighting.

Syntax

st.code(body, lanhguage ="python")

Parameters

  • body: The string is displayed as code.
  • language: This is the code language in which code is written.

7. Preformatted text

It displays the text with preformatted and fixed-width text.

Syntax

st.text(body)

Parameters

  • body: This is the string to be displayed.

8. LaTeX

It displays mathematical expressions formatted as LaTeX.

Syntax

st.latex(body)

Parameters

  • body: This is the string expression to be displayed.

Example

Let’s run the following app to display all text elements on the Streamlit web interface.

import streamlit as st
# Title
st.title ("This is a title")
# Header
st.header ("This is a header")
# Subheader
st.subheader ("This is a subheader")
# Mardown
st.markdown("This is **educative**")
# Caption
st.caption("This is caption")
# Code block

code = '''def func():
    "This is a Python Function"'''
st.code( code,language='python')
# Preformatted text
st.text("This is fixed-width text")
# Latex
st.latex(r'''
     f(x)=x^2
     ''')

Explanation

  • Line 1: We’ll import streamlit.
  • Lines 3–20: We’ll call the text elements functions to display them on the interface.

RELATED TAGS

streamlit
python
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring