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.


    Strange error with ui.webview

    Pythonista
    webview
    3
    8
    6000
    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.
    • Croc
      Croc last edited by Croc

      I'm making a simple application in Pythonista to display webpages, listed in a text file, inside of a webview. I have looked over the documentation and tried to Google this error, but I can't seem to find anything.

      Here's my code:

      # coding: utf-8
      
      import ui
      import urllib2
      import re
      
      v = ui.load_view()
      v.present('sheet')
      webview = ui.WebView
      
      def main():
      	fx = open('sites.txt', 'r')
      	lines = fx.readlines()
      	fx.close()
      	link1 = re.split('/\n', lines[0])
      	link1.pop()
      	link2 = re.split('/\n', lines[1])
      	link2.pop()
      	link3 = re.split('/\n', lines[2])
      	link3.pop()
      	webview.load_url(link1)
      	webview.present()
      	
      main()
      
      

      Here is a version of the error I get. Even when I supply a argument in the webview.load_url() method, it says it needs a URL instead of a string. So what I need to know is how to convert a list with one element into something that webview.load_url() will understand.

      Error

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

        The main problem is that the value of webview is the ui.WebView class, and not an instance of the class. You're missing parentheses there, it should be webview = ui.WebView().

        I don't know what the contents of your sites.txt file is exactly, and if the regex splitting is actually working as you expect. In any case, link1 will be a list of strings, and you need one string for load_url, so it should be something like webview.load_url(link1[0]) (assuming the first item in the list is a URL).

        Croc 1 Reply Last reply Reply Quote 1
        • brumm
          brumm last edited by brumm

          # coding: utf-8
          
          import ui
          import urllib2
          import re
          
          v = ui.load_view()
          webview = ui.WebView()
          webview.flex = 'WH'
          v.add_subview(webview)
          v.present('full_screen')
          
          def main():
              fx = open('sites.txt', 'r')
              lines = fx.readlines()
              fx.close()
              link1 = lines[0].strip()
              #link1 = re.split('/\n', lines[0])
              #link1.pop()
              #link2 = re.split('/\n', lines[1])
              #link2.pop()
              #link3 = re.split('/\n', lines[2])
              #link3.pop()
              webview.load_url(link1)
              #webview.present() #if you don't need a second view!?
              
          main()
          
          Croc 1 Reply Last reply Reply Quote 1
          • Croc
            Croc @omz last edited by

            @omz Thank you for the quick help. I figured it was some dumb mistake. :)

            1 Reply Last reply Reply Quote 0
            • Croc
              Croc @brumm last edited by

              @brumm I edited my code to match what you added and it works somewhat, but for some reason the webview is doing this...

              after running script

              And it should look like this...

              what it should look like

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

                Hi Croc,

                it's hard to guess which sheet size you have, but for e.g. you can set another size via

                webview = ui.WebView(frame=(20,20,400,300))
                

                More comfortable is to use the UI Designer (look for Auto-Resizing / Flex), but then you have to rewrite the source code.

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

                  v = ui.load_view()
                  #webview = ui.WebView(frame=(20,20,400,300))
                  #webview.flex = 'WH'
                  #v.add_subview(webview)
                  v.present('sheet')
                  webview = v['webview1']
                  
                  Croc 1 Reply Last reply Reply Quote 1
                  • Croc
                    Croc @brumm last edited by

                    @brumm Thanks that worked perfectly!

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