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.size})")
    data = io.BytesIO(file.getbuffer())
    file_container.write(pd.read_csv(data, error_bad_lines=False))
dfs = [pd.read_csv(file) for file in multiple_files]
st.write("Print 1st dataframe")


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.


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:


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.


# 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

Great! Thanks Synode! :pray: