Archives Wild Code School
Python - GUI

05 Dash - Déploiement - Bonus

Quête 5 sur 7

Introduction

Déployer une application Dash est une étape cruciale pour la partager avec un public plus large. Après avoir développé et testé ton application en local, la déployer sur un serveur web la rend accessible à toute personne disposant d'une connexion internet. Il existe plusieurs façons de déployer une application Dash, chacune avec ses propres avantages et ses exigences spécifiques.

🎯 Objectifs

À la fin de cette quête, tu seras capable de :

✅ Déployer une application Dash sur Heroku
✅ Gérer les dépendances avec un fichier requirements.txt
✅ Configurer un Procfile pour l'exécution sur Heroku
✅ Utiliser Git pour le déploiement vers Heroku
✅ Vérifier et accéder à l'application déployée


Déployer une application Dash

Une fois ton application Dash construite, tu peux la déployer sur un serveur web. Cela te permet de la partager avec d'autres personnes.

Il existe plusieurs options pour déployer une application Dash. Une méthode consiste à utiliser une plateforme de déploiement cloud (hébergement à distance) comme Heroku ou AWS Elastic Beanstalk. Ces plateformes fournissent une infrastructure prête à l'emploi pour déployer des applications web.

Une autre option consiste à héberger ton application sur ton propre serveur web. Cela implique d'installer le framework (cadre de développement) Dash sur ton serveur.

🚀 Déployer sur Heroku

Préparation du déploiement

1. Vérifier les dépendances

Assure-toi que toutes les bibliothèques nécessaires sont listées dans un fichier requirements.txt. Place ce fichier dans le même répertoire que ton fichier principal d'application (par exemple, app.py).

Exemple de fichier requirements.txt :

dash
dash-bootstrap-components
pandas
plotly
gunicorn
2. Structure du projet
  • Organise ton code Dash dans un fichier Python principal (par exemple, app.py)
  • Assure-toi que les éventuels fichiers CSS sont placés dans un dossier nommé assets
3. Créer un Procfile pour Heroku
  • Ce fichier indique à Heroku comment exécuter ton application
  • Contenu typique du Procfile : web: gunicorn app:server
  • gunicorn est un serveur HTTP WSGI (interface standard entre un serveur web et une application Python) utilisé pour le déploiement en production

Important : Si ton fichier Python contient app = Dash(__name__ ...), pense à ajouter la ligne suivante en dessous :
server = app.server dans ton fichier app.py

Étapes de déploiement sur Heroku

1. Créer un compte Heroku et installer le Heroku CLI

Remarque : Pour accéder à tous les services Heroku, il se peut qu'on te demande de vérifier ton compte avec une carte bancaire.


2. Initialiser un dépôt Git

Avant de commencer, assure-toi d'avoir un compte GitHub.

  • Lance git init dans le répertoire de ton projet
  • Ajoute tous les fichiers au dépôt avec :
    git add . N'oublie pas le . à la fin
  • Valide les changements (commit) :
    git commit -m "Mon message de commit"

3. Créer une nouvelle application sur Heroku

Utilise heroku create pour créer une nouvelle application.

4. Déploiement sur Heroku :
  • Pousse le code vers Heroku avec git push heroku master.
5. Vérifier et accéder à l'application

L'application est maintenant déployée et accessible via une URL fournie par Heroku.

Tape simplement heroku apps dans le terminal, et sur la dernière ligne tu trouveras le nom attribué à ton application, par exemple : powerful-cove-00818. Une fois ce nom en main, tu peux accéder à ton application à l'adresse :
https://le-nom-attribue-a-ton-app.herokuapp.com/


📚 Ressources

↗
Mises en page avec Dash
csyhuang.github.io

💪 Défi

Déploie sur Heroku l'application Dash que tu as construite dans la quête précédente.

Introduction

Deploying a Dash application is a crucial step in sharing it with a wider audience. After developing and testing your application locally, deploying it on a web server makes it accessible to anyone with an internet connection. There are several ways to deploy a Dash app, each with its own advantages and specific requirements.

🎯 Objectives

By the end of this quest, you will be able to:

✅ Deploy a Dash application on Heroku
✅ Manage dependencies with a requirements.txt file
✅ Configure a Procfile for execution on Heroku
✅ Use Git for deployment to Heroku
✅ Verify and access the deployed application


Deploying a Dash Application

Once you've built your Dash app, you can deploy it on a web server. This allows you to share your app with others.

There are multiple options for deploying a Dash app. One method is to use a cloud deployment platform such as Heroku or AWS Elastic Beanstalk. These platforms provide ready-to-use infrastructure for deploying web apps.

Another option is to host your application on your own web server. This involves installing the Dash framework on your server.

🚀 Deploying on Heroku

Preparation for Deployment

1. Verify Dependencies

Ensure all necessary libraries are listed in a requirements.txt file. Place this file in the same directory as your main app file (e.g., app.py).

Example of a requirements.txt file:

dash
dash-bootstrap-components
pandas
plotly
gunicorn
2. Project Structure
  • Organise your Dash code in a main Python file (e.g., app.py)
  • Ensure that any CSS files are placed in a folder named assets
3. Create a Procfile for Heroku
  • This file tells Heroku how to run your application
  • Typical content of the Procfile: web: gunicorn app:server
  • gunicorn is a WSGI HTTP server used for production deployment

Important: If your Python file includes app = Dash(__name__ ...), make sure to add the following line beneath it:
server = app.server dans votre fichier app.py

Deployment Steps on Heroku

1. Create a Heroku Account and Install Heroku CLI

Note: To access all Heroku services, you may be asked to verify your account with a credit card.


2. Initialise a Git Repository

Before starting, ensure you have a GitHub account.

  • Run git init in your project directory
  • Add all files to the repository with:
    git add . Don't forget . at the end
  • Commit the changes:
    git commit -m "My commit message"

3. Create a New Application on Heroku

Use heroku create to create a new app.

4. Deployment on Heroku:
  • Push the code to Heroku using git push heroku master.
5. Verify and Access the Application

The application is now deployed and accessible via a URL provided by Heroku.

Simply type heroku apps in the terminal, and on the last line you will find the name assigned to your app, for example: powerful-cove-00818. Once you have this name, you can access your app at:
https://your-assigned-app-name.herokuapp.com/


📚 Resources

↗
Dash Layouts
csyhuang.github.io

💪 Challenge

Deploy the Dash application you built in the previous quest on Heroku.

Ressources partagées sur cette quête