omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    pip install gestures, WKWebView, UI animations and more

    Pythonista
    8
    36
    13097
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Anxietier
      Anxietier last edited by

      Hi, I tested one demo code in readme of pythonista-wkwebview

      class MagicWebView(WKWebView):
        
        def on_magic(self, message):
          print('WKWebView magic ' + message)
          
      html = '''
      <body>
      <button onclick="window.webkit.messageHandlers.magic.postMessage(\'spell\')">
      Cast a spell
      </button>
      </body>
      '''
      
      v = MagicWebView()
      v.load_html(html)
      

      Well it works good, but when I try to load html file, the 'on_magic' doesn't work, here's my code in py file

      class MagicWebView(WKWebView):
      	def on_magic(self, message):
      		print('WKWebView magic ' + message)
      
      v = MagicWebView()
      v.present()
      # v.load_html(html)
      v.load_url('test.html', no_cache=False, timeout=5)
      v.clear_cache()
      

      I don't know how to fix it

      cvp mikael 3 Replies Last reply Reply Quote 0
      • cvp
        cvp @Anxietier last edited by cvp

        @Anxietier try

        import os
        
        w = WKWebView()
        f = os.path.abspath('test.html')
        print(f)
        w.load_url(f)
        w.present() 
        
        Anxietier 1 Reply Last reply Reply Quote 0
        • mikael
          mikael @Anxietier last edited by

          @Anxietier, I see the online documentation covers this poorly. Here’s the method docstring:

          Loads the contents of the given url asynchronously.

          If the url starts with file://, loads a local file. If the remaining url starts with /, path starts from Pythonista root.

          For remote (non-file) requests, there are two additional options:

          • Set no_cache to True to skip the local cache, default is False
          • Set timeout to a specific timeout value, default is 10 (seconds)
          Anxietier 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @Anxietier last edited by

            @Anxietier, you can get this and similar documentation on other methods using the help function, e.g. in Pythonista console:

            >>> import wkwebview
            >>> help(wkwebview.WKWebView.load_url)
            
            1 Reply Last reply Reply Quote 0
            • Anxietier
              Anxietier @cvp last edited by

              @cvp
              thanks, but it seems doesn’t work, here’s my test code

              file_name = 'test.copy(2).html'
              file_abs_path = os.path.abspath(file_name)
              url_file_name = 'file://' + file_name
              url_file_abs_path = 'file://' + file_abs_path
              
              with open(file_name, 'r', encoding='utf8') as f:
              	html = ''.join(f.readlines())
              
              v = MagicWebView()
              v.present()
              
              # v.load_html(html) # can load but button doesn't work
              
              # v.load_url(file_name, no_cache=False, timeout=5) # can load but button doesn't work
              
              # v.load_url(file_abs_path, no_cache=False, timeout=5) # can load but button doesn't work
              
              # v.load_url(url_file_name, no_cache=False, timeout=5) # would let ista crash or load faild
              
              # v.load_url(url_file_abs_path, no_cache=False, timeout=5) # doesn't load
              
              v.clear_cache() 
              
              1 Reply Last reply Reply Quote 0
              • Anxietier
                Anxietier @mikael last edited by

                @mikael
                I’m not sure if I described my issue clearly, and here’s my another test code, perhaps that would be clearly

                file_name = 'test.copy(2).html'
                file_abs_path = os.path.abspath(file_name)
                url_file_name = 'file://' + file_name
                url_file_abs_path = 'file://' + file_abs_path
                
                with open(file_name, 'r', encoding='utf8') as f:
                	html = ''.join(f.readlines())
                
                v = MagicWebView()
                v.present()
                
                # v.load_html(html) # can load but button doesn't work
                
                # v.load_url(file_name, no_cache=False, timeout=5) # can load but button doesn't work
                
                # v.load_url(file_abs_path, no_cache=False, timeout=5) # can load but button doesn't work
                
                # v.load_url(url_file_name, no_cache=False, timeout=5) # would let ista crash or load faild
                
                # v.load_url(url_file_abs_path, no_cache=False, timeout=5) # doesn't load
                
                v.clear_cache() 
                
                mikael 2 Replies Last reply Reply Quote 0
                • mikael
                  mikael @Anxietier last edited by mikael

                  @Anxietier, I will have to look at this, maybe something has changed in recent iOS versions.

                  Meanwhile, this:

                  html = ''.join(f.readlines())

                  ... looks funny, breaking and joining with no change. Just html = f.read() should work fine.

                  stephen 1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @Anxietier last edited by mikael

                    @Anxietier, I placed this script:

                    import wkwebview
                    
                    class MyWebView(wkwebview.WKWebView):
                        
                        def on_magic(self, message):
                            print('WKWebView magic ' + message)
                    
                    wv = MyWebView()
                    wv.present('fullscreen')
                    
                    wv.load_url('load_url.html')
                    

                    ... and this HTML as the file load_url.html in the same directory:

                    <body>
                    <button onclick="window.webkit.messageHandlers.magic.postMessage('spell')">
                    Cast a spell
                    </button>
                    </body>
                    

                    And the button works fine. Note that I removed the backlashes in the onclick handler as escaping not necessary in a file.

                    I suspect you might have some kind of non-printing character issue.

                    Anxietier 1 Reply Last reply Reply Quote 1
                    • stephen
                      stephen @mikael last edited by stephen

                      @mikael said:

                      @Anxietier, have to look at this, maybe something has changed in recent iOS versions.

                      Meanwhile, this:

                      html = ''.join(f.readlines())

                      ... looks funny, breaking and joining with no change. Just html = f.read() should work fine.

                      much cleaner than i would of. lol

                      
                      html = ''.join([line for line in f])
                      
                      
                      1 Reply Last reply Reply Quote 0
                      • Anxietier
                        Anxietier @mikael last edited by Anxietier

                        @mikael
                        oh I see, I usually use r_string in py, so I didn't notice \' in doc_string
                        now it works fine
                        thanks for help

                        1 Reply Last reply Reply Quote 0
                        • Anxietier
                          Anxietier last edited by Anxietier

                          hi, im back again :D
                          we know that some browser has non-pic mode (i dont know right name,just like that mean), is it possible to add that feature to wkwebview? for some reason, i just need the full html content (dom-tree maybe?)

                          mikael 1 Reply Last reply Reply Quote 0
                          • mikael
                            mikael @Anxietier last edited by

                            @Anxietier, if you mean ”Reader Mode”, WKWebView does not include it. But you can use a SafariViewController, see @cvp’s code around the middle of this thread.

                            Anxietier 1 Reply Last reply Reply Quote 0
                            • JonB
                              JonB last edited by

                              If you just want to parse the HTML, use requests. Unless you need JavaScript to run I guess.

                              @mikael isn't there a delegate that would let you filter by uri? I forget if the should_load_uri or whatever gets called for <img>'s -- in which case he could just return false for non-html urls.

                              mikael Anxietier 2 Replies Last reply Reply Quote 0
                              • mikael
                                mikael @JonB last edited by

                                @JonB, no it does not. Would be good to understand the need a bit more before trying to bend WKWebView to it. For the most part, just fetching the page with requests, then filtering imgs and javascript before giving it to WKwebView might work just as well.

                                1 Reply Last reply Reply Quote 0
                                • Anxietier
                                  Anxietier @JonB last edited by

                                  @JonB
                                  yeah i need js in script tag to run. some requests is not easy to fake them you know, so i was wandering if i can let webview run in 'read mode' to simulate 'selenium', cause some imgs spend many data traffic, and i dont wanna take double traffic for one img

                                  1 Reply Last reply Reply Quote 0
                                  • Anxietier
                                    Anxietier @mikael last edited by

                                    @mikael
                                    thanks, it is helpful, i would try to understand them.

                                    mikael 2 Replies Last reply Reply Quote 0
                                    • mikael
                                      mikael @Anxietier last edited by

                                      @Anxietier, if web scraping is your thing, then the SafariViewController will not help you. WKWebView is the best tool around for Pythonista, as far as I know.

                                      Anxietier 1 Reply Last reply Reply Quote 0
                                      • mikael
                                        mikael @Anxietier last edited by

                                        @Anxietier, hey, there’s something called content rules that can be configured to just stop loading images, see here. I will give it a try a bit later.

                                        1 Reply Last reply Reply Quote 0
                                        • Anxietier
                                          Anxietier @mikael last edited by Anxietier

                                          @mikael
                                          well, theres an open source android app names 'H-viewer', using regex&selector to select out imgs, and re-layout them, so that we can just watch imgs. also, theres an app in appstore names 'yealico', it has similar function.
                                          I was trying to fake(reproduce?) that by python in ista, I found one way to do that, 'use two wkwebviews, only present one, the other one load url in background, when it did finish load, use js selector to get imgs url, then show imgs on presented webview', im not sure will background-webview cost data-usage, but I dont want to cost double 'img data-usage'

                                          mikael 1 Reply Last reply Reply Quote 0
                                          • mikael
                                            mikael @Anxietier last edited by

                                            @Anxietier, the content rules, which might help or not, require at least two completion blocks, doable but unfortunately I do not have the kind of time I had earlier to spend on these projects.

                                            I would be happy to provide background support if you want to give it a go. I would expect it to be mostly versions of code already used in this component.

                                            Anxietier 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors