Home > OS >  Can we use loops in PyScript?
Can we use loops in PyScript?

Time:05-21

Everytime i tried to use for loop in the PyScript tag, getting an invalid Syntax error :/.What i want to do is (for example):

Python:

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

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

HTML:

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

CodePudding user response:

You can just write to the document with print, it doesn't leave you with much choice about the tags for now though. For example,

<!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>

Will show

A
B
C

in the browser.

and it actually generates the following:

<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 >
    <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>
  • Related