我们可以在 PyScript 中使用循环吗?

Can we use loops in PyScript?

每次我尝试在 PyScript 标记中使用 for 循环时,都会收到无效的语法错误:/。我想做的是(例如):

Python:

attr1=['A','B','C']

for i in attr1:
         pyscript.write('output1',i) 

HTML:

<div id='output1'></div>

您可以只使用 print 写入文档,不过目前您对标签没有太多选择。例如,

<!doctype html>
<html>
  <head>
    <title></title>
    <meta charset="utf-8" />
  </head>
  <body>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <py-script>
      l = ["A", "B", "C"]
      for el in l:
          print(el)
      </py-script>
  </body>
</html>

会显示

A
B
C

在浏览器中。

它实际上生成了以下内容:

<html><head>
    <title></title>
    <meta charset="utf-8">
  <script src="https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js"></script></head>
  <body>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css">
    <script defer="" src="https://pyscript.net/alpha/pyscript.js"></script>
    <py-script id="py-2536bda2-b41c-7165-4257-38c5d650d507"><div class="parentBox flex flex-col mx-8">
    <div id="py-2536bda2-b41c-7165-4257-38c5d650d507"></div>
    </div>

<div id="py-2536bda2-b41c-7165-4257-38c5d650d507-2">A</div><div id="py-2536bda2-b41c-7165-4257-38c5d650d507-3">
</div><div id="py-2536bda2-b41c-7165-4257-38c5d650d507-4">B</div><div id="py-2536bda2-b41c-7165-4257-38c5d650d507-5">
</div><div id="py-2536bda2-b41c-7165-4257-38c5d650d507-6">C</div><div id="py-2536bda2-b41c-7165-4257-38c5d650d507-7">
</div></py-script>
  

<py-config></py-config></body></html>