Cannot open an html file from python in a web browser – notepad opens instead

Question:

In python I am trying to plot a graph with the pygal package

import pygal  # First import pygal
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
bar_chart.add('Padovan', [1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12])
bar_chart.render_in_browser()

Unfortunately, it always opens the HTML file, but not an HTML page in the browser window. I read many posts and I see that people had similar issues in the past. I did not find a solution that works. I also tried to open it via the webbrowser module, but that also opens the HTML file in notepad.

url='file://C:/Users/User1/AppData/Local/Temp/tmpsblpwtpr.html'
webbrowser.open(url)

Anyone any thoughts?

Asked By: Goofball

||

Answers:

Despite being labelled with the html extension, it isn’t.

It’s SVG XML.

This is the header from the file:
<svg id="chart-c252fdc0-451c-4482-b9ae-09f5b513a2fc" class="pygal-chart" viewBox="0 0 800 600"><!--Generated with pygal 2.3.1 (lxml) Ā©Kozea 2012-2016 on 2017-06-09--><!--http://pygal.org--><!--http://github.com/Kozea/pygal-->

Either your xml or svg settings are configured for Notepad.

Answered By: Alan

See the following from the webbrowser module documentation:

Note that on some platforms, trying to open a filename using this
function, may work and start the operating system’s associated
program.

Most likely the associated program for .html files on your system is notepad, not your browser.

Answered By: JakeD

As noted in the Python documentation for webbrowser.open, this function isn’t a reliable way to open a local file in a browser:

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program. However, this is neither supported nor portable.

The issue here is how webbrowser.open (Python 2.7 source code) decides which program to use to open the url/file. When webbrowser is imported in, it stores a list of strings corresponding to browsers (_tryorder). The first item in this list (and the first browser used) is a operation-system-specific default browser, followed by other browsers that the module has detected.

The default browser attempts to use a generic command that will call the user’s default internet browser. Depending on your operating system (and possibly your default browser), this may or may not work in opening the file. For example, when I tested it on MacOs with Chrome as the default browser, a(n already opened) Chrome window appeared, but the file did not open (nor did any new tab). However, in Ubuntu with Firefox as the default, the file was opened in the browser.

In Windows, the “default browser” opens the file using os.startfile(), which the Python documentation says “acts like double-clicking the file in Windows Explorer”. As pycoder’s answer mentions, it is likely that the associated program for .html files on your computer is notepad. If this is the case, changing the default program for opening .html files to your web browser should solve this issue.

However, it may be possible to open the file in a web browser without having to change any settings. You can instead try opening it through one of the other browsers listed for webbrowser._tryorder (although it should be noted that the source code (in both 2.7 and 3.6) does not seem to support Chrome on Windows). It should look something like this (although with different browser names):

>>> webbrowser._tryorder
['MacOSX', 'chrome', 'firefox', 'safari']

Once you have this list, you can pick the browser you wish to use (in this example, Firefox) and then use the following code (replacing the index in _tryorder as appropriate):

browser = webbrowser.get(webbrowser._tryorder[2])
browser.open(url)

The above code succeeded in opening a local .html file in the web browser when I tested it using MacOs (Firefox and Chrome) and Ubuntu (just Firefox). I can’t guarantee it will work on Windows or with different browsers, but it seems probable that it would (all of the non-default Windows browsers appear to be called through subprocess.Popen, so they should work as long as it is possible to open the file in the browser from the Windows command line).

Answered By: D. Gillis

To open an html file stored on your device, try this code:

import subprocess
subprocess.call(['open', "YOURHTMLFILE.html"])

This also opens any file on your computer

Answered By: Port Me