How to create columns dynamically

How to create columns dynamically based on user input from a slider ?

where the user select the value from the then the system iterate over the range of the value and create columns equal to it.

I tried to write the code but it did not work as it should where stars must be in Horizontal layout instead of Vertical

code:

import streamlit as st

x = st.slider('Evaluate the record',1,5)

    for i in range(x):
        cols = st.beta_columns(x)
        cols[i] = st.markdown(":star:")

where is the error in my code ?

here is my example code:

import streamlit as st
import string
import random

x = st.slider('choose a number',2,5)

def autocolumn(i):
	temp1=[]
	for x in range(i):
		temp1.append(random.choice(string.ascii_uppercase)+str(x))
	if i == 2:
		temp1[0:1]=st.beta_columns(2)
		with temp1[0]:
			st.markdown(":star:")
		with temp1[1]:
			st.markdown(":star:")

	elif i == 3:
		temp1[0:2]=st.beta_columns(3)
		with temp1[0]:
			st.markdown(":star:")
		with temp1[1]:
			st.markdown(":star:")
		with temp1[2]:
			st.markdown(":star:")
	elif i == 4:
		temp1[0:3]=st.beta_columns(4)
		with temp1[0]:
			st.markdown(":star:")
		with temp1[1]:
			st.markdown(":star:")
		with temp1[2]:
			st.markdown(":star:")
		with temp1[3]:
			st.markdown(":star:")
	elif i == 5:
		temp1[0:4]=st.beta_columns(5)
		with temp1[0]:
			st.markdown(":star:")
		with temp1[1]:
			st.markdown(":star:")
		with temp1[2]:
			st.markdown(":star:")
		with temp1[3]:
			st.markdown(":star:")
		with temp1[4]:
			st.markdown(":star:")

autocolumn(x)




this great thank you
But I have 2 questions :

  1. how to allow the user to select 1 star
  2. can i reduce the width of these columns because this function will be applied on each row where it will be displayed in column.

if you want to set the minum number is 1, you can add 2 rows code

if i == 1:
        st.markdown(":star:")

and to reduce the distance between two stars, you can use code like this:

if i == 2:
		A1,temp1[0],A2,temp1[1],A3=st.beta_columns([30,0.3,30,0.3,30])
		with A1:
			st.empty()
		with temp1[0]:
			st.markdown(":star:")
		with A2:
			st.empty()
		with temp1[1]:
			st.markdown(":star:")
		with A3:
			st.empty()

the code above doesn’t reduce the width of the columns

I find another solution, it is very easy.

x = st.slider('choose a number',1,5)

col1, col2, col3=st.beta_columns([2,0.2,2])
with col1:
	st.empty()
with col2:
	if x==1:
		st.markdown(":star:")
	if x==2:
		st.markdown(":star::star:")
	if x==3:
		st.markdown(":star::star::star:")
	if x==4:
		st.markdown(":star::star::star::star:")
	if x==5:
		st.markdown(":star::star::star::star::star:")
with col3:
	st.empty()





wowww this what i searched for thank you.

1 Like

you are welcome