Not sure if this is documented anywhere (apologies if so! :)) - I was wondering whether there was a way to prevent code that’s been commented out via triple quotes to be displayed in a Streamlit app?
For instance:
'''
multiple_files = st.file_uploader("Multiple File Uploader", accept_multiple_files=True)
for file in multiple_files:
file_container = st.beta_expander(f"File name: {file.name} ({file.size})")
data = io.BytesIO(file.getbuffer())
file_container.write(pd.read_csv(data, error_bad_lines=False))
file.seek(0)
dfs = [pd.read_csv(file) for file in multiple_files]
st.write("Print 1st dataframe")
st.write(dfs[0])
'''
This is a rant of mine, which I’ll spare you (at least to another time).
Most text editors let you select the lines in bulk, then use a shortcut (for example, CTRL + / in VS Code) to place a # in front of the code to make it a proper comment. I would suggest that is a much better alternative.
The only other way I see is to disable the “magic” feature in Streamlit’s options. Unfortunately, this will prevent streamlit from printing variables, strings, and such. It makes the use of st.write() or equivalent mandatory.
[runner]
# Allows you to type a variable or string by itself in a single line of Python code to write it to the app.
# Default: true
magicEnabled = false
Reading this helpful post made me realize that when we do something like:
"""
Some commented out code here
"""
we’re creating a string value, which Streamlit is designed to render by default. This is not that different from what Jupyter Notebooks do, they’ll render the text. There the solution I’ve used is this:
Approach 1 (works in Jupyter, doesn’t work in Streamlit)
# Added a semicolon at the end
"""
Some commented out code here
""";
The semicolon allows us to put multiple expressions into a single line. I’m guessing this “solution” works in Jupyter because the output of the cell is the value of the last expression, which happens to be empty.
That same thing doesn’t work in Streamlit: it appears that Streamlit will just try to render each expression, so the string will get rendered and then nothing else after it.
Approach 2 (works in Jupyter, works in Streamlit)
# Assigned to a variable
_ = """
Some commented out code here
"""
By assigning the comment string to a variable we end up with the value of the assignment statement, which is None, so nothing gets rendered.
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.