Thank you Arvindra for your explanations. It helped me much. I solved my problem after the first explanation and looking at session_state in streamlit.
Here is my script of the solution, (please check for any improvement):
import random
import streamlit as st
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import pyautogui
import time
def sentmail2():
gmailUser = '*********@gmail.com'
gmailPassword = '********'
recipient = str(email)
fixed_digits = 4
key=random.randrange(1111, 9999, fixed_digits)
st.session_state.key=key
message = str(st.session_state.key)
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = str()
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
except:
print ('Something went wrong...')
st.text('please check your email...')
if "submit_e_button" not in st.session_state:
st.session_state.submit_e_button=False
st.session_state.key=0
key=0
def callback():
st.session_state.submit_e_button = True
if "submit_button" not in st.session_state:
st.session_state.submit_button=False
st.session_state.key=0
key=0
def callback_2():
st.session_state.submit_e_button = True
st.session_state.submit_button=True
if "confirm_enter_button" not in st.session_state:
st.session_state.confirm_enter_button=False
def callback_3():
st.session_state.submit_e_button = True
st.session_state.submit_button=True
st.session_state.confirm_enter_button=True
def ref_page():
pyautogui.hotkey("ctrl","F5")
if st.session_state.submit_button == False and st.session_state.submit_e_button == False:
email_text=st.text_input(label='Please enter your email address', placeholder='***@***.com', on_change=callback,disabled=False)
submit_e_button=st.button(label='Send',on_click=callback,disabled=False)
email=email_text
else:
email_text=st.text_input(label='Please enter your email address', placeholder='***@***.com', on_change=callback,disabled=True)
submit_e_button=st.button(label='Send',on_click=callback,disabled=True)
email=email_text
if submit_e_button or st.session_state.submit_e_button:
if st.session_state.submit_button == False and st.session_state.submit_e_button == True:
sentmail2()
if st.session_state.submit_button == False and st.session_state.submit_e_button == True:
submit_button=st.text_input(label='Enter the code', placeholder='****', on_change=callback_2,disabled=False)
confirm_enter_button=st.button(label='Onayla',on_click=callback_2,disabled=False)
code=submit_button
else:
submit_button=st.text_input(label='Enter the code', placeholder='****', on_change=callback_2,disabled=True)
confirm_enter_button=st.button(label='Onayla',on_click=callback_2,disabled=True)
code=submit_button
if submit_button or st.session_state.submit_button:
if str(code)==str(st.session_state.key):
st.text('succcessfully verified e-mail: ')
str(st.text(email))
if str(code)!=str(st.session_state.key):
st.text('code is not true, please wait to send another code again.')
st.session_state.submit_e_button == True
ph = st.empty()
N = 1*30
for secs in range(N,0,-1):
mm, ss = secs//60, secs%60
ph.metric("Countdown", f"{mm:02d}:{ss:02d}")
time.sleep(1)
if ss==1:
ph.metric("Countdown", "Time is up")
try_again=st.button(label='Click to try again.',on_click=ref_page,disabled=False)
Now, I am trying to integrate a countdown clock for the verification process. The part that I want to integrate into my script is:
ph = st.empty()
N = 1*60
for secs in range(N,0,-1):
mm, ss = secs//60, secs%60
ph.metric("Countdown", f"{mm:02d}:{ss:02d}")
time.sleep(1)
t2=time.time()
t=t2-t1
if ss==1:
ph.metric("Countdown", "Time is up")