Home > database >  How to save svg in pycairo w/o context manager
How to save svg in pycairo w/o context manager

Time:10-31

I have a test.py script that I use simply this way:

>>> import test
>>>

Inside test.py:

import cairo


sfc = cairo.SVGSurface("test.svg", 720, 720)
ctx = cairo.Context(sfc)
ctx.set_source_rgb(.5, .5, 1)
ctx.arc(360, 360, 300, 0, 6.28)
ctx.fill()

I get no svg output until I Ctrl D in the console.

Using a context manager (with cairo.SVGSurface("test.svg", 720, 720) as sfc:) produces the svg file immediately once module imported. But, in my (larger) project, I declare the surface in a constructor, and drawings are made in a method... so I cannot use the context manager.

How can I "close" the surface?

CodePudding user response:

Per the documentation You call surface.finish() and then surface.flush()

import cairo


sfc = cairo.SVGSurface("test.svg", 720, 720)
ctx = cairo.Context(sfc)
ctx.set_source_rgb(.5, .5, 1)
ctx.arc(360, 360, 300, 0, 6.28)
ctx.fill()
sfc.finish()
sfc.flush()
  • Related