Files
pyscript/docs/reference/elements/py-script.md
Madhur Tandon 515858f313 implement proposal for fetching paths and retaining structure of dirs and packages (#914)
* implement proposal

* update docs and replace py-env

* more docs

* suggested proposal

* update docs

* add to_file parameter

* remove comment from Makefile

* suggested improvements

* move tests from basic to py_config

* retain leading slash from the first path
2022-11-08 17:26:45 +05:30

1.2 KiB

<py-script>

The <py-script> element lets you execute multi-line Python scripts both inline and via a src attribute.

Attributes

attribute type default description
src url Url to a python source file.

Examples

  • Inline <py-script> element:
<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
  </head>
  <body>
      <py-script>
        print("Let's compute π:")
        def compute_pi(n):
            pi = 2
            for i in range(1,n):
                pi *= 4 * i ** 2 / (4 * i ** 2 - 1)
            return pi

        pi = compute_pi(100000)
        s = f"π is approximately {pi:.3f}"
        print(s)
      </py-script>
  </body>
</html>
  • <py-script> element with src attribute:
<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
    <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    <py-config>
    [[fetch]]
    files =["compute_pi.py"]
    </py-config>
  </head>
  <body>
      <py-script src="compute_pi.py"></py-script>
  </body>
</html>