Introduzione a Pynecone

Mattepuffo's logo
Introduzione a Pynecone

Introduzione a Pynecone

Pynecone è un web framework per Python, che però segue un approccio diversio da altri framework.

In pratica ci costruisce sia il backend che il frontend.

In questo articolo vediamo un esempio basico su come usarlo.

Prima di tutto dobbiamo installarlo; io ho creato una cartella di test, e dentro ci ho installato il framework usando venv:

(test-python) /test/test-python> pip install pynecone

Poi ho lanciato questi comandi:

(test-python) /test/test-python> mkdir test_pynecone
(test-python) /test/test-python> cd test_pynecone
(test-python) /test/test-python/test_pynecone> pc init
(test-python) /test/test-python/test_pynecone> pc run

Il comando init creerà tutte la cartelle necessarie, mentre run lancerà l'applicazione; sarà disponibile su http://localhost:3000.

A questo punto abbiamo il file pcconfig.py che ha la configuazione globale, e test_pynecone/test_pynecone.py che detiene le rotte e lancia l'app.

Questo file si trova ad un livello sotto rispetto alla configurazione globale.

Questo il mio modificato:

from pcconfig import config
import pynecone as pc

style = {
    "font_family": "Comic Sans MS",
    "font_size": "16px",
}

def index() -> pc.Component:
    return pc.center(
        pc.vstack(
            pc.heading("ROOT PAGE", font_size="2em"),
            pc.link(
                "GO TO ABOUT",
                href="/about",
                border="0.1em solid",
                padding="0.5em",
                border_radius="0.5em",
                _hover={
                    "color": "rgb(107,99,246)",
                },
            ),
            spacing="1.5em",
            font_size="2em",
        )
    )

def about():
    return pc.text("About Page")

app = pc.App(style=style)
app.add_page(index)
app.add_page(about)
app.compile()

Ho impostato:

  • due pagine
  • un link sulla prima
  • stile globale
  • stile inline

Enjoy!


Condividi

Commentami!