st.code() in Streamlit

In Streamlit, the code display method is used to show source code directly inside your app UI. It is useful for displaying examples, debugging output, or sharing reusable code snippets with users.

Syntax

st.code(body, language="python", *, line_numbers=False, wrap_lines=False, height="content", width="stretch")    

Parameters

Example:

Python

import streamlit as st

code = """
def add(a, b):
    return a + b
"""

# Display formatted code block
st.code(code, language="python")    

The output of the above code is shown below:

st.code() in Streamlit