从 sympy / svgmath 生成 SVG 时出错

发布于 2024-10-21 13:15:10 字数 3011 浏览 1 评论 0原文

我正在尝试使用 sympy 和 svgmath 将代数渲染为 SVG。我的根目录中有文件“svgmath.xml”。不幸的是我得到:“ImportError:没有名为 libxml2 的模块”。我不确定解决这个问题的最佳方法。我非常感谢任何帮助/建议。我的代码是:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(c2p(mathml(3*x), simple=False))

堆栈跟踪现在是:

Traceback (most recent call last):
  File "D:\Geddes\my python\SVGmath\test1.py", line 29, in <module>
    parser.parse(c2p(mathml(3*x), simple=True))
  File "C:\Python27\lib\xml\sax\expatreader.py", line 102, in parse
    source = saxutils.prepare_input_source(source)
  File "C:\Python27\lib\xml\sax\saxutils.py", line 298, in prepare_input_source
    f = urllib.urlopen(source.getSystemId())
  File "C:\Python27\lib\urllib.py", line 84, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 202, in open
    return self.open_unknown(fullurl, data)
  File "C:\Python27\lib\urllib.py", line 214, in open_unknown
    raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: '?xml%20version=%221.0%22?%3e%0a%3cmath%20xmlns=%22http'

我修改后的代码:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

import libxml2

import StringIO

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(StringIO.StringIO(mathml(3*x)))

这无法生成有效的 SVG 文件并给出:

[WARNING] line 1, column 0: Root element in MathML document must be 'math'
[WARNING] line 1, column 7: MathML element 'times' is unsupported
[WARNING] line 1, column 15: MathML element 'cn' is unsupported
[WARNING] line 1, column 25: MathML element 'ci' is unsupported
[WARNING] line 1, column 0: MathML element 'apply' is unsupported

I am trying to render algebra as an SVG using sympy and svgmath. I have the file 'svgmath.xml' in the root directory. Unfortunately I get: 'ImportError: No module named libxml2'. I'm not sure of the best way to sort this out. I would much appreciate any help/advice. My code is:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(c2p(mathml(3*x), simple=False))

The Stack trace is now:

Traceback (most recent call last):
  File "D:\Geddes\my python\SVGmath\test1.py", line 29, in <module>
    parser.parse(c2p(mathml(3*x), simple=True))
  File "C:\Python27\lib\xml\sax\expatreader.py", line 102, in parse
    source = saxutils.prepare_input_source(source)
  File "C:\Python27\lib\xml\sax\saxutils.py", line 298, in prepare_input_source
    f = urllib.urlopen(source.getSystemId())
  File "C:\Python27\lib\urllib.py", line 84, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 202, in open
    return self.open_unknown(fullurl, data)
  File "C:\Python27\lib\urllib.py", line 214, in open_unknown
    raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: '?xml%20version=%221.0%22?%3e%0a%3cmath%20xmlns=%22http'

My modified code:

from xml import sax
from xml.sax.saxutils import XMLGenerator

from svgmath.tools.saxtools import XMLGenerator, ContentFilter
from svgmath.mathhandler import MathHandler, MathNS
from svgmath.tools.saxtools import XMLGenerator

from sympy.printing.mathml import mathml
from sympy.abc import x

from sympy.utilities.mathml import c2p

import libxml2

import StringIO

# Open all files
output = open("test.svg", "w") 
config = open("svgmath.xml", "r")    

# Create the converter as a content handler. 
saxoutput = XMLGenerator(output, 'utf-8')
handler = MathHandler(saxoutput, config)

# Parse input file with a namespace-aware SAX parser
parser = sax.make_parser()
parser.setFeature(sax.handler.feature_namespaces, 1)
parser.setContentHandler(handler)
parser.parse(StringIO.StringIO(mathml(3*x)))

This fails to produce a valid SVG file and gives:

[WARNING] line 1, column 0: Root element in MathML document must be 'math'
[WARNING] line 1, column 7: MathML element 'times' is unsupported
[WARNING] line 1, column 15: MathML element 'cn' is unsupported
[WARNING] line 1, column 25: MathML element 'ci' is unsupported
[WARNING] line 1, column 0: MathML element 'apply' is unsupported

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-10-28 13:15:10

安装libxml2 库。例如,在 ubuntu 上,这是 python-libxml2 包。我也尝试过 sympy ->数学-> svg 路线一次,我还必须安装该包(iirc 它需要 c2p(),它应用 XSLT 将内容转换为表示 MathML)。

Install the libxml2 library. For example, on ubuntu this is the python-libxml2 package. I too tried the sympy -> mathml -> svg route once, I also had to install the package (iirc it's needed for c2p(), which applies a XSLT to convert content to presentation MathML).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文