RstPreview icon indicating copy to clipboard operation
RstPreview copied to clipboard

RSTPreview doesn't open the preview in user's default browser

Open Joshua-Snapp opened this issue 11 years ago • 1 comments

This is not a significant issue, but I thought I'd mention it since it isn't especially hard to fix.

In RstPreview.py you call the webbrowser.open in the following way.

from webbrowser import open as open_in_browser

I'm not sure why this method is chosen, but apparently there's an issue with the way the webbrowser function works where it doesn't always identify the user's default browser. It also apparently doesn't even "know" about the google-chrome browser.

ref. Calling Chrome web browser from the webbrowser.get() in Python

The way you're calling webbrowser.open isn't causing the problem, but I think it gets in the way of implementing a solution easily. Why not just import the whole function? That way people can use it in the following way.

Solution: Use import webbrowser in RstPreview.py in order to be able to call the webbrowser.get().open() function.

Then replace

open_in_browser('http://127.0.0.1:%s' % server.server_port)

with

# open_in_browser('http://127.0.0.1:%s' % server.server_port) webbrowser.get().open('http://127.0.0.1:%s' % server.server_port)

This way a user can simply specify the browser they want to use.

e.g. Google-Chrome # open_in_browser('http://127.0.0.1:%s' % server.server_port) webbrowser.get('google-chrome %s').open('http://127.0.0.1:%s' % server.server_port)

Firefox # open_in_browser('http://127.0.0.1:%s' % server.server_port) webbrowser.get('firefox %s').open('http://127.0.0.1:%s' % server.server_port)

A comment in the RstPreview.py file and/or the README.rst would be helpful to point people to this option. I think that could improve the user experience.

Thanks for your work! I appreciate being able to preview RST stuff.

Joshua-Snapp avatar Mar 02 '13 22:03 Joshua-Snapp

Hi there!

Thanks for looking into this. I've not had the problem myself - it opens fine in Chrome for me on my various Macs - however I don't see any harm in making this more flexible.

I'm not sure why this method is chosen, but apparently there's an issue with the way the webbrowser function works where it doesn't always identify the user's default browser. It also apparently doesn't even "know" about the google-chrome browser.

I can't really tell you why this method was chosen. I think its just the first one I found in the documentation - I didn't look into it very far after it “just worked” :smile:

Why not just import the whole function? That way people can use it in the following way.

I guess you mean the whole module? It's a bit of a style pet pieve on mine I guess. I like to only import what I need - in my opinion it makes it clearer as people don't need to search for webbrowser to see what is used. However, it can easily be changed, particularly on such a simple module as this.

If your interested in making a start on this and opening a pull request that would be great. Feel free to start with a comment or instructions but I guess ideally we would make this a setting that the user can configure. They may not want to use the default browser in some cases too I guess.

d0ugal avatar Mar 03 '13 13:03 d0ugal