# <py-script> The `` 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 `` element: Let's execute this multi-line Python script to compute π and print it back onto the page ```html 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) ``` ### Using `` element with `src` attribute: we can also move our python code to its own file and reference it via the `src` attribute. ```python # compute_pi.py 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) ``` Since both compute_pi.py and index.html are in the same directory, we can reference the python file with a relative path. ```html ``` ### Writing into labeled elements In the example above, we had a single `` tag printing one or more lines onto the page in order. Within the ``, you can use the `Element` class to create a python object for interacting with page elements. Objects created from the `Element` class provide the `.write()` method which enables you to send strings into the page elements referenced by those objects. For example, we'll add some style elements and provide placeholders for the `` tag to write to. ```html

Today is


import datetime as dt Element('today').write(dt.date.today().strftime('%A %B %d, %Y')) 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) Element('pi').write(f'π is approximately {pi:.3f}') ```