Can someone explain the 'with' notation?


So i have been using columns for my layouts, but i just realised i could add with notations to the columns. I am wondering, why is ‘with’ important? How is it different from using just columns?

Steps to reproduce

Code snippet:

add code here

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Explain what you expect to happen when you run the code above.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.


  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Explanation in comment lines below. :slight_smile:

import streamlit as st
import pandas as pd

col1, col2, col3 = st.columns(3)

col1.write('# Column 1')
col2.write('# Column 2')
col3.write('# Column 3')

# When you want to add to a column:

# Option 1: Use a method on the column object (most of the same things you can 
# get using st.<method> in general)

col1.write('Line 2')
col1.code('import streamlit as st\n' \
          '\n' \

# Option 2: Use *with* and then indent all the things you are adding as you would
# with any conditional or loop. This allows you to skip putting the column name 
# in front of each line of code. It's personal taste and context that will determine
# which is stylistically preferable.

with col2:
    st.write('Say I want to do a lot of things in column 2 all at once. Maybe ' \
             'I\'ll just use `with` and indent those several lines of code.')

# You can mix and match using a method on the column option and using the with
# syntax.

with col3:

col3.write('Hi, again!')

# And of course you can add on to any column at any time, so you can go back and
# append to col1 at the end if you want.

col1.write('P.S. One more thing')