Hello everyone,
Since it is (as far as I know) not possible to put the label of a widget (like st.selectbox, st.button,…) on its side, I’m using columns to achieve this effect, and since I don’t want to rewrite the same code each time, I wanted to try and create a custom context manager for it.
This is a very simplified example:
import streamlit as st
class CustomContextManager:
def __init__(self, label: str):
lc, rc = st.columns(
spec=[1, 2],
vertical_alignment='center',
border=False,
)
with lc:
st.write(label)
self.right_col = rc
def __enter__(self):
return self.right_col
def __exit__(self, *args):
pass
with CustomContextManager('This is a label on the left') as container:
container.selectbox(
label='test',
options=[1, 2, 3, 4],
key='selected_choice',
label_visibility='collapsed',
)
st.write(f'You choose option {st.session_state['selected_choice']}')
This is working perfectly fine, but I was wondering if there was a way to improve even more and get rid of the ‘as container’ and call st directly like this:
with CustomContextManager('This is a label on the left'):
st.selectbox(
label='test',
options=[1, 2, 3, 4],
key='selected_choice',
label_visibility='collapsed',
)
Thanks for any insight people can provide, and keep being awesome with Streamlit!