Wiederverwendbare Dash-Komponenten

Dashboards mit Dash und Plotly erstellen

Alex Scriven

Data Scientist

DRY-Code

 

  • DRY = Don't Repeat Yourself (oder Refactoring)
    • Doppelte Codeanteile entfernen
  • Funktionen für wiederkehrende Aufgaben schreiben
Dashboards mit Dash und Plotly erstellen

DRY-Code: Beispiel

$$

sales_country = ecom_sales\
   .groupby('Country')['OrderValue']\
   .sum()\
   .reset_index(name='Total Sales ($)')\
   .sort_values('Total Sales ($)', 
                ascending=False)

sales_ma_cat = ecom_sales\ .groupby('Major Category')['OrderValue']\ .sum()\ .reset_index(name='Total Sales ($)')\ .sort_values('Total Sales ($)', ascending=False)

Refactored:

def sales_by(col):
    df = ecom_sales\
    .groupby(col)['OrderValue']\
    .sum()\
    .reset_index(name='Total Sales ($)')\
    .sort_values('Total Sales ($)', 
                 ascending=False)
    return df

# Mehrfach aufrufen sales_country = sales_by('Country') sales_ma_cat = sales_by('Major Category') sales_mi_cat = sales_by('Minor Category')
Dashboards mit Dash und Plotly erstellen

DRY in Dash

 

  • In Dash: Funktionen zum Refactoring nutzen
  • Anwendungsfälle (mit Funktionen):
    • HTML-(oder andere) Komponenten wiederverwenden
    • Einheitliches Styling hinzufügen (CSS kann fummelig sein!)
    • Styles aktualisieren
Dashboards mit Dash und Plotly erstellen

Komponenten wiederverwenden

Beispiel: stark gestyltes Logo

def create_logo():
  logo=html.Img(src=logo_link, style=
  {'margin':'30px 0px 0px 0px',
  'padding':'50px 50px',
  'border':'3px dotted lightblue',
  'background-color':'rgb(230,131,247)'
  })
  return logo

 

app.layout = [
  create_logo(),
  html.Div(),
  # Weitere Komponenten
  create_logo(),
  dcc.Graph(id='my_graph'),
  create_logo()
]

Das Logo wird 3-mal eingefügt!

Dashboards mit Dash und Plotly erstellen

Eine Komponentenliste generieren

Vorher:

app.layout = [
  html.Img(src=logo_link),
  html.Br(),
  html.Br(),
  html.H1("Sales breakdowns"),
  html.Br(),
  html.Br(),
  html.Br(),
  ...

Nachher:

def make_break(num_breaks):
    br_list = [html.Br()] * num_breaks
    return br_list

app.layout = [ html.Img(src=logo_link), *make_break(2), html.H1("Sales breakdowns"), *make_break(3), ...
Dashboards mit Dash und Plotly erstellen

Styling wiederverwenden

 

  • Gemeinsames Styling nutzen
  • Python-Dictionary .update() verwenden (Achtung: eindeutige Keys!)
d1 = {'Country':'Australia'}
d2 = {'City':'Sydney'}
d1.update(d2)
print(d1)
{'Country':'Australia', 'City':'Sydney'}
Dashboards mit Dash und Plotly erstellen

Styling-Funktionen in Dash

 

Funktion vorbereiten:

def style_c():
  corp_style={
    'margin':'0 auto',
    'border':'2px solid black',
    'display':'inline-block',
  }
  return corp_style

 

Aufruf im Dash-Layout:

app.layout = [
 html.Img(src=logo_link, 
 style=style_c()),

dcc.DatePickerSingle( style={'width':'200px'}.update(style_c()) )]
Dashboards mit Dash und Plotly erstellen

Lass uns üben!

Dashboards mit Dash und Plotly erstellen

Preparing Video For Download...