St.code on multiple lines

Summary

Hi, I want to use st.code for the copy button functionality but it shows the text in one line, even if it is long. I know i can format the text with \n but i do not want that.

Steps to reproduce

Code snippet:

import streamlit as st
st.code('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.')

Expected behavior:

The text should be formatted the same as st.markdown, but using st.code and keeping the copy to clipboard functionality. If the text is too long, it should be split on multiple lines. Without using \n.

Actual behavior:

The text is printed on same line, no matter how long it is.

Hi @Erica

How about formatting as follows:

import streamlit as st
text = '''
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
'''
st.code(text)
st.markdown(text)

Here, we can explicitly format the text by adding a new line at the specific location that we see fit. Using β€˜β€™β€™ to encapsulate the text content allows us to work with text on multiple lines.

Hope this helps!

Hi @dataprofessor, I have tried your code but it is not what I want. With the st.code, the text gets out of the box after the word tempor when it should continue on the next line, as the st.markdown. I want to make the format of st.markdown but with st.code, so I can also have the copy button without using st.button.

Here in this repo

they are making it as a new file and using text from there[because formatting everything is hard] so instead get the data from elsewhere

if st.button("Show Code"):
        file_path = code_files[PathToFile/File.txt]
        code = read_code_file(file_path)
        st.code(code, language='c')

This has nothing to do with what I am trying to accomplish. And it still shows the content in one line:

@erica Try using st.text_area instead.

This does not have the copy button that the st.code has. I need that copy button and the formatting behaviour from st.markdown. I tried using st.button but the page keeps refreshing and I have strange behaviour with it.

1 Like

One thing you can do is use css

import streamlit as st
from streamlit_extras.stylable_container import stylable_container

with stylable_container(
    "codeblock",
    """
    code {
        white-space: pre-wrap !important;
    }
    """,
):
    st.code(
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        * 10
    )

@Erica also, you can use textwrap library to force the wrapping on a specific length.

import streamlit as st
import textwrap as tw


st.code(
    "\n".join(
        tw.wrap(
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua",
            width=80,
        )
    )
)


Its working for me

text file
image

image

and u can type however u want and it will show like that

Thank you, this is what I was looking for. Thank you to others too for the effort! Wish you a great day to all of you :slight_smile:

Do you guys have an idea on how could I find the css code of the text showed with st.markdown? I want to make the st.code output look the same as st.markdown. I tried to look with dev mode but it does not work.

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.