Name "variable" is not defined

I am trying to access my variable but it say name is not defined, Here I read pdf file

def read_pdf(file):

	with open(file, "rb") as f:
	    pdf = pdftotext.PDF(f)
	st.write('button clicked',file[:-4]+'.txt')
	with open(file[4:-4]+'.txt', 'a') as f:
		for page in pdf:
			f.write("%s\n" % page)    
	return " ".join(pdf)

if st.button('read_pdf'):
	para = read_pdf('qna/'+filename)
	st.write('length of text: %s' % len(para))

Here I can get length of para.

sentence = st.text_input('Input your Question here:') 
if sentence:
	st.write('length of text: %s' % len(para))
	st.write('length of sentence: %s' % len(sentence))

But here it say

NameError: name 'para' is not defined

Hi @Talha_Anwar, welcome to the Streamlit community!

If I’m understanding the flow here, upon first loading of your app, if st.button('read_pdf'): will return False, because the user hasn’t pressed the button yet. Because of that, para will not get defined. leading to the error you describe.

Best,
Randy