graphics save svg to string

61 views
Skip to first unread message

Pedro Cruz

unread,
Jun 29, 2012, 6:55:29 AM6/29/12
to sage-s...@googlegroups.com
Consider this sequence:
    g = Graphics()
    g += ... primitives..
    g.save("tempfile.svg")

Is there any command like:
    one_str = g.savestring("svg")
that avoids opening and reading "tempfile.svg" ?
 
Thank you.

Jason Grout

unread,
Jun 29, 2012, 9:40:05 AM6/29/12
to sage-s...@googlegroups.com
Not easily. You can use matplotlib to do it directly (basically,
duplicate what is done in the .save() method, but save to a
StringIO.StringIO() object). I have some patches up at the Sage cell
server. If you apply the hunk at
https://github.com/sagemath/sagecell/blob/master/sage-patches/02-sage-show.patch#L189,
you should be able to do something like:

sage: import StringIO
sage: f=StringIO.StringIO()
sage: a=plot(x^2)
sage: a.save(f,format='svg')
sage: f.getvalue() # gets the SVG string

Thanks,

Jason

Reply all
Reply to author
Forward
0 new messages