CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 07:54:35 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"7602a01e77922c41015e4252b02aa007"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com github.githubassets.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com wss://alive-staging.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com marketplace-screenshots.githubusercontent.com/ copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=0E0ZGrxOaYGiGd31GKjUYGjnK%2Byr1ZgK4dLOMp0BzZkZnhFJ6B16uAxuv8mqiAZxNRG1WeIy%2FYbdlBo%2FLZBHtCWZo6XW8Vd%2FD1Qm%2FLdqvtSatZYjdUT%2Bn76mI8oPCyonfhsTtcw8hPRSe7SrXUExQ8dYqYDvAZ2d9FTjhQlGiFeNIyhp%2FVigSaprCtZhYkZWhn0%2ByasJOTdmFnvuxbKwKj9tnym7%2BeuYEWVP%2B9NNgPGY%2BkegLyg%2Bs4IgtrDTZxxEbStDgnxHgU7I540QaHS%2Flg%3D%3D--H7VjOm9jFcJXkwQD--NwUTVPCoqxynfoIahAyAWA%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1868519169.1760255675; Path=/; Domain=github.com; Expires=Mon, 12 Oct 2026 07:54:35 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 12 Oct 2026 07:54:35 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: EAB0:136466:D41F26:11C7F2A:68EB5EBB
A container to have elements next to each other in streamlit by manipulating the styles. · GitHub
Show Gist options
Save ddorn/decf8f21421728b02b447589e7ec7235 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Last active
October 10, 2025 15:07
-
Star
3
(3)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ddorn/decf8f21421728b02b447589e7ec7235 to your computer and use it in GitHub Desktop.
A container to have elements next to each other in streamlit by manipulating the styles.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
MIT License | |
Copyright (c) 2024 Diego Dorn | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
""" | |
from contextlib import contextmanager | |
import streamlit as st | |
HORIZONTAL_STYLE = """ | |
<style class="hide-element"> | |
/* Hides the style container and removes the extra spacing */ | |
.element-container:has(.hide-element) { | |
display: none; | |
} | |
/* | |
The selector for >.element-container is necessary to avoid selecting the whole | |
body of the streamlit app, which is also a stVerticalBlock. | |
*/ | |
div[data-testid="stVerticalBlock"]:has(> .element-container .horizontal-marker) { | |
display: flex; | |
flex-direction: row !important; | |
flex-wrap: wrap; | |
gap: 0.5rem; | |
align-items: baseline; | |
} | |
/* Buttons and their parent container all have a width of 704px, which we need to override. We target the direct child and grand-child to avoid styling smaller elements, like toggles, that also use divs */ | |
div[data-testid="stVerticalBlock"]:has(> .element-container .horizontal-marker) > div > div, | |
div[data-testid="stVerticalBlock"]:has(> .element-container .horizontal-marker) > div { | |
width: max-content !important; | |
} | |
/* Just an example of how you would style buttons, if desired */ | |
/* | |
div[data-testid="stVerticalBlock"]:has(> .element-container .horizontal-marker) button { | |
border-color: red; | |
} | |
*/ | |
</style> | |
""" | |
@contextmanager | |
def st_horizontal(): | |
st.markdown(HORIZONTAL_STYLE, unsafe_allow_html=True) | |
with st.container(): | |
st.markdown('<span class="hide-element horizontal-marker"></span>', unsafe_allow_html=True) | |
yield | |
if __name__ == "__main__": | |
buttons = [ | |
"Allow", | |
"Deny", | |
"Always Allow", | |
"Edit", | |
"More Options", | |
] * 2 | |
st.header("With the new horizontal layout") | |
with st_horizontal(): | |
st.write("Confirm?") | |
st.button("✅ Yes") | |
st.button("❌ No") | |
with st_horizontal(): | |
for i, option in enumerate(buttons): | |
st.button(option, key=f"button_{i}") | |
st.header("With columns") | |
cols = st.columns(len(buttons)) | |
for i, option in enumerate(buttons): | |
cols[i].button(option, key=f"button_col_{i}") | |
st.header("Sample elements to check that we did not break anything") | |
st.button("A button") | |
st.button("Another button") | |
with st.expander("Code"): | |
st.code(""" | |
print("Hello, world!") | |
""", language="python") | |
cols = st.columns(3) | |
for i, col in enumerate(cols): | |
col.write(f"Column {i}") | |
col.button("Click me", key=f"col_{i}") | |
with st.container(border=True): | |
st.write("Inside container") | |
st.button("Click me", key="container") | |
st.button("Click me", key="container1") | |
with st_horizontal(): | |
st.write("Inside horizontal container") | |
st.toggle("Toggle", key="containera") | |
st.toggle("Toggle 2", key="containerb") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.
How can i made the button goes after the text , but the text can auto wrap like not in the st_horizontal() container.
like in the following code:
with st_horizontal():
st.,markdown("Confirm?")
st.button("✅ Yes")
st.button("❌ No")
if st.markdown(long_text),it can automatically wrap. But button still goes after the text.
Thanks