Hi all, as the title suggests, I would like to have st.text_input and st.form_submit_button lie in the same line. Currently, my code looks like this:
instr = 'Hi there! Enter what you want to let me know here.'
with st.form('chat_input_form'):
if (prompt := st.text_input(
instr,
value=init_prompt,
placeholder=instr,
label_visibility='collapsed'
)) and st.form_submit_button('Chat'):
# Do something with the inputted text heere
Right now, the submit button is below rather than horizontally next to the input box. Is there any way to have st.text_input and st.form_submit_button in the same line?
You can use columns to get the design that you described. Here’s an example code to do that:
import streamlit as st
instr = 'Hi there! Enter what you want to let me know here.'
with st.form('chat_input_form'):
# Create two columns; adjust the ratio to your liking
col1, col2 = st.columns([3,1])
# Use the first column for text input
with col1:
prompt = st.text_input(
instr,
value=instr,
placeholder=instr,
label_visibility='collapsed'
)
# Use the second column for the submit button
with col2:
submitted = st.form_submit_button('Chat')
if prompt and submitted:
# Do something with the inputted text here
st.write(f"You said: {prompt}")
Hi Tony, sorry to bother you, but have you had a chance to look into my issue? I actually have a scheduled upcoming presentation, and I’d really appreciate it if you could provide me with some hints to fix it.
Full code below with adjustment based on code from @tonykip:
import streamlit as st
instr = 'Hi there! Enter what you want to let me know here.'
with st.form('chat_input_form'):
# Create two columns; adjust the ratio to your liking
col1, col2 = st.columns([9,1])
# Use the first column for text input
with col1:
prompt = st.text_input(
instr,
value=instr,
placeholder=instr,
label_visibility='collapsed'
)
# Use the second column for the submit button
with col2:
submitted = st.form_submit_button('Chat')
if prompt and submitted:
# Do something with the inputted text here
st.write(f"You said: {prompt}")
Note: you can use whatever numbers including large numbers to get your desired alignment in Streamlit, for example:
col1, col2 = st.columns([300, 145])
Please mark this as solution if this worked for you. Otherwise please note what is still an issue!
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.