Display a code block with optional syntax highlighting.

Function signature[source]

st.code(body, language="python", *, line_numbers=False, wrap_lines=False)

Parameters

body (str)

The string to display as code.

language (str or None)

The language that the code is written in, for syntax highlighting. If None, the code will be unstyled. Defaults to "python".

For a list of available language values, see:

https://github.com/react-syntax-highlighter/react-syntax-highlighter/blob/master/AVAILABLE_LANGUAGES_PRISM.MD

line_numbers (bool)

An optional boolean indicating whether to show line numbers to the left of the code block. Defaults to False.

wrap_lines (bool)

An optional boolean indicating whether to wrap lines. Defaults to False.

Example

import streamlit as st

code = '''def hello():
    print("Hello, Streamlit!")'''
st.code(code, language="python")
forum

Still have questions?

Our forums are full of helpful information and Streamlit experts.