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?
-
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?
-
Check out the green square... https://github.com/cclauss/Pythonista_ui/blob/master/ScreenshotWebView.py
-
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?
-
@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.
-
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.
-
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)... -
edit:
Added omz'sabspath()
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!
-
@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.
-
console.quicklook()
needs an absolute path. Try something like:import os import console console.quicklook(os.path.abspath(filename))
-
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?