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)
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.
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)
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.
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?
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.