Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· OpenAIAi2 WildChat-4.8M (AI2)

in streamlit how to open a local html page in new tab

  1. User

    in streamlit how to open a local html page in new tab

  2. Assistant

    To open a local HTML page in a new tab in Streamlit, you can use the webbrowser library in Python. Here's an example code snippet that demonstrates how to achieve this:

    import streamlit as st
    import webbrowser
    
    # Path to the local HTML file
    html_file_path = "path/to/your/local/file.html"
    
    # Function to open the local HTML file in a new tab
    def open_html_in_new_tab(file_path):
        url = f"file://{file_path}"
        webbrowser.open_new_tab(url)
    
    # Button to trigger opening the HTML file in a new tab
    if st.button("Open HTML file in new tab"):
        open_html_in_new_tab(html_file_path)
    

    In this code snippet, we first define the path to your local HTML file. Then, we create a function open_html_in_new_tab that takes the file path as an argument and opens it in a new tab using the webbrowser.open_new_tab method.

    Next, we create a button in the Streamlit app using st.button that, when clicked, calls the open_html_in_new_tab function to open the HTML file in a new tab.

    You can copy this code into your Streamlit app script and replace "path/to/your/local/file.html" with the actual path to your HTML file.