And I don’t see anything too different on how they read and display the PDF apart from the annotation stuff so what is happening? Can someone try this and tell me if it’s working for them?
The pdf_file might be lost as you navigate to other pages or some reruns. It is better to store it in session state.
from streamlit import session_state as ss
# Declare variable.
if 'pdf_ref' not in ss:
ss.pdf_ref = None
Upload the file and save the uploaded file reference in the session variable.
# Access the uploaded ref via a key.
st.file_uploader("Upload PDF file", type=('pdf'), key='pdf')
if ss.pdf:
ss.pdf_ref = ss.pdf # backup
Use the saved pdf_ref.
# Now you can access "pdf_ref" anywhere in your app.
if ss.pdf_ref:
binary_data = ss.pdf_ref.getvalue()
pdf_viewer(input=binary_data, width=700)
Complete sample code.
from streamlit import session_state as ss
from streamlit_pdf_viewer import pdf_viewer
# Declare variable.
if 'pdf_ref' not in ss:
ss.pdf_ref = None
# Access the uploaded ref via a key.
st.file_uploader("Upload PDF file", type=('pdf'), key='pdf')
if ss.pdf:
ss.pdf_ref = ss.pdf # backup
# Now you can access "pdf_ref" anywhere in your app.
if ss.pdf_ref:
binary_data = ss.pdf_ref.getvalue()
pdf_viewer(input=binary_data, width=700)
So even if the pdf filename is no longer visible, the pdf is still viewable.