I want to display this information to the user when the script is running:
Here the corresponding (simplified) code:
import streamlit as st
import tempfile
from io import BytesIO
from pdf2docx import Converter
uploaded_file = st.file_uploader("Choose a PDF file",
type="pdf")
if uploaded_file:
with st.spinner("Converting the document (the duration depends on the number of pages... 1 page ≈ 1 second)"):
filename = uploaded_file.name
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(uploaded_file.getvalue())
temp_file.close()
cv = Converter(temp_file)
docx_stream = BytesIO()
cv.convert(docx_stream, start=0, end=None)
cv.close()
docx_stream.seek(0)
st.download_button(label="📥 Click to download your Word document!",
data=docx_stream,
file_name=filename[:-4] + ".docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
Is there any way to capture and display these logs when st.spinner is looping?
Thanks
Edit: adding @snehankekre as I saw this thread but I was not able to use it properly
We can make some modifications to the code from the linked thread to capture the logs from pdf2docx and show them in the app + clear them after the conversion is complete.
The pdf2docx library initializes its logger with logging.basicConfig at the module level, which means that all logging messages from this module are processed by the root logger. So we should attach the custom handler to the root logger.
import logging
import re
import tempfile
from io import BytesIO
from pdf2docx import Converter
import streamlit as st
class StreamlitLogHandler(logging.Handler):
# Initializes a custom log handler with a Streamlit container for displaying logs
def __init__(self, container):
super().__init__()
# Store the Streamlit container for log output
self.container = container
self.ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') # Regex to remove ANSI codes
self.log_area = self.container.empty() # Prepare an empty conatiner for log output
def emit(self, record):
msg = self.format(record)
clean_msg = self.ansi_escape.sub('', msg) # Strip ANSI codes
self.log_area.markdown(clean_msg)
def clear_logs(self):
self.log_area.empty() # Clear previous logs
# Set up logging to capture all info level logs from the root logger
def setup_logging():
root_logger = logging.getLogger() # Get the root logger
log_container = st.container() # Create a container within which we display logs
handler = StreamlitLogHandler(log_container)
handler.setLevel(logging.INFO)
root_logger.addHandler(handler)
return handler
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file:
handler = setup_logging() # Set up logging with container
with st.spinner("Converting the document..."):
filename = uploaded_file.name
temp_file = tempfile.NamedTemporaryFile(delete=False)
temp_file.write(uploaded_file.getvalue())
temp_file.close()
cv = Converter(temp_file.name)
docx_stream = BytesIO()
cv.convert(docx_stream, start=0, end=None)
cv.close()
docx_stream.seek(0)
st.download_button("📥 Click to download your Word document!",
data=docx_stream,
file_name=filename[:-4] + ".docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
handler.clear_logs() # Clear logs after conversion
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.