St.code() adds a tab to code automatically start from the second line in a tab container

This is because there is a tab at the beginning of each line of your triple-quoted string, due to it being nested inside of with tab1:. Two solutions:

  1. Don’t indent the code inside the triple-quote:
with tab1:
    st.code('''
import streamlit as st

st.write("Hello, Streamlit!")
st.write("Hello, Streamlit!")
    ''', 
    'python')

Option 2: from textwrap import dedent

with tab1:
    st.code(
        dedent('''
        import streamlit as st
    
        st.write("Hello, Streamlit!")
        st.write("Hello, Streamlit!")
        '''),
        "python",
    )
1 Like