在Cloudflare Worker上运行的JavaScript中的Python代码

2024-10-01 09:32:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个worker,它显示使用python代码triangles.py生成的svg文件。在这段python代码中,我有一个函数:

getSVGCode(vw, vh):
    ⋮
    callingOtherFunction(inThisFile)
    ⋮
    return svgCode

需要从Cloudflare Workers上运行的javascript调用此函数(使用下面给出的方法)。svgCode插入到javascript返回到浏览器的html字符串中

const html = `<!DOCTYPE html>
<body>
  <h1>Hello World</h1>

  <svg>My svg here</svg>

  <p>This markup was generated by a Cloudflare Worker.</p>
</body>`

async function handleRequest(request) {
  return new Response(html, {
    headers: {
      "content-type": "text/html;charset=UTF-8",
    },
  })
}

addEventListener("fetch", event => {
  return event.respondWith(handleRequest(event.request))
})

因此,我想知道一种让Javascript代码将视口维度(vhvw)传递给python函数的方法,以便可以使用此大小生成svg。如果这个选项不可行,我将不得不调整svg的大小,它看起来会被拉伸。我还想知道如何让工作人员也从javascript运行python代码

我是一名代码初学者,尤其是javascript。请提供完整信息,以便我能理解您的答案。谢谢


Tags: 方法函数代码svgeventreturnhtmlbody
1条回答
网友
1楼 · 发布于 2024-10-01 09:32:52

因此,我想我可以在html变量中使用<script></script>。我用了如下的方法

<script>
    const vw = document.documentElement.clientWidth;
    const vh = document.documentElement.clientHeight;
    var svgHref = "https://your.url.here/using?height=vh&ampwidth=vw";
    document.getElementById("triangles-svg").src = svgHref;
</script>

伴随着一具尸体

<body>
    <img id="triangles-svg">
</body>

中提琴.)

相关问题 更多 >