@@ -46,4 +46,71 @@ introduce the 8 tags provided by PyScript.
46
46
47
47
## The py-script tag
48
48
49
- ...
49
+ The ` <py-script> ` tag let's you execute multi-line Python scripts and
50
+ print back onto the page. For
51
+ example we can compute π.
52
+
53
+ ``` html
54
+ <html >
55
+ <head >
56
+ <link rel =" stylesheet" href =" pyscript.css" />
57
+ <script defer src =" pyscript.js" ></script >
58
+ </head >
59
+ <body >
60
+ <py-script >
61
+ print("Let's compute π:")
62
+ def wallis(n):
63
+ pi = 2
64
+ for i in range(1,n):
65
+ pi *= 4 * i ** 2 / (4 * i ** 2 - 1)
66
+ return pi
67
+
68
+ pi = wallis(100000)
69
+ s = f"π is approximately {pi:.3f}"
70
+ print(s)
71
+ </py-script >
72
+ </html >
73
+ ```
74
+
75
+ ### Writing into labeled elements
76
+
77
+ In the example above we had a single ` <py-script> ` tag and it printed
78
+ one or more lines onto the page in order. Within the ` <py-script> ` you
79
+ have access to the ` pyscript ` module, which provides a ` .write() ` method
80
+ to send strings into labeled elements on the page.
81
+
82
+ For example we'll add some style elements and provide place holders for
83
+ the ` <py-script> ` tag write to.
84
+
85
+ ``` html
86
+ <html >
87
+ <head >
88
+ <link rel =" stylesheet" href =" pyscript.css" />
89
+ <script defer src =" pyscript.js" ></script >
90
+ <link href =" https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel =" stylesheet" crossorigin =" anonymous" >
91
+ </head >
92
+
93
+ <body >
94
+ <b ><p >Today is <u ><label id =' today' ></label ></u ></p ></b >
95
+ <br >
96
+ <div id =" pi" class =" alert alert-primary" ></div >
97
+ <py-script >
98
+ import datetime as dt
99
+ pyscript.write('today', dt.date.today().strftime('%A %B %d, %Y'))
100
+
101
+ def wallis(n):
102
+ pi = 2
103
+ for i in range(1,n):
104
+ pi *= 4 * i ** 2 / (4 * i ** 2 - 1)
105
+ return pi
106
+
107
+ pi = wallis(100000)
108
+ pyscript.write('pi', f'π is approximately {pi:.3f}')
109
+ </py-script >
110
+ </body >
111
+ </html >
112
+ ```
113
+
114
+
115
+
116
+ ## Asynchronous
0 commit comments