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

Hi guys!

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])
'''

Thanks,
Charly

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.

3 Likes

Hahaha shame, you know how much I love your rants Randy! :grin:

I’m actually using the hash in bulk option most of the time! :slight_smile:

But, in this specific case, I wanted a way to colour differentiate my commented-out code
(grey for commented out via hash, yellow via triple quotes)

I’m sure I could add colours to discarded code snippets via an extension, so that’s not an issue! :wink:

Cheers,
Charly

1 Like

Hello @Charly_Wargnier,

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
3 Likes

Great! Thanks Synode! :pray:

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.

5 Likes