28
loading...
This website collects cookies to deliver better user experience
pip install flask
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'hello, world'
run.bat
pour lancer facilement mon application :set FLASK_APP=main.py
.\venv\Scripts\flask run
python
mais on dit à flask
quel fichier décrit notre application grâce à une variable d'environnement.flask
, il démarre un serveur local, par défaut sur le port 5000, pour servir notre application :C:\python_flask_heroku>.\venv\bin\flask run
* Serving Flask app "main.py"
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [22/Apr/2021 11:56:20] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2021 11:58:30] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [22/Apr/2021 11:58:38] "GET /try HTTP/1.1" 404 -
/
du serveur qui ont fonctionné avec le code 200, ainsi qu'un accès à /try
qui a échoué avec le code 404. En effet, mon application définit bien une route vers /
, mais pas vers /try
.heroku
est désormais disponible depuis le terminal et nous pouvons nous connecter à notre compte avec heroku login
. Une page internet s'ouvre dans notre navigateur par défaut pour entrer nos identifiants.git push
vers un dépôt distant qui est sur Heroku. Ce Git contient le code ainsi que des fichiers de configuration pour expliquer à Heroku comment lancer tout ça.git init
.$ heroku apps
You have no apps.
$ heroku apps:create younup-flask
Creating ⬢ younup-flask... done
https://younup-flask.herokuapp.com/ | https://git.heroku.com/younup-flask.git
$ heroku apps
=== [email protected] Apps
younup-flask
$ git remote -v
heroku https://git.heroku.com/younup-flask.git (fetch)
heroku https://git.heroku.com/younup-flask.git (push)
runtime.txt
dans lequel on indique la version de Python qu'on souhaite utiliser. Il y a une seule ligne dans ce fichier :python-3.9.2
flask
gunicorn
flask
, OK. Mais pourquoi gunicorn
? Souvenez-vous plus haut d'un warning lors du lancement de Flask, nous conseillant de "Use a production WSGI server instead". Et bien c'est exactement ce qu'est gunicorn
:Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. It's a pre-fork worker model. The Gunicorn server is broadly compatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.
gunicorn
pour servir notre application Flask, on va utiliser le fichier Procfile
:Heroku apps include a Procfile that specifies the commands that are executed by the app on startup. You can use a Procfile to declare a variety of process types, including your app’s web server".
Procfile
:web: gunicorn main:app
heroku local
.git commit
et git push
:$ git push --set-upstream heroku master
Énumération des objets: 6, fait.
Décompte des objets: 100% (6/6), fait.
Compression par delta en utilisant jusqu'à 4 fils d'exécution
Compression des objets: 100% (3/3), fait.
Écriture des objets: 100% (6/6), 1.18 Kio | 1.18 Mio/s, fait.
Total 6 (delta 0), réutilisés 0 (delta 0), réutilisés du pack 0
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Building on the Heroku-20 stack
remote: -----> Determining which buildpack to use for this app
remote: -----> Python app detected
remote: ! Python has released a security update! Please consider upgrading to python-3.9.4
remote: Learn More: https://devcenter.heroku.com/articles/python-runtimes
remote: -----> Installing python-3.9.2
remote: -----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: Collecting flask
remote: Downloading Flask-1.1.2-py2.py3-none-any.whl (94 kB)
remote: Collecting gunicorn
remote: Downloading gunicorn-20.1.0.tar.gz (370 kB)
remote: Collecting itsdangerous>=0.24
remote: Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
remote: Collecting Werkzeug>=0.15
remote: Downloading Werkzeug-1.0.1-py2.py3-none-any.whl (298 kB)
remote: Collecting click>=5.1
remote: Downloading click-7.1.2-py2.py3-none-any.whl (82 kB)
remote: Collecting Jinja2>=2.10.1
remote: Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
remote: Collecting MarkupSafe>=0.23
remote: Downloading MarkupSafe-1.1.1-cp39-cp39-manylinux2010_x86_64.whl (32 kB)
remote: Building wheels for collected packages: gunicorn
remote: Building wheel for gunicorn (setup.py): started
remote: Building wheel for gunicorn (setup.py): finished with status 'done'
remote: Created wheel for gunicorn: filename=gunicorn-20.1.0-py3-none-any.whl size=78917 sha256=389135bf7e57c6bee7f1675ce59482c4d7ab3500ab4debf1c633e23986c5326c
remote: Stored in directory: /tmp/pip-ephem-wheel-cache-c0xwgcsz/wheels/ee/ca/72/3e9be4033d3993d4d78e2f4accdfcfff6c690921fef5ea0d57
remote: Successfully built gunicorn
remote: Installing collected packages: colorama, itsdangerous, Werkzeug, click, MarkupSafe, Jinja2, flask, gunicorn
remote: Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 colorama-0.4.4 flask-1.1.2 gunicorn-20.1.0 itsdangerous-1.1.0
remote: -----> Discovering process types
remote: Procfile declares types -> web
remote:
remote: -----> Compressing...
remote: Done: 52.6M
remote: -----> Launching...
remote: Released v3
remote: https://younup-flask.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/younup-flask.git
* [new branch] master -> master
pip
, puis il redémarre notre application, qui est maintenant disponible :runtime.txt
, on commit, on push, et le processus d'installation et de déploiement recommence. Cette fois, par contre, on lira :remote: -----> Python app detected
remote: -----> Found python-3.9.2, removing
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing python-3.9.4