Scrivi e testa localmente un servizio web che pubblica un file HTML statico utilizzando Flask. Quindi, crea i file di configurazione necessari per eseguire il deployment del servizio web in App Engine.
In questo passaggio, crei e testi localmente una versione di un servizio web che visualizza i dati segnaposto. L'obiettivo è assicurarti che il servizio web di base funzioni prima di aggiungere l'autenticazione Datastore e Firebase.
Prima di iniziare
Se non hai ancora creato un progetto Google Cloud, creane uno.
Se non lo hai già fatto, configura il tuo ambiente locale per Python 3 completando i seguenti passaggi:
Scarica e installa Python 3 per sviluppare il tuo servizio web ed eseguire Google Cloud CLI.
Utilizza le tue credenziali utente Google Cloud per l'autenticazione con Google Cloud CLI e abilita i test locali con Datastore:
gcloud auth application-default login
Strutturare i file dei servizi web
La directory del progetto in cui crei il servizio web avrà la seguente struttura di file:
building-an-app/
app.yaml
main.py
requirements.txt
static/
script.js
style.css
templates/
index.html
Le sezioni seguenti forniscono un esempio di come configurare i file nella directory del progetto.
Scrittura del servizio web
L'iterazione iniziale del servizio web utilizza Flask per pubblicare un modello HTML basato su Jinja.
Per configurare il servizio web:
Crea il tuo file
templates/index.html
:Aggiungi comportamenti e stili con i file
static/script.js
estatic/style.css
:Nel file
main.py
, utilizza Flask per eseguire il rendering del modello HTML con i dati del segnaposto:Configura tutte le dipendenze necessarie per il servizio web nel tuo File
requirements.txt
:
Prova il tuo servizio web
Testa il servizio web eseguendolo localmente in un ambiente virtuale:
Mac OS/Linux
- Crea un
ambiente Python isolato:
python3 -m venv env
source env/bin/activate
- Se non sei nella directory che contiene il codice di esempio, vai alla directory che contiene il codice di esempio
hello_world
. Quindi installa dependencies:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
- Esegui l'applicazione:
python main.py
- Nel browser web, inserisci il seguente indirizzo:
http://localhost:8080
Windows
Utilizza le funzionalità di PowerShell per eseguire i pacchetti Python.
- Individua l'installazione di PowerShell.
- Fai clic con il tasto destro del mouse sulla scorciatoia di PowerShell e avviala come amministratore.
- Crea un
ambiente Python isolato.
python -m venv env
.\env\Scripts\activate
- Naviga alla directory del progetto e installa le dipendenze. Se non sei nella directory
che contiene il codice campione, passa alla directory che contiene
Codice campione
hello_world
. Quindi, installa le dipendenze:cd YOUR_SAMPLE_CODE_DIR
pip install -r requirements.txt
- Esegui l'applicazione:
python main.py
- Nel browser web, inserisci il seguente indirizzo:
http://localhost:8080
Configurare il servizio web per App Engine
Per eseguire il deployment del servizio web in App Engine, devi disporre di un file app.yaml
.
Questo file di configurazione definisce le impostazioni del servizio web per App Engine.
Per configurare il servizio web per il deployment in App Engine, crea il file app.yaml
nella directory principale del progetto, ad esempio building-an-app
:
Tieni presente che per questo semplice servizio web, il file app.yaml
deve definire solo l'impostazione di runtime e i gestori per i file statici.
Per servizi web più complicati, puoi configurare impostazioni aggiuntive
nel tuo app.yaml
, come scalabilità, gestori aggiuntivi e
degli elementi dell'applicazione, come le variabili di ambiente
e i nomi dei servizi.
Per ulteriori informazioni e per un elenco di tutti gli elementi supportati, consulta la
Riferimento app.yaml
.
Passaggi successivi
Dopo aver configurato, creato e testato il servizio web, puoi di questa versione del tuo servizio web in App Engine.