Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Leading first line spaces trimmed by st.code() #6302

@ya55en

Description

@ya55en

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

Rendering a code block using st.code() or st.markdown() where the block's first line has leading spaces results in these leading spaces (of the first line only) trimmed.

Reproducible Code Example

Open in Streamlit Cloud

import streamlit as st

block = """
    line 1
    line 2
    line 3
"""

st.code(block)
st.markdown(f"```\n{block}\n```")

Steps To Reproduce

  1. Paste the above example into a python file (like example.py)
  2. Run it with Streamlit (e.g. streamlit run example.py)
  3. Point your browser to the URL listed.

Expected Behavior

The code block should render as

    line 1
    line 2
    line 3

Current Behavior

Block is rendered as

line 1
    line 2
    line 3

Is this a regression?

  • Not sure.

Debug info

  • Streamlit version: 1.20.0
  • Python version: 3.10.10
  • Operating System: Ubuntu 22.04
  • Browser: Firefox 110.0.1
  • Virtual environment: poetry 1.3-built

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:markdownRelated to Markdown renderingfeature:st.codeRelated to the `st.code` elementpriority:P3Medium prioritystatus:confirmedBug has been confirmed by the Streamlit teamtype:bugSomething isn't working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions