Partager des fichiers via un site web

Introduction à AWS Boto en Python

Maksim Pecherskiy

Data Engineer

Servir des pages HTML

Service S3 d'HTML

Introduction à AWS Boto en Python

Table HTML avec pandas

Convertir un DataFrame en HTML

df.to_html('table_agg.html')

DataFrame vers HTML

Introduction à AWS Boto en Python

Table HTML avec pandas et liens

Convertir un DataFrame en HTML

df.to_html('table_agg.html', render_links=True)

DataFrame vers HTML

Introduction à AWS Boto en Python

Sélection de colonnes en HTML

Convertir un DataFrame en HTML

df.to_html('table_agg.html', 
           render_links=True,
           columns['service_name', 'request_count', 'info_link'])

DataFrame vers HTML

Introduction à AWS Boto en Python

Bordures

Convertir un DataFrame en HTML

df.to_html('table_agg.html', 
           render_links=True,
           columns['service_name', 'request_count', 'info_link'],
           border=0)

Bordures DataFrame vers HTML

Introduction à AWS Boto en Python

Charger un fichier HTML vers S3

Charger un fichier HTML vers S3

s3.upload_file(
  Filename='./table_agg.html', 
  Bucket='datacamp-website',

Key='table.html',
ExtraArgs = { 'ContentType': 'text/html', 'ACL': 'public-read'} )
Introduction à AWS Boto en Python

Accéder au fichier HTML

Modèle d'URL d'objet S3

https://{bucket}.{key}
https://datacamp-website.table.html
Introduction à AWS Boto en Python

Page HTML

Page HTML

Introduction à AWS Boto en Python

Charger d'autres types de contenu

Charger une image vers S3

s3.upload_file(
  Filename='./plot_image.png', 
  Bucket='datacamp-website',
  Key='plot_image.png',

ExtraArgs = { 'ContentType': 'image/png', 'ACL': 'public-read'} )
Introduction à AWS Boto en Python

Types MIME IANA

  • JSON : application/json
  • PNG : image/png
  • PDF : application/pdf
  • CSV : text/csv
1 http://www.iana.org/assignments/media-types/media-types.xhtml
Introduction à AWS Boto en Python

Générer une page d'index

# Lister les objets du bucket gid-reports commençant par 2019/
r = s3.list_objects(Bucket='gid-reports', Prefix='2019/')

# Convertir le contenu de la réponse en DataFrame objects_df = pd.DataFrame(r['Contents'])

Page d'index

Introduction à AWS Boto en Python

Générer une page d'index

# Créer une colonne "Link" contenant url du site + clé
base_url = "http://datacamp-website."
objects_df['Link'] = base_url + objects_df['Key']
# Écrire le DataFrame en HTML
objects_df.to_html('report_listing.html', 
                   columns=['Link', 'LastModified', 'Size'],
                   render_links=True)

Générer une page d'index

Introduction à AWS Boto en Python

Charger la page d'index

Charger un fichier HTML vers S3

s3.upload_file(
  Filename='./report_listing.html', 
  Bucket='datacamp-website',
  Key='index.html',
  ExtraArgs = {
    'ContentType': 'text/html', 
    'ACL': 'public-read'}
)

https://datacamp-website.index.html

Introduction à AWS Boto en Python

Récapitulatif

  • Table HTML avec pandas (df.to_html('table.html'))
  • Charger un fichier HTML (ContentType: text/html)
  • Charger une image (ContentType: image/png)
  • Partager l'URL de notre page HTML !
Introduction à AWS Boto en Python

Passons à la pratique !

Introduction à AWS Boto en Python

Preparing Video For Download...