Any way to prevent commented-out code (via triple quotes) to be displayed in Streamlit?

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.

3 Likes