Streamlit is a powerful tools to quickliy build the demo application.
If we use Streamlit file upload feature via WebBrowser then we need to its file path to process the uploaded file.
So I will introduce how to get uploaed file path in Streamlit.
We buid the PDF File upload feature in Streamlit and its PDF file convert to image.
We use Belval/pdf2image which is a populer PDF converting tool. It needs to file path to apply the module feature. we assume local machine is the MacOS then we need to install the poppler to use pdf2image,
importbase64importtempfileimportstreamlitasstfrompdf2imageimportconvert_from_pathfrompathlibimportPathdefshow_pdf(file_path:str):"""Show the PDF in Streamlit
That returns as html component
Parameters
----------
file_path : [str]
Uploaded PDF file path
"""withopen(file_path,"rb")asf:base64_pdf=base64.b64encode(f.read()).decode("utf-8")pdf_display=f'<embed src="data:application/pdf;base64,{base64_pdf}" width="100%" height="1000" type="application/pdf">'st.markdown(pdf_display,unsafe_allow_html=True)defmain():"""Streamlit application
"""st.title("PDF file uplodaer")uploaded_file=st.file_uploader("Choose your .pdf file",type="pdf")ifuploaded_fileisnotNone:# Make temp file path from uploaded filewithtempfile.NamedTemporaryFile(delete=False)astmp_file:st.markdown("## Original PDF file")fp=Path(tmp_file.name)fp.write_bytes(uploaded_file.getvalue())st.write(show_pdf(tmp_file.name))imgs=convert_from_path(tmp_file.name)st.markdown(f"Converted images from PDF")st.image(imgs)if__name__=="__main__":main()