Is there a way to wrap a long list through a series of columns

I want to wrap a long list of items through three columns that are a fixed height, newspaper style, i.e.:


col1.            col2                         col3
text text.      continued from       continued from
text ...          col1 ....                   col2.

Ideally, some conditional logic to create a new “page” with the continued text running through the columns on that page.

Hi there @fredzannarbor ,

See below a quick snippet in case it helps. Two considerations:

  • The number of columns can be changed, so it works for other use cases and users.
  • The logic for splitting the list into equal chunks can be improved (right now the last column takes the remainder items, whenever the list of items is not divisible by the number of columns).
import streamlit as st

# Define the list of items
items = [f'Item {i}' for i in range(1,36)]

# Define the number of columns
NUMBER_OF_COLUMNS = 3
columns = st.columns(spec=NUMBER_OF_COLUMNS)
items_per_col = len(items) // NUMBER_OF_COLUMNS

# Loop through columns and render in each one an equal chunk of the items
for i,col in enumerate(columns):

    items_index_start = i*items_per_col
    items_index_end = (i+1)*items_per_col if i+1 != NUMBER_OF_COLUMNS else len(items)

    for item in items[items_index_start:items_index_end]:
        col.write(item)

Hope this helps!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.