|
| 1 | +#Getting started with PyScript |
| 2 | + |
| 3 | +This page will guide you through getting started with PyScript. |
| 4 | + |
| 5 | +##Development setup |
| 6 | + |
| 7 | +PyScript does not require any development environment other |
| 8 | +than a web browser. We recommend using Chrome. |
| 9 | + |
| 10 | +##Installation |
| 11 | + |
| 12 | +First, go tohttps://pyscript.net and download the PyScript assets. |
| 13 | +Unzip the archive to a directory where you wish to write PyScript-enabled |
| 14 | +HTML files. You should then have three files in your directory. |
| 15 | + |
| 16 | +``` |
| 17 | +├── ./ |
| 18 | +│ ├── pyscript.css |
| 19 | +│ ├── pyscript.js |
| 20 | +│ └── pyscript.js.map |
| 21 | +``` |
| 22 | + |
| 23 | +##Your first PyScript HTML file |
| 24 | + |
| 25 | +Here's a "Hello, world!" example using PyScript using the assets you |
| 26 | +downloaded fromhttps://pyscript.net. |
| 27 | + |
| 28 | +Using your favorite editor create a new file called`hello.html` in |
| 29 | +the same directory as your PyScript JavaScript and CSS files with the |
| 30 | +following content and open the file in your web browser. You can typically |
| 31 | +open an HTML by double clicking it in your file explorer. |
| 32 | + |
| 33 | +```html |
| 34 | +<html> |
| 35 | + <head> |
| 36 | + <linkrel="stylesheet"href="pyscript.css" /> |
| 37 | + <scriptdefersrc="pyscript.js"></script> |
| 38 | + </head> |
| 39 | + <body> <py-script> print('Hello, World!') </py-script> </body> |
| 40 | +</html> |
| 41 | +``` |
| 42 | + |
| 43 | +Notice the use of the`<py-script>` tag in the HTML body. This |
| 44 | +is where you'll write your Python code. In the following sections we'll |
| 45 | +introduce the 8 tags provided by PyScript. |
| 46 | + |
| 47 | +##The py-script tag |
| 48 | + |
| 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 | + <linkrel="stylesheet"href="pyscript.css" /> |
| 57 | + <scriptdefersrc="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 | + <linkrel="stylesheet"href="pyscript.css" /> |
| 89 | + <scriptdefersrc="pyscript.js"></script> |
| 90 | + <linkhref="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><labelid='today'></label></u></p></b> |
| 95 | + <br> |
| 96 | + <divid="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 | +##Packages and modules |
| 115 | + |
| 116 | +In addition to the[Python Standard Library](https://docs.python.org/3/library/) and |
| 117 | +the`pyscript` module, many 3rd-party OSS packages will work out-of-the-box with PyScript. |
| 118 | +In order to use them you will need to delcare the dependencies using the`<py-env>` in the |
| 119 | +HTML head. |
| 120 | + |
| 121 | +For example, NumPy and Matplotlib are available. Notice here we're using`<py-script output="plot">` |
| 122 | +as a shortcut, which takes the expression on the last line of the script and runs`pyscript.write('plot', fig)`. |
| 123 | + |
| 124 | + |
| 125 | +```html |
| 126 | +<html> |
| 127 | + <head> |
| 128 | + <linkrel="stylesheet"href="pyscript.css" /> |
| 129 | + <scriptdefersrc="pyscript.js"></script> |
| 130 | + <py-env> |
| 131 | + - numpy |
| 132 | + - matplotlib |
| 133 | + </py-env> |
| 134 | + </head> |
| 135 | + |
| 136 | + <body> |
| 137 | + <h1>Let's plot random numbers</h1> |
| 138 | + <divid="plot"></div> |
| 139 | + <py-scriptoutput="plot"> |
| 140 | +import matplotlib.pyplot as plt |
| 141 | +import numpy as np |
| 142 | + |
| 143 | +x = np.random.randn(1000) |
| 144 | +y = np.random.randn(1000) |
| 145 | + |
| 146 | +fig, ax = plt.subplots() |
| 147 | +ax.scatter(x, y) |
| 148 | +fig |
| 149 | + </py-script> |
| 150 | + </body> |
| 151 | +</html> |
| 152 | +``` |