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: