mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-19 10:17:23 -05:00
* Initial setup. This does a few things: - Adds some placeholders following the Diátaxis framework (https://diataxis.fr) - Sets up Sphinx with MyST parser for Markdown in addition to rST. - Uses the well-known PyData Sphinx theme. - Moves some already existing Markdown files into the docs directory. - Sets up the initial doc review GitHub action to auto-deploy to GitHub pages. * Activate conda env. * Remove custom action. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make the dir. * Push directly * Add readthedocs config. * Disable GitHub pages deployment for now. * Add release and latest workflows as well. * Make clear that this is work in progress. * Made docs merge ready, added What is PyScript section with example. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Replace `on:tags:` with `on:create:` The existing trigger is apparently not in the GHA spec * Pretty format YAML * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add s3 sync and permissions * Leave status message in PR. * Redirect from docs.pyscript.net/ to docs.pyscript.net/latest/ * Delete latest directory before deployment. * Update review and release workflows, too. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Allow access to S3 for review and release doc workflow. * Fix name of workflow. * Bump up Python version. * Because YAML. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Revert move to 3.10. * Fix sitemap. * Remove status settgin from release and latest build. * Comment out cleanup. * Add write permissions for statuses. * More permissions? * Fix artifact name. * Use appropriate concurrency. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * .zip not needed * Align name of workflows with CI workflows. * Add checks permission. * Set a notice instead. * Move to sphinx-design. * Add sphinx-autobuild. * Use frontmatter more. * Add section for mdformat but disable it for now. See https://github.com/executablebooks/mdformat-myst/pull/9 for more details. * Fix fencing. * Actually using html renderer. * Revert moving governance files. * Use full URLs for governance docs. * Added warning. * Fix copyright and author. * Another minor fix. * Use GitHub Action summary instead of notice. * Fix variable name. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Matt Kramer <mkramer@anaconda.com> Co-authored-by: ximena9201 <ximenandrea.ro@gmail.com>
210 lines
6.0 KiB
Markdown
210 lines
6.0 KiB
Markdown
# Getting started with PyScript
|
|
|
|
This page will guide you through getting started with PyScript.
|
|
|
|
## Development setup
|
|
|
|
PyScript does not require any development environment other
|
|
than a web browser. We recommend using [Chrome](https://www.google.com/chrome/).
|
|
|
|
If you're using [VSCode](https://code.visualstudio.com/), the
|
|
[Live Server extension](https://marketplace.visualstudio.com/items?itemName=ritwickdey.LiveServer)
|
|
can be used to reload the page as you edit the HTML file.
|
|
|
|
## Installation
|
|
|
|
There is no installation required. In this document, we'll use
|
|
the PyScript assets served on https://pyscript.net.
|
|
|
|
If you want to download the source and build it yourself, follow
|
|
the instructions in the README.md file.
|
|
|
|
## Your first PyScript HTML file
|
|
|
|
Here's a "Hello, world!" example using PyScript.
|
|
|
|
Using your favorite editor, create a new file called `hello.html` in
|
|
the same directory as your PyScript, JavaScript, and CSS files with the
|
|
following content, and open the file in your web browser. You can typically
|
|
open an HTML by double-clicking it in your file explorer.
|
|
|
|
```html
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
|
|
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
|
|
</head>
|
|
<body> <py-script> print('Hello, World!') </py-script> </body>
|
|
</html>
|
|
```
|
|
|
|
Notice the use of the `<py-script>` tag in the HTML body. This
|
|
is where you'll write your Python code. In the following sections, we'll
|
|
introduce the eight tags provided by PyScript.
|
|
|
|
## The py-script tag
|
|
|
|
The `<py-script>` tag lets you execute multi-line Python scripts and
|
|
print back onto the page. For example, we can compute π.
|
|
|
|
```html
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
|
|
<script defer src="https://pyscript.net/alpha/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>
|
|
```
|
|
|
|
### Writing into labeled elements
|
|
|
|
In the example above, we had a single `<py-script>` tag and it printed
|
|
one or more lines onto the page in order. Within the `<py-script>`, you
|
|
have access to the `pyscript` module, which provides a `.write()` method
|
|
to send strings into labeled elements on the page.
|
|
|
|
For example, we'll add some style elements and provide place holders for
|
|
the `<py-script>` tag write to.
|
|
|
|
```html
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
|
|
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
|
|
</head>
|
|
|
|
<body>
|
|
<b><p>Today is <u><label id='today'></label></u></p></b>
|
|
<br>
|
|
<div id="pi" class="alert alert-primary"></div>
|
|
<py-script>
|
|
import datetime as dt
|
|
pyscript.write('today', 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)
|
|
pyscript.write('pi', f'π is approximately {pi:.3f}')
|
|
</py-script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
## Packages and modules
|
|
|
|
In addition to the [Python Standard Library](https://docs.python.org/3/library/) and
|
|
the `pyscript` module, many 3rd-party OSS packages will work out-of-the-box with PyScript.
|
|
|
|
In order to use them, you will need to declare the dependencies using the `<py-env>` in the
|
|
HTML head. You can also link to `.whl` files directly on disk like in our [toga example](https://github.com/pyscript/pyscript/blob/main/pyscriptjs/examples/toga/freedom.html).
|
|
|
|
```
|
|
<py-env>
|
|
- './static/wheels/travertino-0.1.3-py3-none-any.whl'
|
|
</py-env>
|
|
```
|
|
|
|
If your `.whl` is not a pure Python wheel, then open a PR or issue with [pyodide](https://github.com/pyodide/pyodide) to get it added [here](https://github.com/pyodide/pyodide/tree/main/packages).
|
|
If there's enough popular demand, the pyodide team will likely work on supporting your package. Regardless, things will likely move faster if you make the PR and consult with the team to get unblocked.
|
|
|
|
For example, NumPy and Matplotlib are available. Notice here we're using `<py-script output="plot">`
|
|
as a shortcut, which takes the expression on the last line of the script and runs `pyscript.write('plot', fig)`.
|
|
|
|
```html
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
|
|
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
|
|
<py-env>
|
|
- numpy
|
|
- matplotlib
|
|
</py-env>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Let's plot random numbers</h1>
|
|
<div id="plot"></div>
|
|
<py-script output="plot">
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
x = np.random.randn(1000)
|
|
y = np.random.randn(1000)
|
|
|
|
fig, ax = plt.subplots()
|
|
ax.scatter(x, y)
|
|
fig
|
|
</py-script>
|
|
</body>
|
|
</html>
|
|
```
|
|
|
|
### Local modules
|
|
|
|
In addition to packages, you can declare local Python modules that will
|
|
be imported in the `<py-script>` tag. For example, we can place the random
|
|
number generation steps in a function in the file `data.py`.
|
|
|
|
```python
|
|
# data.py
|
|
import numpy as np
|
|
|
|
|
|
def make_x_and_y(n):
|
|
x = np.random.randn(n)
|
|
y = np.random.randn(n)
|
|
return x, y
|
|
```
|
|
|
|
In the HTML tag `<py-env>`, paths to local modules are provided in the
|
|
`paths:` key.
|
|
|
|
```html
|
|
<html>
|
|
<head>
|
|
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
|
|
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
|
|
<py-env>
|
|
- numpy
|
|
- matplotlib
|
|
- paths:
|
|
- /data.py
|
|
</py-env>
|
|
</head>
|
|
|
|
<body>
|
|
<h1>Let's plot random numbers</h1>
|
|
<div id="plot"></div>
|
|
<py-script output="plot">
|
|
import matplotlib.pyplot as plt
|
|
from data import make_x_and_y
|
|
|
|
x, y = make_x_and_y(n=1000)
|
|
|
|
fig, ax = plt.subplots()
|
|
ax.scatter(x, y)
|
|
fig
|
|
</py-script>
|
|
</body>
|
|
</html>
|
|
```
|