Connessione a PostgreSQL in JustPy

Mattepuffo's logo
Connessione a PostgreSQL in JustPy

Connessione a PostgreSQL in JustPy

In questo articolo vediamo come connetterci ad un db PostgreSQL in JustPy.

Queste le dipendenze (sempre che non abbiate già installato tutto) da installare con pip:

pip install justpy psycopg2

Qui sotto un pò di codice:

import justpy as jp
import psycopg2

def get_utenti():
    conn = psycopg2.connect(
        host='localhost',
        user='postgres',
        password='9211',
        dbname='postgres'
    )

    cursor = conn.cursor()
    cursor.execute("SELECT id, email FROM utenti")
    risultati = cursor.fetchall()
    cursor.close()
    conn.close()
    return risultati

def index():
    wp = jp.WebPage()
    table = jp.Table(a=wp, classes="q-table")
    tr_head = jp.Tr(a=table)
    jp.Th(a=tr_head, text="ID")
    jp.Th(a=tr_head, text="Email")
    for utente in get_utenti():
        tr = jp.Tr(a=table)
        jp.Td(a=tr, text=utente[0])
        jp.Td(a=tr, text=utente[1])
    return wp

jp.justpy(index)

Enjoy!


Condividi

Commentami!