简体   繁体   English

无法在网络浏览器中从python打开html文件-记事本改为打开

[英]Cannot open an html file from python in a web browser - notepad opens instead

In python I am trying to plot a graph with the pygal package 在python中,我正在尝试使用pygal软件包绘制图

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. 不幸的是,它总是打开HTML文件,但不会在浏览器窗口中打开HTML页面。 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. 我也尝试通过webbrowser模块打开它,但是这也可以在记事本中打开HTML文件。

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

Anyone any thoughts? 有任何想法吗?

Despite being labelled with the html extension, it isn't. 尽管标有html扩展名,但事实并非如此。

It's SVG XML. 这是SVG XML。

This is the header from the file: <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/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--> 这是文件的标题: <svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/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. 为记事本配置了xml或svg设置。

See the following from the webbrowser module documentation: 请参阅webbrowser模块文档中的以下内容:

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. 与系统上的.html文件相关的程序很可能是记事本,而不是浏览器。

As noted in the Python documentation for webbrowser.open , this function isn't a reliable way to open a local file in a browser: webbrowser.openPython文档所述 ,此函数不是在浏览器中打开本地文件的可靠方法:

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. 这里的问题是webbrowser.open (Python 2.7源代码)如何决定使用哪个程序打开url /文件。 When webbrowser is imported in, it stores a list of strings corresponding to browsers ( _tryorder ). 导入webbrowser时,它将存储与浏览器相对应的字符串列表( _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. 默认浏览器尝试使用通用命令来调用用户的默认Internet浏览器。 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). 例如,当我在MacOs上使用Chrome作为默认浏览器对其进行测试时,会出现一个(尚未打开)Chrome窗口,但是该文件没有打开(也没有任何新选项卡)。 However, in Ubuntu with Firefox as the default, the file was opened in the browser. 但是,在默认使用Firefox的Ubuntu中,该文件是在浏览器中打开的。

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". 在Windows中,“默认浏览器”使用os.startfile()打开文件, Python文档称其为“类似于在Windows资源管理器中双击文件”。 As pycoder's answer mentions, it is likely that the associated program for .html files on your computer is notepad. 正如pycoder的答案所提到的,您计算机上.html文件的关联程序很可能是记事本。 If this is the case, changing the default program for opening .html files to your web browser should solve this issue. 在这种情况下,更改用于将.html文件打开到Web浏览器的默认程序应该可以解决此问题。

However, it may be possible to open the file in a web browser without having to change any settings. 但是,可以在Web浏览器中打开文件而不必更改任何设置。 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). 您可以尝试通过为webbrowser._tryorder列出的其他浏览器之一打开它(尽管应注意,源代码(在2.7和3.6中)似乎不支持Windows上的Chrome)。 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): 获得此列表后,您可以选择要使用的浏览器(在此示例中为Firefox),然后使用以下代码(适当替换_tryorder的索引):

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). 当我使用MacO(Firefox和Chrome)和Ubuntu(仅Firefox)进行测试时,以上代码成功在网络浏览器中打开了本地.html文件。 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). 我不能保证它可以在Windows或不同的浏览器上运行,但是似乎有可能(所有非默认的Windows浏览器似乎都是通过subprocess.Popen调用的,因此只要有可能,它们就应该可以运行从Windows命令行在浏览器中打开文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM