My app uses the Streamlit html component to render an html document on a page.
I’d like to be able to have the user “jump” to a specific section of the document. Does anyone know if this is possible through streamlit?
My app uses the Streamlit html component to render an html document on a page.
I’d like to be able to have the user “jump” to a specific section of the document. Does anyone know if this is possible through streamlit?
Hi @lattice,
Thanks for sharing your question! Have you tried adding anchor HTML elements?
Hi, thanks for the response. I am specifically looking to allow the users to jump within content rendered by the components.html streamlit component. Is there a way to accomplish this using an anchor element in the html passed to the component?
Understood – have you tried passing an HTML anchor element to the components.html()
function?
Hello, I am having the same issue. I pass an html body to components.html()
, which includes some anchors <a id="myanchor">
but calling these anchors either within the html itself or outside of it in the streamlit app does not work. The link within the html tries to go to https://myapplocation:host/#myanchor
instead of within the html.
Is there a way to add a “jump to” link to a specific part of the html we feed into components.html()?
Also, note that when opening the html outside the app. the anchors and links work as expected.
Thank you!
PJ
Hi PJ,
I am the same user as “lattice” above - I was able to get this to work by adding javascript to the html file being displayed by components.html(). The following function takes a bytes-type html document and inserts a short scrolling script at the bottom of the body. Hope this helps.
def scroll(html, element_id):
return html.replace('</body>'.encode(), """
<script>
var element = document.getElementById("{}");
element.scrollIntoView();
</script>
</body>
""".format(element_id).encode())
components.html(scroll(html, element))
James
Thank you James! I confirm it worked like a charm!
PJ
Unfortunately this is not working for me. It seems to me that the command does not propagate to the parent window.
Is it possible that an existing script in your html is taking precedence over the inserted script? or that the element isnt being selected, maybe if its loaded in asynchronously?
I think there was some asynchronocity that lead to issues. If I put a long enough timer on it, it does work.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
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.
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.
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.
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.