Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical alignment for columns #3052

Open
asaini opened this issue Mar 31, 2021 · 24 comments · May be fixed by #8568
Open

Vertical alignment for columns #3052

asaini opened this issue Mar 31, 2021 · 24 comments · May be fixed by #8568
Labels
feature:st.columns status:in-progress We're on it! type:enhancement Requests for feature enhancements or new features

Comments

@asaini
Copy link

asaini commented Mar 31, 2021

Problem

As a user, I'd like to have the ability to vertically align within a container. While vertical alignment is not a problem in single columns, if there are 2 columns side by side then not having vertical alignment makes the contents look off.

Screen Shot 2021-03-30 at 11 19 38 PM

Code to repro

import streamlit as st

col1, col2 = st.beta_columns(2)

with col1:
    st.title('Streamlit Template: Big Chart!')

with col2:
    st.write('Explainer text about something. Lorem ipsum')

Adding padding artificially somewhat mitigates the issue but it is not a perfect solution either

import streamlit as st

col1, col2 = st.beta_columns(2)

with col1:
    st.title('Streamlit Template: Big Chart!')

with col2:
    st.write('')
    st.write('')
    st.write('')
    st.write('Explainer text about something. Lorem ipsum')

Screen Shot 2021-03-30 at 11 25 13 PM


Community voting on feature requests enables the Streamlit team to understand which features are most important to our users.

If you'd like the Streamlit team to prioritize this feature request, please use the 👍 (thumbs up emoji) reaction in response to the initial post.

@asaini asaini added type:enhancement Requests for feature enhancements or new features status:needs-triage Has not been triaged by the Streamlit team labels Mar 31, 2021
@vdonato vdonato removed the status:needs-triage Has not been triaged by the Streamlit team label Apr 3, 2021
@ushinghal19
Copy link

Hi, has there been any update on this feature? I am looking for something similar.

@Maarten-vr
Copy link

I have a similar request, though the solution might be a bit simpler.
In my case I want to align pictures to the bottom of the columns, instead of the default alignment at the top of each container.

Example:
Screenshot 2021-05-18 at 16 49 16
Current behaviour is on the left side; it would be nice to have an option to align at the bottom to achieve the right side.

Reproducable code:

import streamlit as st

urls = ["https://images.pexels.com/photos/1170986/pexels-photo-1170986.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=350&w=860",
        "https://images.pexels.com/photos/2014027/pexels-photo-2014027.jpeg?auto=compress&cs=tinysrgb&h=250&w=760",
        "https://images.pexels.com/photos/4056753/pexels-photo-4056753.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260",
        "https://images.pexels.com/photos/172421/pexels-photo-172421.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260"]

caption = ["Cat-A", "Cat-B", "Cat-C", "Cat-D"]

cols = {}
for i in range(0, 4, 2):
    cols[i], cols[i+1] = st.beta_columns(2)
    cols[i].image(urls[i], caption=caption[i], use_column_width=True)
    cols[i+1].image(urls[i+1], caption=caption[i+1], use_column_width=True)

@pjgao
Copy link

pjgao commented Jul 16, 2021

same features needed, any update?

@sbarham
Copy link

sbarham commented Dec 30, 2021

Indeed -- I'm looking for the same functionality. Any updates?

@sddhrthrt
Copy link

Would love this feature, will stay tuned!

@kkpsiren
Copy link

Any updates?

@thinklm
Copy link

thinklm commented Mar 30, 2022

Any updates? Really need this haha

@jrieke
Copy link
Collaborator

jrieke commented Mar 30, 2022

Hey all! We'll be working on layout options this summer and we definitely want to improve support for alignment. Don't know yet in detail how it will work, but we are all very aware of the pain to align things inside of columns! ;)

@msquaredds
Copy link

msquaredds commented Jun 9, 2022

Thanks @jrieke! I was just experience the same issue, so wanted to share my example too as another use case

I'm aligning text/numbers/charts across columns and would like for them to all stay in a consistent row

By the way, I think I could do the text/number part with a dataframe, but it doesn't solve the chart piece. I tried containers too and didn't have any luck

import streamlit as st
import plotly.graph_objects as go

st.set_page_config(layout="wide")

col1, col2, col3, col4, col5, col6, col7 = st.columns(7)

with col1:
    st.markdown('This is a long line that will wrap if the screen is not wide')
    st.markdown('Just a short line')
    
    # alternate version with a container per column, still not working
    #c1 = st.container()
    #c1.markdown('This is a long line that will wrap if the screen is not wide')
    #c1.markdown('Just a short line')
    
    # third version with a container per column and row, still not working
    #c3 = st.container()
    #c3.markdown('This is a long line that will wrap if the screen is not wide')
    #c4 = st.container()
    #c4.markdown('Just a short line')
    
