Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import tempfile
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
def create_pdf_download_button(pdf_path):
|
| 6 |
+
# Check if the PDF file exists
|
| 7 |
+
if not os.path.exists(pdf_path):
|
| 8 |
+
st.error(f"The PDF file '{os.path.basename(pdf_path)}' was not found.")
|
| 9 |
+
return
|
| 10 |
+
|
| 11 |
+
# Create a download link
|
| 12 |
+
st.markdown(
|
| 13 |
+
f"""
|
| 14 |
+
<a href="data:application/pdf;base64,{base64_encode_file(pdf_path)}"
|
| 15 |
+
download="{os.path.basename(pdf_path)}"
|
| 16 |
+
class="streamlit-button">
|
| 17 |
+
Download PDF
|
| 18 |
+
</a>
|
| 19 |
+
""",
|
| 20 |
+
unsafe_allow_html=True,
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def base64_encode_file(file_path):
|
| 24 |
+
# Read the file contents
|
| 25 |
+
with open(file_path, "rb") as file:
|
| 26 |
+
encoded_data = base64.b64encode(file.read()).decode()
|
| 27 |
+
|
| 28 |
+
return encoded_data
|
| 29 |
+
|
| 30 |
+
def main():
|
| 31 |
+
if "tmp_dir" not in st.session_state:
|
| 32 |
+
st.session_state.tmp_dir = tempfile.TemporaryDirectory()
|
| 33 |
+
if "temp_pdf_path" not in st.session_state:
|
| 34 |
+
st.session_state.temp_pdf_path = None
|
| 35 |
+
|
| 36 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 37 |
+
|
| 38 |
+
if uploaded_file:
|
| 39 |
+
pdf_file = uploaded_file
|
| 40 |
+
temp_pdf_path = os.path.join(st.session_state.tmp_dir, "pdf_file")
|
| 41 |
+
with open(temp_pdf_path, "wb") as f:
|
| 42 |
+
f.write(pdf_file.getvalue())
|
| 43 |
+
st.session_state.temp_pdf_path = temp_pdf_path
|
| 44 |
+
uploaded_file = None
|
| 45 |
+
|
| 46 |
+
create_pdf_download_button(st.session_state.temp_pdf_path)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|