Hi, I have a Streamlit application where I can upload some pdf files and then select the files I want to work on from a select box and they will be displayed in the main panel. Now instead of displaying all the selected pdfs one after the other, I wonder if itβs possible to dynamically open every selected file in a new tab with the file name as its title. Of course, if I un-select the file, the tab should disappear.
> import streamlit as st
> import os
> from PyPDF2 import PdfReader
> from io import BytesIO
>
> # Function to read PDF and return content
> def read_pdf(file_path):
> # Replace with your PDF reading logic
> pdf = PdfReader(file_path)
> return pdf
>
> # Function to display PDF content
> def display_pdf(pdf):
> num_pages = pdf.page_count
>
> # Display navigation input for selecting a specific page
> page_number = st.number_input(
> "Enter page number", value=1, min_value=1, max_value=num_pages
> )
>
> # Display an image for the selected page
> image_bytes = pdf[page_number - 1].get_pixmap().tobytes()
> st.image(image_bytes, caption=f"Page {page_number} of {num_pages}")
>
> # Main Streamlit app code
> st.title("PDF Viewer App")
>
> # Get uploaded files
> selected_files = st.file_uploader("Upload PDF files", type=["pdf"], accept_multiple_files=True)
>
> # Dictionary to store selected file content
> selected_file_content = {}
>
> # Iterate over uploaded files
> for uploaded_file in selected_files:
> file_content = uploaded_file.read()
>
> temp_file_path = f"./temp/{uploaded_file.name}"
> os.makedirs(os.path.dirname(temp_file_path), exist_ok=True)
>
> with open(temp_file_path, "wb") as temp_file:
> temp_file.write(file_content)
>
> if uploaded_file.type == "application/pdf":
> # Read PDF and store content
> pdf = read_pdf(temp_file_path)
> selected_file_content[uploaded_file.name] = pdf
>
> # Cleanup: Remove the temporary file
> os.remove(temp_file_path)
>
> # Allow user to select which file to display using st.selectbox
> selected_file_name = st.selectbox("Select a file to display", list(selected_file_content.keys()))
>
> # Display the selected file content
> display_pdf(selected_file_content[selected_file_name])
Any suggestions ?