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!