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.


    ui.WebView in an extension

    Pythonista
    webview appex extension
    3
    7
    9540
    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.
    • Zed_Oud
      Zed_Oud last edited by Zed_Oud

      Correct me if I'm wrong, but does the memory limit while running Python as an appex extension prevent us from loading things with WebView.load_url or WebView.load_html ?

      I've managed to get a local html file to load, but it will not populate its img tags, images will not load and leave the default blank bar/box ( "<img src="http://url.jpg"> ). The same html file will load perfectly using ui.webview or webbrowser NOT running from an extension (the html doc will work anywhere and everywhere else).

      import ui
      def view(text):
        v = ui.View()
        wv = ui.WebView()
        v.add_subview(wv)
        wv.load_html(text)
        v.frame = (0,0,320,568)
        wv.frame = (0,0,320,568)
        v.present()
      

      There's a cleaned up example of the function called to run my html doc as a string, though I've also tried running as a local file.

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

        This works for me...
        Shows the image and formatted text.

        # coding: utf-8
        
        import ui
        import appex
        
        text = appex.get_text()
        
        if text:
        	w = ui.WebView()
        	w.scales_page_to_fit = False
        	w.load_html(text)
        	w.present()
        

        Tested this from Drafts:

        <h1>header</h1>
        <b>some bold text</b>
        <img src= "https://lh4.ggpht.com/wKrDLLmmxjfRG2-E-k5L5BUuHWpCOe4lWRF7oVs1Gzdn5e5yvr8fj-ORTlBF43U47yI=w300"></img>
        

        Is there something really different that you're trying to do?

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

          Make sure you have valid html. here is an example showing an image.
          Note, if you are trying to load a local image, you may need to use the full path in a file:// url(os.path.abspath)

          import ui
          text="<img src='http://omz-software.com/pythonista/images/DeviceScreenshots.png'></img>"
          def view(text):
            v = ui.View()
            wv = ui.WebView()
            v.add_subview(wv)
            wv.load_html(text)
            v.frame = (0,0,320,568)
            wv.frame = (0,0,320,568)
            v.present()
          view(text)
          
          1 Reply Last reply Reply Quote 0
          • Zed_Oud
            Zed_Oud last edited by

            The HTML I'm using works everywhere, but not loaded through in an extension. That's my whole goal, I am trying to replace webbrowser.open("local file") for use in an extension. I haven't tried to load a local image using my HTML doc, I'll try that out.
            Here is an example of my HTML when I point it at "http://xkcd.com"

            <html>
            <body bgcolor="#000000">
            <img src="http://imgs.xkcd.com/static/terrible_small_logo.png" alt="http://imgs.xkcd.com/static/terrible_small_logo.png" ><br><br>
            <img src="http://imgs.xkcd.com/comics/tire_swing.png" alt="http://imgs.xkcd.com/comics/tire_swing.png" ><br><br>
            <img src="http://imgs.xkcd.com/store/te-pages-sb.png" alt="http://imgs.xkcd.com/store/te-pages-sb.png" ><br><br>
            <img src="http://imgs.xkcd.com/s/a899e84.jpg" alt="http://imgs.xkcd.com/s/a899e84.jpg" >
            </body>
            </html>
            

            Here is my full code (cleaned and formatted, but just as dysfunctional when used as an extension):

            # coding: utf-8
            
            import appex
            
            from urllib2 import urlopen
            import os, console, requests, urlparse
            
            def write_text(name, text, writ='w'):
              with open(name, writ) as o:
                o.write(text)
            
            def img_page(file_list, link_list=None):
              if link_list is None: link_list = file_list
              links = zip(file_list, link_list)
              x = '<br><br>\n'.join(['<img src="{0}" alt="{1}" >'.format(a,b) for a,b in links])
              y = """
            <html>
            <body bgcolor="#000000">
            {0}
            </body>
            </html>
            """.format(x)
              return y
              
            def view_doc(text):
              import ui
              w = ui.WebView()
              w.scales_page_to_fit = False
              w.load_html(text)
              w.present()
              
            def open_file(file_path):
              import ui
              file_path = os.path.abspath(file_path)
              file_path = urlparse.urljoin('file://', os.path.abspath(file_path))
              #v = ui.View()
              #file_path = 'http://xkcd.com'
              wv = ui.WebView()
              #v.add_subview(wv)
              wv.load_url(file_path)
              #v.frame = (0,0,320,568)
              #wv.frame = (0,0,320,568)
              #v.present()
              wv.present()
              
            def view_temp_index(file_url_list):
              temp_fn = '__temp.html'
              write_text(temp_fn, img_page(file_url_list))
              open_file(temp_fn)
            
            def get_Pic_Links_Content(content,url=None):
              from bs4 import BeautifulSoup as bs
              if url is None:
                url = '' # 'http://'
              s = bs(content)
              p = s.findAll('img')
              pics = []
              for x in p:
                y = urlparse.urljoin(url, x['src'])
                if y not in pics:
                  pics.append(y)
              return pics
            
            def get_Pic_Links(url):
              r = requests.get(url)
              #print 'viewing pics from url:', r.url
              return get_Pic_Links_Content(r.content, url)
              
            def pick(url):
              choice = console.alert('View:','Pick where to view source:','Make File','View Directly','Console')
              pics = get_Pic_Links(url)
              
              if choice == 1:
                view_temp_index(pics)
              elif choice == 2:
                view_doc(img_page(pics))
              else:
                print '\n'.join(pics)
            
            def main():
              if not appex.is_running_extension():
                print '\nRunning using test data...'
                url = 'http://xkcd.com'
              else:
                url = appex.get_url()
              if url:
                pick(url)
              else:
                print 'No input URL found.'
            
            if __name__ == '__main__':
              main()```
            1 Reply Last reply Reply Quote 0
            • JonB
              JonB last edited by

              your code works fine for me... this is in the beta.
              there may have been an issue with webviews in 2.0, I forget...

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

                @Zed_Oud
                I now see that I misunderstood your original post. You intended to feed an URL and retrieve images instead of sending HTML text to the script.

                Anyway, I also had the same problem as you when I ran the script you wrote. I changed the approach a little but with the same results.

                It seems as though with the next version of Pythonista this isn't an issue.

                I did test out a few different sites - some actually worked while others showed the same empty boxes. Perhaps there is some caching problem? I do not know!

                What is the purpose of displaying these files? Do you want to then choose one to save it to the camera roll? Do you want to download them all?

                I'm just asking because I'm sure there's another way to get what you want in the end!!

                Zed_Oud 1 Reply Last reply Reply Quote 0
                • Zed_Oud
                  Zed_Oud @cook last edited by

                  @cook
                  Thanks for looking at my code. I'm glad to know I hadn't overlooked something silly.

                  As it is, I am trying to make an extension to re-display or mobilize certain kinds of websites. Imagine if I went to a website full of thumbnails, and the script grabbed all of the linked full-size images. There's other websites and whatnot, but I was mostly playing around with extensions for now.

                  Thanks again for the help.

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