with col2:
    st.write(123456789)
    st.write(321)
    
    # alternate version with a container per column, still not working
    #c2 = st.container()
    #c2.write(123456789)
    #c2.write(321)
    
    # third version with a container per column and row, still not working
    #c5 = st.container()
    #c5.write(123456789)
    #c6 = st.container()
    #c6.write(321)
    
with col3:
    chart_values = [566559, 786901, 1810183, 2257403, 1355262]
    trend = go.Figure()
    trend.add_trace(go.Line(y=chart_values))
    trend.update_layout(width=250, height=250,
        xaxis={'showticklabels': False},
        yaxis={'showticklabels': False})
    st.plotly_chart(trend)

Output:

image

@Khalilsqu
Copy link

I want to align widgets horizontally. Is it possible with multiple columns container? Any help is appreciated!

I really appreciate any help you can provide.

image

@Khalilsqu
Copy link

Tried the approach below, but no luck!!

image

@ssp24
Copy link

ssp24 commented Aug 8, 2022

I'd love to be able to "center" an item in a column vertically. Is there already a workaround to achieve this? Otherwise I'll hope that the summer updates will help with this as well :).

@msquaredds
Copy link

@Khalilsqu you can't yet, that's why people are requesting that feature here. The request seems to be in the process from the streamlit team, but maybe someone can shine more light on where that stands/an ETA.

I've only found workarounds for table-like data, using st.dataframe or st.table, or creating a HTML table (and then using st.markdown(, allow_unsafe_html=True). For text boxes and drop downs, I'd be curious if anyone's found a good solution too - I've just done something like inserting st.write('') to force a extra space in one of the columns.

@Khalilsqu
Copy link

@msquaredds Thanks a lot for your answer. This is really unfortunate!!

@naomilago
Copy link

Any update? I'm struggling to vertically align this lottie animation centered with the body text...

image

@nikhil191991
Copy link

We are desperately waiting for this feature. Is there an ETA for this issue?

@sfc-gh-kbregula
Copy link
Contributor

sfc-gh-kbregula commented Sep 26, 2022

The alignment method can be controlled using CSS styles.

import streamlit as st
from PIL import Image


left, right = st.columns(2)

with left:
    st.markdown("""
# Hello
## It is n awesome app
""")

with right:
    st.image("https://cataas.com/cat")

st.write(
    """<style>
    [data-testid="stHorizontalBlock"] {
        align-items: center;
    }
    </style>
    """,
    unsafe_allow_html=True
)

Screenshot 2022-09-26 at 15 00 07

@ZEOwen
Copy link

ZEOwen commented May 9, 2023

The above works fine for text and images, but we still would like to see the possibility to align other streamlit objects like buttons and text inputs.

@AyhamSaffar
Copy link

@carolinedlu did this enhancement not get past the "added-voting-callout" stage last year?

@BertPlasschaert
Copy link

BertPlasschaert commented Jul 28, 2023

I've added this in my column, just before the streamlit object. This is far from pixel-perfect but was the best/only option I could find without affecting every reference of that object's class.
st.markdown("<br><br><br><br><br>", unsafe_allow_html=True)

@CoolShades
Copy link

Still eagerly awaiting this function...

@cuipengfeily
Copy link

cuipengfeily commented Jan 3, 2024

grid = st.columns([3, 2, 2, 2])
grid[0].text("column 1")
grid[1].text("column 2")
grid[2].text("column 3")
grid[3].text('column 4')
for i in range (10):
    grid = st.columns([3, 2, 2, 2])
    grid[0].text_input("inmput", "input", key= f'input_text_row{i}',label_visibility="collapsed", disabled=True)
    grid[1].selectbox("sel", ["a", "b", "c"] , key=f'input_sel_row{i}', label_visibility="collapsed")
    grid[2].button('button', key=f'button_row{i}')
    grid[3].markdown('This is a long line that will wrap if the screen is not wide')

image

@jorenverspeurt
Copy link

Having columns(..., align: Literal["top", "middle", "bottom"] = "top", ...) would be amazing.
Having a grid element on top of the columns might help for the kinds of apps people would like to use this feature for as well, might be a cleaner solution than having to do it with columns and control over alignment and spacing. Something like grid(columns: SpecType, rows: SpecType, align_x: ..., align_y: ..., gap: ...)?

@jrieke
Copy link
Collaborator

jrieke commented Apr 27, 2024

Hey all!

We started building an early prototype for this feature. It simply adds a parameter vertical_alignment="top"|"center"|"bottom" to st.columns. An early demo app with a link to the wheel file is here: https://vertical-alignment.streamlit.app/

I also added a few of the examples above to the demo app, thanks a lot to everyone who provided one! ❤️

We're still iterating on this since it doesn't yet solve all problems (especially aligning widgets with different sizes); see comments in the demo app. But if you give the wheel file a spin and have feedback, please let us know here!

Btw re grid: there's a grid layout in streamlit-extras that you can give a try. We might also integrate something like this into Streamlit in the future but I don't have a timeline yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:st.columns status:in-progress We're on it! type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.