I’m very new to streamlit library. Currently, I’m building OCR application with streamlit. My current flow is that the output of OCR is shown at the textarea and I want to edit the wrong characters from OCR output. Then I want to download the OCR output as a document file so I used with download button. My current problem is that the OCR output text I edited at the textarea is not updated at the downloaded file. Only the unedited previous (original) OCR output is downloaded.
Steps to reproduce
Code snippet:
output = st.text_area("Output is: ", text, height=200) ## the value of the text came from ocr function and u can assume it as some random value; then I wanted to edit the character from this text area
st.download_button(label="Download data as docx", data=output, file_name='output.docx', mime='docx') ## I want to download the output of the text area from download_button and I also want to edit the text area and want to get the edited text when I downloaded
Expected behavior:
I want to download the output of the text area from download_button and I also want to edit the text area and want to get the edited text when I downloaded
Actual behavior:
Only the original ocr output is downloaded from the download_button although I edited it. The value of the text area is not updated when downloaded.
Debug info
Streamlit version: latest(get it with $ streamlit version)
Python version: 3.10(get it with $ python --version)
If you want to reproduce the code, here is the sample code.
import streamlit as st
from PIL import Image
st.markdown("<h2 style='text-align: center'>For OCR app</h1>", unsafe_allow_html=True)
image = st.file_uploader("Upload Image",type=['jpg','png','jpeg','JPG'])
col1, col2 = st.columns(2)
if image is not None:
with col1:
input_image = Image.open(image) #read image
width, height = input_image.size
st.image(input_image, caption="Input Image is: ") #display image
extract_button = st.button("Extract text")
if extract_button:
with col2:
text = "OCR text with a lot of errors and need to edit it "
output = st.text_area("Output is: ", text, height=220)
st.download_button(label="Download data as docx", data=output, file_name='output.docx', mime='docx')
st.success('Text Extraction Completed!')
else:
st.write("Upload an Image")
You will need to upload some image and need to click the button called Extract. Then you will see the text area box with OCR text with a lot of errors and need to edit it. What I wanna do is that I want to edit the text inside text area.
Then, I want to edit the text inside the text area like that
Normally, when I edit the text inside the text area, I need to press ctrl+enter to update the value of the text area. But with my current code flow, the text area part disappeared when I pressed ctrl+enter.
I need to download the text file with edited text. How can I do that to achieve this?
After you edit to the text area, extract_button becomes False and you no longer have a text area nor a download button and nothing is downloaded. You can try something like this instead.
extract_button = st.button("Extract text")
if extract_button:
st.session_state["text"] = "OCR text with a lot of errors and need to edit it "
if "text" in st.session_state:
st.text_area("Output is: ", key="text", height=220)
st.download_button(
label="Download data as docx",
data=st.session_state["text"],
file_name="output.docx",
mime="docx",
)
I am facing a issue . When I am clicking on Download data as docx without editing the text , the file is downloaded on one click . But when I am editing the text , I need to click twice on Download data as docx button to download the edited text file . Can you please provide me a solution for this ?
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.