Get Started
Create the project structure
-
Clone the prune repo into your folder:
-
Now create 3 files
index.html
,pyscript.json
andcounter.py
:
Finally your project should look like this :
- index.html
- counter.py
- pyscript.json
Directoryprunepy/
- …
Add some logic in python
The counter.py
previously created will store the logic of the front.
In this example we will see how to create a simple counter app (Yes I know… for the 1000th time, but this time it will be different, I swear).
Here you write your logic like in a normal python file.
Setup pyscript.json
The pyscript.json
file is just a mapping between the URL location of your python files and their location in the python virtual filetree.
The left part is the URL of the file on the server and the right part is the location in python filetree.
Now in counter.py
we are able to import prune.py
as if it was at the same level in the filetree even if prune.py
is into prunepy/prune.py
Nothing really important here.
Plug the logic into the HTML
Now it’s the most interesting part, we will plug the logic into the HTML.
The result 🥳
About the import
The first two marked lines are symply the pyscript import, thanks to pyscript we can run python in the browser.
The last marked line is the link to the counter.py file,
type="mpy"
tells to pyscript to use micropython, a lightweight python interpreter.src
is the URL path to the python file we want to link.config
is the URL path to pyscript.json, which solves import in our python files.
About the templating
As you can see we use p-...
attribute to plug our logic into the HTML.
Let’s see what happens here :
p-text
will change the text of the element to the given expression, in this case it’sstore.counter.value
but it could be any valid python expressions@click
is run when a click event happens on the element