mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 12:12:59 -05:00
* Reference Docs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * py-repl * pyrepl Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
54 lines
1.2 KiB
Markdown
54 lines
1.2 KiB
Markdown
# <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
|
|
<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
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
|
|
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
|
|
<py-config>
|
|
paths =[
|
|
"compute_pi.py"
|
|
]
|
|
</py-config>
|
|
</head>
|
|
<body>
|
|
<py-script src="compute_pi.py"></py-script>
|
|
</body>
|
|
</html>
|
|
```
|