PyScript 🐰

🐍 Python dovunque e perchiunque

github.com/pietroppeter/pyscript-python-dovunque-perchiunque

<!DOCTYPE html>
<head>
    <title>Questa Γ¨ una WebApp</title>
    <style>
      ...
    </style>
</head>
<body>
    <button id="cliccami">questa</button>
    <script src="./main.js"></script>
</body>
</html>

main.js

// cicla su questi testi
const texts = ["questa", "this"];
let currentIndex = 0;

// prendi il bottone
const button = document.getElementById("cliccami");

// aggiungi event listener per evento click
button.addEventListener("click", () => {
    // aggiorna indice
    currentIndex = (currentIndex + 1) % texts.length;
    // aggiorna testo bottone
    button.textContent = texts[currentIndex];
});

🐰

main.py

from pyscript import document

def cambia(event):
    bottone = document.querySelector("#bottone")
    testo = bottone.innerText
    bottone.innerText = cicla(testo)

def cicla(testo: str) -> str:
    if testo == "questa":
        return "this"
    elif testo == "this":
        return "chistaccΓ "
    else:
        return "questa"

<!DOCTYPE html>
<head>
    <title>ChistaccΓ  Γ¨ una PyWebApp</title>
    <style>
      ...
    </style>
    <script type="module" src="https://pyscript.net/releases/2024.8.2/core.js"></script>
</head>
<body>
    <button id="bottone" mpy-click="cambia">questa</button>
    <script type="mpy" src="./main.py"></script>
</body>
</html>

If the Web and Python had a baby,
you'd get PyScript

-- docs.pyscript.net

βš™οΈ come funziona?

πŸ§‘β€πŸ’» Demo (Github Pages)

GitHub logo

πŸ’‘ Che ci faccio?

  1. cose frontend (πŸ”΅πŸŸ©πŸ”Ί...)
  2. contribuire open source πŸ™‹πŸΌβ€β™€οΈ
  3. imparo cose web (tech e cultura) 🌍

🌸 Creative Coding

πŸ“š risorse

πŸ™‡β€β™‚οΈ Grazie