Use webview in kivy as a widget

5,330 views
Skip to first unread message

shiva upreti

unread,
Nov 26, 2015, 10:40:43 PM11/26/15
to Kivy users support
This is the code I got from internet which allow to use webview in kivy and it works perfectly well:
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()

However I am unable to use it as a widget. I want to design a carousel with two screens, one with webview and the other with one of my own widget.
I wrote the below code but it just renders the webview and I am unable to slide between the two screens.

main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, ListProperty, NumericProperty, StringProperty
import json
from kivy.network.urlrequest import UrlRequest
import urllib
from kivy.factory import Factory
import kivy                                                                                                                                                            
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                                                      

LOGGED_IN
=""

class RentRoot(Screen):
    carousel
= ObjectProperty()  
   
def __init__(self, **kwargs):
       
super(RentRoot, self).__init__(**kwargs)
     


class LoginPage(BoxLayout):
   
def login_request(self, req, result):
       
print result
       
if result['error'] == False:
           
self.clear_widgets()
           
self.add_widget(ThankYou)
           
global LOGGED_IN
            LOGGED_IN
=result['email']

   
def validate_login(self):
       
print "in validate_login"
        email
=self.email.text
        password
=self.password.text
       
print email, password
       
params = urllib.urlencode({'email':email, 'password':password})        
        req
= UrlRequest('http://localhost:8000/api/login/', on_success=self.login_request, req_body=params)
       

class RentApp(App):
   
pass
                                                                     

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')

if __name__ == "__main__":
   
RentApp().run()Enter code here...

rent.kv:
RentRoot:
<RentRoot>:
    carousel
: carousel
    loginpage
: loginpage    
    wv
: wv    
   
Carousel:
        id
: carousel
       
LoginPage
            id
: loginpage    
       
Wv:
            id
:wv    



<LoginPage>:
    email
: email
    password
: password    
   
FloatLayout:        
       
Label:
            text
: 'Log In'
            pos
: 350, 450
            size_hint
: .1, .2
            font_size
: 36
       
TextInput:
           
#text: ""
            id
: email
            hint_text
: 'Username'
            font_size
: 20
            pos
: 280, 350
            size_hint
: .3, .07
       
TextInput:
           
#text: ""
            id
: password
            hint_text
: 'Password'
            font_size
: 20
            pos
: 280, 300
            size_hint
: .3, .07
       
Button:
            text
: 'Log In'
            pos
: 280, 200
            size_hint
: .3, .07
            font_size
: 16
            on_press
: root.validate_login()



Can someone please help me. Its urgent.

daro sior

unread,
Apr 13, 2016, 8:17:02 AM4/13/16
to Kivy users support
Hello, since then, have you successed in creating your webview ? I want to create an application with kivy to display my website on android devices, could you help cause i'm a little bit helpless with this new tool, however it has a strong doc.
Thank you,
daro

Kevin Ollivier

unread,
Apr 15, 2016, 2:42:09 PM4/15/16
to Kivy users support
Here's some code that I'm using to load up a webview in Kivy:


I needed to receive page load UI callbacks, which required me to create a little Java class for it, but if you don't need that part you can just remove the PEWebViewClientInterface and PythonWebViewClient classes from the code and it should Just Work. 

Regards,

Kevin

ZenCODE

unread,
Apr 16, 2016, 10:04:06 AM4/16/16
to Kivy users support
@Kevin Olivier. Thanks for posting. If that works (will be a while before I can test), it will really help us. It's also quite a common request on the forums, so you may have helped more people than you know :-)
Reply all
Reply to author
Forward
0 new messages