4

webbrowser.open() works on Windows 7; Python 2.7 opens my default web browser (Chrome) on my Windows machine no problem.
My simple code to analyze CSV data works perfectly on both Windows and Android (4.1). webbrowser.open() or .get() does not work on Android with Firefox and Chrome.
I'm plotting/drawing analyzed data on Google Map on a browser. It's such a simple thing to do yet Python doesn't even find a "runnable" browser on Android.

I'm using QPython and Kivy Launcher to run my Python code on Android.

     QPython Log of webbrowser.get() or webbrowser.open(); 
     "webbrowser.Error: could not locate runnable browser."

Even if webbrowser.get(browser_path).open(url) worked, it's a bit of pain having to find a "runnable" browser among many possible browsers' path.

What API/module/trick do you wizards use, Pyjnius to call Android API?

"Mmmm, pieThong, uuugghhh..."
pieThong
  • 41
  • 1
  • 3
  • 1
    Do you want an analog of: `import android; android.Android().startActivity('android.intent.action.VIEW', url)`? – jfs Jan 07 '15 at 05:29
  • Thanks for your speedy response & tips, much appreciated! I prefer using just Python 2.7, but that may not be an option in this case. – pieThong Jan 07 '15 at 05:40
  • There is a Python code importing Android to open browser: [GitHub knappador/kivy-browser](https://github.com/knappador/kivy-browser/blob/master/src/browser/androidbrowser.py). But I think it's such a common operation that Python should be able to handle it (unless I'm doing something wrong!) – pieThong Jan 07 '15 at 06:21
  • Are you able to test by building your own apk rather than using qpython or the kivy launcher? Python-for-android *does* register a browser for android, so this should work. I would have thought this would work with what you're already trying, but maybe not. – inclement Jan 07 '15 at 12:33
  • Failing that, knappador's method is good. – inclement Jan 07 '15 at 12:35
  • Wow!...inclement "the Kivy" himself!...I wish I had your brain!... I didn't think of building APK to get it to work. I thought it would be a waist of time if I can't even get the code running first on Android. Thanks for your response! – pieThong Jan 07 '15 at 12:59

1 Answers1

0

Using webbrowser is not an option on Android so far. Instead you can use Pyjnius:

from jnius import autoclass, cast

def open_url(url):
    Intent = autoclass("android.content.Intent")
    Uri = autoclass("android.net.Uri")
    PythonActivity = autoclass("org.kivy.android.PythonActivity")

    i = Intent(Intent.ACTION_VIEW)
    i.setData(Uri.parse(url))

    current_activity = cast('android.app.Activity', PythonActivity.mActivity)
    current_activity.startActivity(i)

Reference: Pyjnius documentation

Vito Gentile
  • 13,336
  • 9
  • 61
  • 96