Skip to content

Instantly share code, notes, and snippets.

@kolibril13
Created April 15, 2025 10:03
Show Gist options
  • Save kolibril13/3ba36c5ecaa09a35f3dcd32ccd5bfa29 to your computer and use it in GitHub Desktop.
Save kolibril13/3ba36c5ecaa09a35f3dcd32ccd5bfa29 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:h5=\"http://www.w3.org/1999/xhtml\" class=\"typst-doc\" viewBox=\"0 0 150 95.438\" width=\"150pt\" height=\"95.438pt\">\n",
" <g>\n",
" <g transform=\"translate(0 7.238)\">\n",
" <g class=\"typst-text\" transform=\"scale(1, -1)\">\n",
" <use xlink:href=\"#gEC8A2FFF6D64E6AEBEAA4CCD9883101B\" x=\"0\" fill=\"#000000\" fill-rule=\"nonzero\"/>\n",
" <use xlink:href=\"#gD46A8F3A0FCE0846A2367EED65BA049B\" x=\"8.03\" fill=\"#000000\" fill-rule=\"nonzero\"/>\n",
" </g>\n",
" </g>\n",
" <g transform=\"translate(0 20.438)\">\n",
" <image xlink:href=\"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8dGV4dCB4PSIxMDAiIHk9IjUwIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIgogICAgICAgIGZvbnQtZmFtaWx5PSJDYWxpYnJpLCBzYW5zLXNlcmlmIiBmb250LXNpemU9IjQwIiBmaWxsPSIjMDAwMDAwIj4KICAgIFdvcmxkCiAgPC90ZXh0Pgo8L3N2Zz4=\" width=\"150\" height=\"75\" preserveAspectRatio=\"none\"/>\n",
" </g>\n",
" </g>\n",
" <defs id=\"glyph\">\n",
" <symbol id=\"gEC8A2FFF6D64E6AEBEAA4CCD9883101B\" overflow=\"visible\">\n",
" <path d=\"M 6.798 1.342 L 6.798 5.753 C 6.798 6.666 6.985 6.721 7.755 6.754 C 7.821 6.82 7.821 7.051 7.755 7.117 C 7.304 7.106 6.743 7.095 6.325 7.095 C 5.9179997 7.095 5.39 7.106 4.906 7.117 C 4.84 7.051 4.84 6.82 4.906 6.754 C 5.676 6.721 5.863 6.666 5.863 5.753 L 5.863 3.993 L 2.101 3.993 L 2.101 5.753 C 2.101 6.666 2.288 6.721 3.058 6.754 C 3.124 6.82 3.124 7.051 3.058 7.117 C 2.6399999 7.106 2.189 7.095 1.628 7.095 C 1.078 7.095 0.627 7.106 0.20899999 7.117 C 0.143 7.051 0.143 6.82 0.20899999 6.754 C 0.979 6.721 1.166 6.666 1.166 5.753 L 1.166 1.342 C 1.166 0.429 0.979 0.374 0.20899999 0.341 C 0.143 0.275 0.143 0.044 0.20899999 -0.022 C 0.693 -0.011 1.221 0 1.6389999 0 C 2.035 0 2.574 -0.011 3.058 -0.022 C 3.124 0.044 3.124 0.275 3.058 0.341 C 2.288 0.374 2.101 0.429 2.101 1.342 L 2.101 3.531 L 5.863 3.531 L 5.863 1.342 C 5.863 0.429 5.676 0.374 4.906 0.341 C 4.84 0.275 4.84 0.044 4.906 -0.022 C 5.401 -0.011 5.929 0 6.336 0 C 6.743 0 7.271 -0.011 7.755 -0.022 C 7.821 0.044 7.821 0.275 7.755 0.341 C 6.985 0.374 6.798 0.429 6.798 1.342 Z \"/>\n",
" </symbol>\n",
" <symbol id=\"gD46A8F3A0FCE0846A2367EED65BA049B\" overflow=\"visible\">\n",
" <path d=\"M 1.9909999 1.342 L 1.9909999 3.531 C 1.9909999 4.081 2.035 4.785 2.035 4.785 C 2.035 4.829 1.98 4.862 1.892 4.862 C 1.584 4.741 1.144 4.642 0.385 4.5429997 C 0.363 4.4769998 0.385 4.301 0.407 4.235 C 1.012 4.18 1.122 4.114 1.122 3.487 L 1.122 1.342 C 1.122 0.429 1.001 0.396 0.32999998 0.341 C 0.264 0.275 0.264 0.044 0.32999998 -0.022 C 0.693 -0.011 1.122 0 1.562 0 C 2.002 0 2.42 -0.011 2.783 -0.022 C 2.849 0.044 2.849 0.275 2.783 0.341 C 2.112 0.385 1.9909999 0.429 1.9909999 1.342 Z M 0.99 6.5889997 C 0.99 6.303 1.254 6.017 1.518 6.017 C 1.826 6.017 2.09 6.314 2.09 6.545 C 2.09 6.809 1.859 7.117 1.562 7.117 C 1.298 7.117 0.99 6.853 0.99 6.5889997 Z \"/>\n",
" </symbol>\n",
" </defs>\n",
"</svg>"
],
"text/plain": [
"<IPython.core.display.SVG object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from pathlib import Path\n",
"import typst\n",
"\n",
"svg_content = '''<svg width=\"200\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">\n",
" <text x=\"100\" y=\"50\" text-anchor=\"middle\" dominant-baseline=\"middle\"\n",
" font-family=\"Calibri, sans-serif\" font-size=\"40\" fill=\"#000000\">\n",
" World\n",
" </text>\n",
"</svg>'''\n",
"\n",
"cwd = Path.cwd()\n",
"svg_file = cwd / \"temp_svg.svg\"\n",
"svg_file.write_text(svg_content)\n",
"\n",
"typst_file = cwd / \"temp_typst.typ\"\n",
"typst_file.write_text(\"\"\"\n",
"#set page(width: auto, height: auto, margin: 0cm, fill: none)\n",
"Hi \n",
"#image(\"temp_svg.svg\")\n",
"\"\"\")\n",
"\n",
"output_svg = cwd / \"output.svg\"\n",
"typst.compile(typst_file, format=\"svg\", output=str(output_svg))\n",
"svg_content = output_svg.read_text()\n",
"\n",
"from IPython.display import SVG\n",
"display(SVG(svg_content))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<svg class=\"typst-doc\" viewBox=\"0 0 150 95.438\" width=\"150pt\" height=\"95.438pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:h5=\"http://www.w3.org/1999/xhtml\">\n",
" <g>\n",
" <g transform=\"translate(0 7.238)\">\n",
" <g class=\"typst-text\" transform=\"scale(1, -1)\">\n",
" <use xlink:href=\"#gEC8A2FFF6D64E6AEBEAA4CCD9883101B\" x=\"0\" fill=\"#000000\" fill-rule=\"nonzero\"/>\n",
" <use xlink:href=\"#gD46A8F3A0FCE0846A2367EED65BA049B\" x=\"8.03\" fill=\"#000000\" fill-rule=\"nonzero\"/>\n",
" </g>\n",
" </g>\n",
" <g transform=\"translate(0 20.438)\">\n",
" <image xlink:href=\"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjAwIiBoZWlnaHQ9IjEwMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KICA8dGV4dCB4PSIxMDAiIHk9IjUwIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIgogICAgICAgIGZvbnQtZmFtaWx5PSJDYWxpYnJpLCBzYW5zLXNlcmlmIiBmb250LXNpemU9IjQwIiBmaWxsPSIjMDAwMDAwIj4KICAgIFdvcmxkCiAgPC90ZXh0Pgo8L3N2Zz4=\" width=\"150\" height=\"75\" preserveAspectRatio=\"none\"/>\n",
" </g>\n",
" </g>\n",
" <defs id=\"glyph\">\n",
" <symbol id=\"gEC8A2FFF6D64E6AEBEAA4CCD9883101B\" overflow=\"visible\">\n",
" <path d=\"M 6.798 1.342 L 6.798 5.753 C 6.798 6.666 6.985 6.721 7.755 6.754 C 7.821 6.82 7.821 7.051 7.755 7.117 C 7.304 7.106 6.743 7.095 6.325 7.095 C 5.9179997 7.095 5.39 7.106 4.906 7.117 C 4.84 7.051 4.84 6.82 4.906 6.754 C 5.676 6.721 5.863 6.666 5.863 5.753 L 5.863 3.993 L 2.101 3.993 L 2.101 5.753 C 2.101 6.666 2.288 6.721 3.058 6.754 C 3.124 6.82 3.124 7.051 3.058 7.117 C 2.6399999 7.106 2.189 7.095 1.628 7.095 C 1.078 7.095 0.627 7.106 0.20899999 7.117 C 0.143 7.051 0.143 6.82 0.20899999 6.754 C 0.979 6.721 1.166 6.666 1.166 5.753 L 1.166 1.342 C 1.166 0.429 0.979 0.374 0.20899999 0.341 C 0.143 0.275 0.143 0.044 0.20899999 -0.022 C 0.693 -0.011 1.221 0 1.6389999 0 C 2.035 0 2.574 -0.011 3.058 -0.022 C 3.124 0.044 3.124 0.275 3.058 0.341 C 2.288 0.374 2.101 0.429 2.101 1.342 L 2.101 3.531 L 5.863 3.531 L 5.863 1.342 C 5.863 0.429 5.676 0.374 4.906 0.341 C 4.84 0.275 4.84 0.044 4.906 -0.022 C 5.401 -0.011 5.929 0 6.336 0 C 6.743 0 7.271 -0.011 7.755 -0.022 C 7.821 0.044 7.821 0.275 7.755 0.341 C 6.985 0.374 6.798 0.429 6.798 1.342 Z \"/>\n",
" </symbol>\n",
" <symbol id=\"gD46A8F3A0FCE0846A2367EED65BA049B\" overflow=\"visible\">\n",
" <path d=\"M 1.9909999 1.342 L 1.9909999 3.531 C 1.9909999 4.081 2.035 4.785 2.035 4.785 C 2.035 4.829 1.98 4.862 1.892 4.862 C 1.584 4.741 1.144 4.642 0.385 4.5429997 C 0.363 4.4769998 0.385 4.301 0.407 4.235 C 1.012 4.18 1.122 4.114 1.122 3.487 L 1.122 1.342 C 1.122 0.429 1.001 0.396 0.32999998 0.341 C 0.264 0.275 0.264 0.044 0.32999998 -0.022 C 0.693 -0.011 1.122 0 1.562 0 C 2.002 0 2.42 -0.011 2.783 -0.022 C 2.849 0.044 2.849 0.275 2.783 0.341 C 2.112 0.385 1.9909999 0.429 1.9909999 1.342 Z M 0.99 6.5889997 C 0.99 6.303 1.254 6.017 1.518 6.017 C 1.826 6.017 2.09 6.314 2.09 6.545 C 2.09 6.809 1.859 7.117 1.562 7.117 C 1.298 7.117 0.99 6.853 0.99 6.5889997 Z \"/>\n",
" </symbol>\n",
" </defs>\n",
"</svg>\n",
"\n"
]
}
],
"source": [
"print(svg_content)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Decoded embedded SVG from svg_content:\n",
"<svg width=\"200\" height=\"100\" xmlns=\"http://www.w3.org/2000/svg\">\n",
" <text x=\"100\" y=\"50\" text-anchor=\"middle\" dominant-baseline=\"middle\"\n",
" font-family=\"Calibri, sans-serif\" font-size=\"40\" fill=\"#000000\">\n",
" World\n",
" </text>\n",
"</svg>\n"
]
}
],
"source": [
"# the \"World\" string is hidden in data:image/svg+xml;base64,PHN2Z... , and here's how to extract it:\n",
"import base64\n",
"\n",
"import re\n",
"embedded_base64 = re.search(r'data:image/svg\\+xml;base64,([^\"]+)', svg_content).group(1)\n",
"\n",
"# Decode the embedded SVG\n",
"embedded_decoded_bytes = base64.b64decode(embedded_base64)\n",
"embedded_decoded_svg = embedded_decoded_bytes.decode(\"utf-8\")\n",
"\n",
"# Write the embedded SVG to a file\n",
"with open(\"embedded_svg_output.svg\", \"w\", encoding=\"utf-8\") as svg_file:\n",
" svg_file.write(embedded_decoded_svg)\n",
"\n",
"# Print the decoded embedded SVG\n",
"print(\"\\nDecoded embedded SVG from svg_content:\")\n",
"print(embedded_decoded_svg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment