I’m running my app locally right now. It’s an LLM powered CTRL+F search on steroids. I’m uploading a PDF, and getting an answer from said PDF, but I want to be able to scroll to and highlight the source of the answer within the PDF.
I just have no clue how to get the javascript into the rest of my python program in order to scroll the darn embedded PDF. Here’s my feeble attempt, can anyone make some suggestions or tell me what I’m doing wrong?:
file = "c:/Users/zmzac/desktop/guideline_bling/10pages.pdf"
with open(file, "rb") as f:
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
# Embedding PDF in HTML
pdf_display = F'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'
# Displaying File
st.markdown(pdf_display, unsafe_allow_html=True)
# Scroll to and highlight text
html = (f'''
<alert> JS injected... </alert>
<script>
var container = document.getElementById("viewer");
container.scrollTop = container.scrollHeight;
</script>
''')
if st.button("scroll"):
st.components.v1.html(html)
hi @zakelectric
It looks like you’re trying to inject JavaScript into your Streamlit app to scroll to the bottom of the embedded PDF when a button is clicked. However, there are a couple of issues in your code:
The container variable is attempting to reference an element with the id “viewer,” but it’s not clear from your provided code whether such an element exists in your HTML. You might need to inspect the generated HTML to find the correct element or add an id to the appropriate element.
The st.components.v1.html method is used for rendering HTML components. However, it won’t necessarily execute JavaScript in the same way as if it were directly in the HTML. Instead, you can use st.markdown with unsafe_allow_html=True to render the HTML and execute the included JavaScript.
import streamlit as st
import base64
file = "c:/Users/zmzac/desktop/guideline_bling/10pages.pdf"
with open(file, "rb") as f:
base64_pdf = base64.b64encode(f.read()).decode('utf-8')
# Embedding PDF in HTML
pdf_display = F'<embed id="pdfViewer" src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'
# Displaying File
st.markdown(pdf_display, unsafe_allow_html=True)
# Scroll to and highlight text
html = (f'''
<alert> JS injected... </alert>
<script>
var container = document.getElementById("pdfViewer");
container.scrollTop = container.scrollHeight;
</script>
''')
if st.button("Scroll"):
st.markdown(html, unsafe_allow_html=True)
Hey bud,
Yeah I ended up using pdf.js and passing the search parameters to pdf.js through the URL where pdf.js is hosted (in AWS).
You can see my app at guideline-gopher.streamlit.app to get an idea of how i accomplished it.
Answers for the rag? Sorry I don’t understand haha.
Feel free to DM me if you wanna hop on the phone or something and I can answer questions for you… I banged my head against the wall a LONG TIME to get this to work
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.