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.


    Are SVG objects loadable/viewable in the Web View?

    Pythonista
    3
    10
    6126
    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.
    • ltddev
      ltddev last edited by

      I'm wondering if it is possible to load SVG objects to a Web View and using load_url() or load_html() or loading via javascript? If it is possible can someone post a snippet of code to view/load an SVG object in the web view?

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

        Check out the green square... https://github.com/cclauss/Pythonista_ui/blob/master/ScreenshotWebView.py

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

          As a follow on question to my own question I noticed that the svg file I have been trying to load in a web view loads fine in the main Pythonista editor. Do I have programmatic capability to load via a view in the same way the svg was properly displayed in (an) editor when when this file type was selected and what is the best view to do so?

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

            @ccc, thanks, my html is inlined with <svg> </svg> exactly like yours (as opposed to using another method like embed,frame,or object) and I am doing a load_html() just like you are doing but it did not load as I expected which is what prompted me to ask this question. I can load your html snippet and the svg object so there must be something fishy with my svg.

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

              filename = 'green_square.svg'
              svg= '''<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
                  <rect width="150" height="150" fill="rgb(0, 255, 0)" stroke-width="1" stroke="rgb(0, 0, 0)" />
              </svg>'''
              
              with open(filename, 'w') as out_file:
                  out_file.write(svg)
                  
              import editor
              editor.open_file(filename)
              

              The last line causes the Editor to open the file as editable text.

              But tapping the file in the file list causes the Editor to render the graphics.

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

                console.quicklook() does pretty much the same as the built-in preview for files that the editor doesn't support (i.e. everything that isn't text)...

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

                  edit: Added omz's abspath() fix below.

                  filename = 'green_square.svg'
                  svg= '''<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
                      <rect width="150" height="150" fill="rgb(0, 255, 0)" stroke-width="1" stroke="rgb(0, 0, 0)" />
                  </svg>'''
                  
                  with open(filename, 'w') as out_file:
                      out_file.write(svg)
                      
                  #import editor   ; editor.open_file(filename)   # opens file as editable text
                  import console #; console.quicklook(filename)  # does not render
                  import os       ; console.quicklook(os.path.abspath(filename))  # renders!
                  
                  1 Reply Last reply Reply Quote 0
                  • ltddev
                    ltddev last edited by

                    @ccc That's what I was thinking too. The exposed console method does not work to expose same functionality as the built-in preview which is not exposed. I was able after futzing around a bit to get my svg object loading in a web view through either directly inlining the svg or by reference to separate file so thanks for the pointers.

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

                      console.quicklook() needs an absolute path. Try something like:

                      import os
                      import console
                      console.quicklook(os.path.abspath(filename))
                      
                      1 Reply Last reply Reply Quote 0
                      • ltddev
                        ltddev last edited by

                        Thanks to everyone for the useful explanations. I can now render my SVG in a web view by loading html. I'm still not sure that web view best meets my need. Is there any other ui view that can load and properly render an SVG object?

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