Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter to document rendering to show only specific pages #22

Closed
Vinno97 opened this issue Feb 1, 2024 · 8 comments
Closed

Add filter to document rendering to show only specific pages #22

Vinno97 opened this issue Feb 1, 2024 · 8 comments
Assignees
Labels
enhancement New feature or request implemented
Milestone

Comments

@Vinno97
Copy link

Vinno97 commented Feb 1, 2024

It would be very nice to be able to tell the viewer to open the PDF on a specific page or section (anchor). This enables more interactivity between the streamlit app and the PDF

@lfoppiano
Copy link
Owner

HI @Vinno97 and thanks for your interest in this component.
I will try to give you an answer, however we are learning streamlit on the way with our development.

We have been thinking about opening the PDF in a new window (#21), instead of in an iframe within the application window or tab, however, as with other features (e.g. getting the height/width of the iframe) there are quite a lot of limitations.

I think, at the moment you can do that by placing the pdf_viewer() call in the corresponding section (for example under a st.column() or a st.component()).

Are you aware of other components that can be opened in a targeted page or section?

@lfoppiano lfoppiano added the enhancement New feature or request label Feb 1, 2024
@Vinno97
Copy link
Author

Vinno97 commented Feb 2, 2024

Thanks for your quick response! I think there may be a misunderstanding. What I meant, was that it would be nice to have the ability to have the PDF viewer focus its view on a specific section of the PDF.

For example, I now did something like this as a workaround:

from io import BytesIO

import streamlit as st
from pypdf import PdfReader, PdfWriter
from streamlit_pdf_viewer import pdf_viewer

# Get the PDF
pdf = st.file_uploader("PDF Report")
if pdf is None:
    st.stop()

# Let the user choose a page number
pagenum = st.number("Page number")

# Create a new one-page PDF of the selected page
reader = PdfReader(pdf)
writer = PdfWriter()

writer.add_page(reader.pages[pagenum])

new_pdf = BytesIO()
writer.write(new_pdf)
new_pdf.seek(0)

# Show the new PDF
pdf_viewer(new_pdf.read(), height=800)

@lfoppiano
Copy link
Owner

Hi @Vinno97, thanks for the clarification, now I understand.

What we could try to do, is to verify whether it is possible to switch to a specific page when calling pdf_viewer(...).
However, I'm not sure we can control the javascript in this way, so your workaround might be the only solution.

@t29mato maybe you can check whether it's feasible to pass an additional parameter page and see if we can scroll down automatically after we load the pdf?

@lfoppiano
Copy link
Owner

@Vinno97 about your example, once the modified PDF is visualised it needs to be re-built again if the user wants other pages. Do you have a specific use case for this kind of feature?

@Vinno97
Copy link
Author

Vinno97 commented Feb 12, 2024

Yes my workaround is really crude. I wouldn't advocate for this being the actual solution. If you guys agree that allowing the user to focus on a specific page is a nice feature, I think something like this would be better.

My use case was that I was debugging PDF data extraction logic for some very large documents. I had the PDF viewer in one streamlit column and some extracted information in another column. The code I sent before allowed me to quickly focus on the exact page that corresponded to an element I was interested in. Otherwise i would have had to scroll through 100 pages until I found it.

@lfoppiano lfoppiano changed the title Open specific pages Add filter to document rendering to show only specific pages Feb 13, 2024
@lfoppiano
Copy link
Owner

@Vinno97 Thanks to @t29mato who has implemented a selective rendering on the javascript should be more efficient. It's released in version 0.0.7 (please skip version 0.0.6 😅).
You can see it in action here.

@lfoppiano lfoppiano added this to the 0.0.x milestone Feb 14, 2024
@Vinno97
Copy link
Author

Vinno97 commented Feb 14, 2024

Thanks, guys! Made quick work of it 😄

@lfoppiano
Copy link
Owner

I close the issue. Feel free to reopen or comment if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request implemented
Projects
None yet
Development

No branches or pull requests

3 participants