user input is not capturing the text

Question:

I am trying to capture the text using text input with button called "Email" whenever i click the button instead of displaying the text the box is closing automatically

I have tried but dont know where the issue is any idea would be appreciated

import streamlit as st #web app and camera
import numpy as np # for image processing 
from PIL import Image #Image processing 
import cv2 #computer vision 

def dodgeV2(x, y):
    return cv2.divide(x, 255 - y, scale=256)

def pencilsketch(inp_img):
    img_gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
    img_invert = cv2.bitwise_not(img_gray)
    img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)
    final_img = dodgeV2(img_gray, img_smoothing)
    return(final_img)


file_image = st.camera_input(label = "Take a pic of you to be sketched out")

if file_image:
    input_img = Image.open(file_image)
    final_sketch = pencilsketch(np.array(input_img))
    st.write("**Output Pencil Sketch**")
    st.image(final_sketch, use_column_width=True)
    if st.button("Download Sketch Images"):
        im_pil = Image.fromarray(final_sketch)
        im_pil.save('final_image.jpeg')
        st.write('Download completed')
#the issue code starts from here
        if st.button("Email"):
            form = st.form(key='my-form')
            name = form.text_input('Enter your name')
            submit = form.form_submit_button('Submit')
            st.write('Press submit to have your name printed below')
            if submit:
                st.write(f'hello {name}')
                
    
    else:
         st.write("You haven't uploaded any image file")
Asked By: Nazima

||

Answers:

Streamlit button has no callbacks. If you try to make other operations under the button, (I mean after clicking the button), the page will rerun, so you end up losing your entries.

But you can work around it by initializing a session state for the button. Also you can chose to use st.checkbox() if you don’t want to show the form right away.

 if st.checkbox("Click Here to Send Email"): # Modified
    form = st.form(key='my-form')
    name = form.text_input('Enter your name')
    submit_button = form.form_submit_button(label='Submit') # Modified
    st.write('Press submit to have your name printed below')
    if submit_button: # Modified
        st.write(f'hello {name}')

This should work fine

Answered By: Jamiu Shaibu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.