'
f'Schemdraw error:
{result.stderr[:500] or result.stdout[:500]}'
f'
'
)
# Read the generated SVG
if svg_path.exists() and svg_path.stat().st_size > 0:
svg_content = svg_path.read_text(encoding='utf-8')
b64 = base64.b64encode(svg_content.encode('utf-8')).decode('ascii')
return (
f''
f'Schemdraw ran but produced no SVG output.'
f'
'
)
finally:
if svg_path.exists():
svg_path.unlink(missing_ok=True)
def _wrap_code(code: str, svg_path: str) -> str:
"""Wrap user code to redirect schemdraw output to a file."""
# schemdraw writes SVG to the 'file' parameter of Drawing().
# We modify the code to inject file= if missing.
if "file=" not in code and "Drawing(" in code:
code = code.replace(
'Drawing(show=False)',
f"Drawing(show=False, file=r'{svg_path}')",
)
code = code.replace(
'Drawing()',
f"Drawing(show=False, file=r'{svg_path}')",
)
return code