Pdf_viewer probem

I’m creating a simple pdf viewer. See the code below. When I click on a button the first time nothing is displayed. When I click on it a second time thh pdf is displayed. What am I doing wrong?

import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
import os

if ‘selected_file’ not in st.session_state:
st.session_state[‘selected_file’] = None

with st.sidebar:
library = st.container()
with library:

    def get_files(directory):
        return [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]

    def file_selector(folder_path='.'):
        st.write("Select a document from the list below:")
        files = get_files(folder_path)
        for file in files:
            if st.button(file): 
                st.session_state['selected_file'] = file  

    folder_path = './docs' 
    file_selector(folder_path)

if st.session_state[‘selected_file’] is not None:
filename = st.session_state[‘selected_file’]
st.write(filename)
folder_path = ‘./docs’
uploaded = os.path.join(folder_path, filename)
pdf_viewer(input=uploaded, width=1000)

Try to understand the button behavior.

Also, if I select Rerun from the menu it will display the PDF.

To check my work I set up in a new folder. I installed a fresh virtual environment and pip installed a fresh streamlit and streamlit_pdf_viewer. I then coded a minimumal version of a pdf viewer (see below). It still won’t show the pdf until I select Rerun from the menu it will display the PDF.

import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
import os

filename = ‘Choosing a Treatment for Kidney Failure - NIDDK.pdf’
st.write(filename)
folder_path = ‘./docs’
uploaded = os.path.join(folder_path, filename)
pdf_viewer(input=uploaded, width=1000)

I reviewed the supplied link but it was unhelpful as it doesn’t appear to be a button issue.

I reviewed the supplied link but it was unhelpful as it doesn’t appear to be a button issue.

To check my work I set up in a new folder. I installed a fresh virtual environment and pip installed a fresh streamlit and streamlit_pdf_viewer. I then coded a minimumal version of a pdf viewer (see below). It still won’t show the pdf until I select Rerun from the menu it will display the PDF.

import streamlit as st
from streamlit_pdf_viewer import pdf_viewer
import os

filename = ‘Choosing a Treatment for Kidney Failure - NIDDK.pdf’
st.write(filename)
folder_path = ‘./docs’
uploaded = os.path.join(folder_path, filename)
pdf_viewer(input=uploaded, width=1000)

Thanks

Bob

2 Likes

This has been resolved by Luca Foppiano, the author of Pdf_viewer:
If you revert to version 0.0.7 it should work, however, the application will use more CPU because of an infinite loop in the frontend. it’s a regression from #28 which we did not notice in our demo tests.
We are working already on a solution, and we will have integration tests trying to spot those situations in future. Leave this issue open and we will update you when a new release will solve this issue.

1 Like

Now the problem is a problem with displaying a pdf in Streamlit Cloud. The pdf displays correctly when I run it locally, but no luck with displaying it on Streamlit Cloud. Any ideas?