In the below code execution the button operation Save to file is not working. I have added the print statement inside the Save as TXT Button (Execution is not going up to this print statement).
Steps:
- Click on TAKEOUT
- Click on Save as TXT
- elements_text data from st.text_area should be save in saves.txt file
import streamlit as st
extract_button = st.button(
'TAKEOUT',
key='extract_elements_button' )
if extract_button:
elements = ["List"]
if elements:
elements_text='Test_String'
tab1, tab2, tab3 = st.tabs(["Elements", "Screen", "Stats"])
with tab1:
st.text_area('Elements', elements_text, height=300)
# Add export options
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
save_path = st.text_input(
'Save to file:',
'saves.txt',
key='save_path',
help="Enter filename to save results"
)
with col2:
if st.button('Save as TXT', key='save_txt'):
print("Code is Not coming Here")
try:
os.makedirs(os.path.dirname(save_path), exist_ok=True)
with open(save_path, 'w', encoding='utf-8') as f:
f.write(elements_text)
st.success(f'Successfully saved to {save_path}')
except Exception as e:
st.error(f"Error saving file: {e}")
with col3:
if st.button('Save as JSON', key='save_json'):
try:
os.makedirs(os.path.dirname(save_path.replace('.txt', '.json')), exist_ok=True)
json_path = save_path.replace('.txt', '.json')
with open(json_path, 'w', encoding='utf-8') as f:
if 'elements_raw' in st.session_state:
json.dump(st.session_state['elements_raw'], f, indent=2)
else:
json.dump(elements, f, indent=2)
st.success(f'Successfully saved to {json_path}')
except Exception as e:
st.error(f"Error saving JSON: {e}")
with tab2:
# Display screenshots if available
elements_text1='Test_String1'
st.text_area('Elements1', elements_text1, height=300)
with tab3:
# Display element statistics
elements_text2 = 'Test_String2'
st.text_area('Elements2', elements_text2, height=300)