How do i open a python html file in chrome?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Prerequisites: Webbrowser

    HTML files contain Hypertext Markup Language (HTML), which is used to design and format the structure of a webpage. It is stored in a text format and contains tags that define the layout and content of the webpage. HTML files are widely used online and displayed in web browsers.

    To preview HTML files, we make the use of browsers, like Google Chrome, Mozilla Firefox, Apple Safari etc. The task of creating and previewing HTML files can be automated with the help of python scripts.

    Here are a few ways how we can open HTML files on chrome:

    Method 1: Using os and webbrowser

    The webbrowser module in python provides a high-level interface to allow displaying Web-based documents to users, and the os module provides a portable way of using operating system dependent functionalities (like reading or writing files, manipulating file paths etc.). So let’s see how a combination of these both can help us to open an HTML page in Chrome browser:

    Function used: open_new_tab() function is used to open html file in a new tab of your default browser.

    Syntax:

    open_new_tab(filename)

    Approach:

    • Import module
    • Open and Create file 
    • Add html code 
    • Write code to file
    • Close file
    • Open file in browser window

    Example:

    Python3

    import webbrowser

    import os

    f = open('GFG.html', 'w')

    html_template =

    f.write(html_template)

    f.close()

    filename = 'file:///'+os.getcwd()+'/' + 'GFG.html'

    webbrowser.open_new_tab(filename)

    Output:

    How do i open a python html file in chrome?

    Method 2: Without using the ‘os’ module:

    If the HTML file is in the same directory as that of the python script, then there is no need of defining the file path with the os module. We can simply run the html file in new browser using the given steps:

    File in Use: GFG.html

    Approach

    • Create a html file that you want to open
    • In Python, Import module
    • Call html file using open_new_tab()

    Example:

    Python3

    Import webbrowser

    webbrowser.open_new_tab('GFG.html')

    Output:

    How do i open a python html file in chrome?


    I am trying to open an HTML file from Python but my script just displays the contents of the HTML file in Python instead of opening it in the browser. How can I fix this problem? How can I open the HTML file in my Chrome browser?

    testdata.html

    <div>
        <a href="https://plot.ly/user001/2/" target="_blank" title="Success vs Failure" style="display: block; text-align: center;"><img src="https://plot.ly/~user001/2.png" alt="Success vs Failure" style="max-width: 100%;width: 600px;"  width="600" onerror="this.onerror=null;this.src='https://plot.ly/404.png';" /></a>
        <script data-plotly="user001:2"  src="https://plot.ly/embed.js" async></script>
    </div>
    

    Python 2.7 script:

    import urllib
    page =  urllib.urlopen('testdata.html').read()
    print page
    

    How do i open a python html file in chrome?

    Danielo515

    4,4832 gold badges28 silver badges50 bronze badges

    asked Dec 1, 2016 at 8:21

    1

    Try specifying the "file://" at the start of the URL.

    // Also, use the absolute path of the file:
    
    webbrowser.open('file://' + os.path.realpath(filename))
    

    Or

    import webbrowser
    new = 2 # open in a new tab, if possible
    
    // open a public URL, in this case, the webbrowser docs
    url = "http://docs.python.org/library/webbrowser.html"
    webbrowser.open(url,new=new)
    
    // open an HTML file on my own (Windows) computer
    url = "file://d/testdata.html"
    webbrowser.open(url,new=new)
    

    answered Dec 1, 2016 at 8:27

    3

    import os
    os.system("start [your's_url]")
    

    Enjoy!

    answered Dec 1, 2016 at 8:24

    How do i open a python html file in chrome?

    Yuval PrussYuval Pruss

    7,60012 gold badges39 silver badges65 bronze badges

    2

    You can use webbrowser library:

    import webbrowser
    url = 'file:///path/to/your/file/testdata.html'
    webbrowser.open(url, new=2)  # open in new tab
    

    answered Dec 1, 2016 at 8:32

    How do i open a python html file in chrome?

    Here's a way that doesn't require external libraries and that can work of local files as well.

    import subprocess
    import os
    
    url = "https://stackoverflow.com"
    # or a file on your computer
    # url = "/Users/yourusername/Desktop/index.html
    try: # should work on Windows
        os.startfile(url)
    except AttributeError:
        try: # should work on MacOS and most linux versions
            subprocess.call(['open', url])
        except:
            print('Could not open URL')
    

    answered May 23, 2020 at 6:34

    How do i open a python html file in chrome?

    WyattBlueWyattBlue

    4991 gold badge5 silver badges18 bronze badges

    You can use Selenium.

    download the latest chromedriver, paste the chromedriver.exe in "C:\Python27\Scripts".

    then

    from selenium import webdriver
    
    driver = webdriver.Chrome()
    driver.get("your page path")
    print driver.page_source.encode('utf-8')
    driver.quit()
    display.stop()
    

    answered Dec 1, 2016 at 8:36

    How do i open a python html file in chrome?

    RainmakerRainmaker

    9,2966 gold badges49 silver badges78 bronze badges

    I feel this is the easiest solution:

    import os
    
    os.getcwd() #To check the current working directory or path
    os.chdir("D:\\Folder Name\\") # D:\Folder Name\ is the new path where you want to save the converted dataframe(df) to .html file
    
    import webbrowser
    
    df.to_html("filename.html") #Converting dataframe df to html and saving with a name 'filename' and 
    webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("file://" + os.path.realpath("filename.html"))
    

    snwflk

    3,1314 gold badges22 silver badges36 bronze badges

    answered Oct 26, 2020 at 16:35

    GoutamGoutam

    3551 gold badge2 silver badges9 bronze badges

    you can download latest version of "gecodriver" from here.then add gecodriver executable file to your project.then pip install selenium and below the code for windows:

    from selenium import webdriver   
    from selenium.webdriver.firefox.options import Options   
    import os
    
    #optional
    options = Options()   
    options.set_preference('permissions.default.image', 2)   
    options.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', False)   
    
    #for windows
    Driver = webdriver.Firefox(options=options, executable_path='geckodriver.exe')   
    Driver.implicitly_wait(15)
    
    #path of your project -> reference : "https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure/40227116"   
    Root = os.path.dirname(os.path.abspath(__file__))    
    driver.get('file://' + Root + 'path/to/htmlfile')
    

    Hope I Helped You:)

    answered Mar 3, 2020 at 20:49

    How do i open a python html file in chrome?

    2

    import os
    os.system('open "/Applications/Safari.app" '+ '"' + os.path.realpath(fname)+ '"')
    

    answered Nov 19, 2020 at 11:41

    1

    How do I open an HTML file in Chrome using python?

    To preview HTML files, we make the use of browsers, like Google Chrome, Mozilla Firefox, Apple Safari etc. The task of creating and previewing HTML files can be automated with the help of python scripts..
    Import module..
    Open and Create file..
    Add html code..
    Write code to file..
    Close file..
    Open file in browser window..

    How do I open an HTML file in Chrome?

    Fire up Chrome and jump to the webpage you want to view the HTML source code. Right-click the page and click on “View Page Source,” or press Ctrl + U, to see the page's source in a new tab. A new tab opens along with all the HTML for the webpage, completely expanded and unformatted.

    How do I view an HTML file in python?

    Install Beautifulsoup. Use the Anaconda package manager to install the required package and its dependent packages. ... .
    Reading the HTML file. In the below example we make a request to an url to be loaded into the python environment. ... .
    Extracting Tag Value. ... .
    Extracting All Tags..

    How do I open and read an HTML file in python?

    open() to open an HTML file within Python. Call codecs. open(filename, mode, encoding) with filename as the name of the HTML file, mode as "r" , and encoding as "utf-8" to open an HTML file in read-only mode.