st.error() in Streamlit

The error method in Streamlit is used to display an error message box — typically shown in red color — to indicate that something went wrong (like an exception, invalid input, or system error).

Syntax

st.error(body, icon=None)    

The function has the following parameters:

Example: Basic Example.

Python

# Importing the Streamlit library
import streamlit as st

# Setting the title of the Streamlit app
st.title("Login Example")

username = st.text_input("Enter username:")
password = st.text_input("Enter password:", type="password")

if st.button("Login"):
    if username == "" or password == "":
        st.error("Username and password cannot be empty!")
    elif username == "admin" and password == "1234":
        st.success("Login successful!")
    else:
        st.error("Incorrect credentials. Please try again.")    

Explanation

The output of the above code is shown below:

Error method in Streamlit