1

我将 MathJax 用于 Python/Google App Engine CRUD webapp。我希望人们使用 Content MathML 创建方程式(以便 webapp 可以利用其他地方的语义信息)。

然后我想使用这个 Content-to-Presentation MML XSLT,以便我可以在生成的 Presentation MathML 上使用 MathJax。

通常,我会做这样的事情来提供转换服务:

import lxml.etree as etree

class MathMLTranslator(object):
    def __init__(self):
        with open('ctop.xsl') as f:
            self.xslt = etree.XSLT(etree.XML(f.read()))

    def translate(self, xml_string):
            return self.xslt(etree.XML(xml_string))

但我不能open('ctop.xsl')在 Google App Engine 中做,也不能只是将 的内容ctop.xsl作为字符串文字粘贴到 MathMLTranslator 中(它太大并且有两种类型的引号)。

我该如何处理?

4

3 回答 3

1

您可以在 App Engine 上打开作为应用程序的一部分上传的文件 - 您只需确保路径是相对于应用程序的。例如,如果ctop.xsl与您的 Python 模块位于同一目录中,您可以这样做:

fh = open(os.path.join(os.path.dirname(__file__), 'ctop.xsl')
于 2012-01-16T01:55:07.597 回答
0

我对 GAE 不熟悉,但是如果您可以上传任意大小的 Python 文件,则可以将ctop.xsl内容写入 .py 文件。例如ctop_data.py

ctop_xsl = """<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
... SKIPPED
"""

然后在代码中你可以:

from ctop_data import ctop_xsl
...
self.xslt = etree.XSLT(etree.XML(ctop_xsl))
于 2012-01-15T07:16:00.860 回答
-1

您可以将文件放在保管箱上并使用保管箱 API:

https://www.dropbox.com/developers/reference/api#files-GET

为此,我建议使用 python-requests:

https://github.com/kennethreitz/requests

我没有从谷歌应用引擎做到这一点,虽然我确实有代码在 heroku 上运行做类似的事情。

于 2012-01-14T22:24:52.070 回答