Kogan007
Kogan007

Reputation: 164

Python - Showing a web browser/iframe right into the app

I have a script which if helpful to people answering questions, is using kivy. I want to have it show a iframe kind of thing right into it when run, instead of opening the browser. For example something like this:

def browser():
    url = "google.com"
    iframe(url)
browser()

Obviously this wouldnt work as python is not html. Keep in mind, I am not trying to run this script on the web, but on the kivy launcher. As intended, it should not open the webbrowser but instead show the page in a box built right into the script.

Upvotes: 5

Views: 9100

Answers (2)

Michael Galaxy
Michael Galaxy

Reputation: 1283

Here's an actual running example which works right inside the "Kivy Launcher" app:

import kivy                                                                                     
from kivy.app import App                                                                        
from kivy.lang import Builder                                                                   
from kivy.utils import platform                                                                 
from kivy.uix.widget import Widget                                                              
from kivy.clock import Clock                                                                    
from jnius import autoclass                                                                     
from android.runnable import run_on_ui_thread                                                   

WebView = autoclass('android.webkit.WebView')                                                   
WebViewClient = autoclass('android.webkit.WebViewClient')                                       
activity = autoclass('org.renpy.android.PythonActivity').mActivity                              

class Wv(Widget):                                                                               
    def __init__(self, **kwargs):                                                               
        super(Wv, self).__init__(**kwargs)                                                      
        Clock.schedule_once(self.create_webview, 0)                                             

    @run_on_ui_thread                                                                           
    def create_webview(self, *args):                                                            
        webview = WebView(activity)                                                             
        webview.getSettings().setJavaScriptEnabled(True)                                        
        wvc = WebViewClient();                                                                  
        webview.setWebViewClient(wvc);                                                          
        activity.setContentView(webview)                                                        
        webview.loadUrl('http://www.google.com')

class ServiceApp(App):                                                                          
    def build(self):                                                                            
        return Wv()                                                                             

if __name__ == '__main__':                                                                      
    ServiceApp().run()

Upvotes: 7

inclement
inclement

Reputation: 29488

You are trying to do this on an android device? There's currently not a build in way to do this, but I'm pretty sure it's possible to load a native android webview via pyjnius. I'm not sure the current state, but for example here is an example of how to do it. I've pasted some of the code below, but I recommend asking on the kivy mailing list or irc if you have any questions, as this kind of thing is under discussion and development.

from android.runnable import run_on_ui_thread

WebView = autoclass('android.webkit.WebView')
LayoutParams = autoclass('android.view.ViewGroup$LayoutParams')
activity = autoclass('org.renpy.android.PythonActivity').mActivity

class Wv(Widget):
    def __init__(self, **kwargs):
        super(Wv, self).__init__(**kwargs)
        self.create_webview()

    @run_on_ui_thread
    def create_webview(self):
        webview = WebView(activity)
        activity.addContentView(webview, LayoutParams(-1, -1))
        webview.getSettings().setJavaScriptEnabled(True)
        #having some trouble with this one: webview.setBackgroundColor(Color.TRANSPARENT)

        html = "<html><body style='margin:0;padding:0;'>\
            <script type='text/javascript'\
            src='http://ad.leadboltads.net/show_app_ad.js?section_id=ID_HERE'>\
            </script></body></html>"    

        activity.setContentView(webview)
        webview.loadData(html, "text/html", "utf-8")
        layout = LinearLayout(activity)
        layout.addView(activity.mView)
        activity.setContentView(layout)

Upvotes: 3

Related Questions