Search code examples
pythonpython-2.7flaskopenshiftpython-webbrowser

Why is webbrowser lib acting differently when run locally vs. remotely?


I'm writing a Flask (python 2.7) application in which a function uses the webbrowser lib to open several new tabs in the user's browser (similar to Kayak).

The app works correctly when I run it locally. But when I deploy to OpenShift, the app doesn't launch tabs or indicate any errors. Is there any reason why webbrowser would operate differently on my local machine vs. a PaaS? I've tried this in Chrome / Safari / Firefox on OSX & Chrome on Android. Appreciate your help!

Here's the function:

def launch(data):
  try:
    for x in data:
      webbrowser.open(x['url'],new=2)
      # i've also tried .open_new_tab(x['url'])
  except TypeError:
    return render_template('error.html')

and here's the output from tail:

[24/Jun/2014:23:19:24 -0400] "GET /-JQDELftipTPc12ohHy4 HTTP/1.1" 200 2563 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"
[24/Jun/2014:23:19:25 -0400] "GET /static/style.css HTTP/1.1" 304 - "http://tshare-nealrs.rhcloud.com/-JQDELftipTPc12ohHy4" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"
[24/Jun/2014:23:19:25 -0400] "GET /static/share.js HTTP/1.1" 304 - "http://tshare-nealrs.rhcloud.com/-JQDELftipTPc12ohHy4" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36"

Solution

  • webbrowser opens the browser on the machine on which Python is running. When you are developing, the machine on which Python is running and the machine on which the user is browsing are one and the same and everything seems to work. When you deploy to OpenShift the machine on which Python is running is the OpenShift box where your code is deployed ... which is not where you want to be opening up new browser instances at all.

    Instead, you will want to trigger several new windows to open via JavaScript's window.open. This will cause the browser to open several new tabs, rather than invoking a new browser process on the machine running your server code.