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.
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.
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.
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.
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,
)
)
)
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking βAccept allβ, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.